1 /* 2 * Copyright 2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.openehealth.ipf.commons.ihe.ws.cxf.payload; 17 18 import java.io.FilterOutputStream; 19 import java.io.IOException; 20 import java.io.OutputStream; 21 import java.nio.charset.Charset; 22 23 /** 24 * An implementation of output stream which serves as a proxy for another output 25 * stream instance and collects the data pieces to be written in a string buffer 26 * (these pieces are XML and/or MIME artifacts). 27 * 28 * @author Dmytro Rud 29 */ 30 public class WrappedOutputStream extends FilterOutputStream { 31 32 private final String charsetName; 33 private final StringBuilder payloadCollector; 34 private boolean isActive; 35 36 /** 37 * Constructor. 38 * 39 * @param os 40 * the output data stream to be wrapped 41 * @param charsetName 42 * character set name, may be <code>null</code> if not known. 43 */ 44 public WrappedOutputStream(OutputStream os, String charsetName) { 45 super(os); 46 this.charsetName = (charsetName != null) ? charsetName : Charset.defaultCharset().name(); 47 isActive = true; 48 payloadCollector = new StringBuilder(); 49 } 50 51 52 /** 53 * Returns the collected message payload. 54 * @return SOAP payload as XML String. 55 */ 56 public String getCollectedPayload() { 57 return payloadCollector.toString(); 58 } 59 60 61 /** 62 * Deactivates any further data collecting. 63 */ 64 public void deactivate() { 65 isActive = false; 66 } 67 68 69 /* ----- implementation of standard OutputStream methods ----- */ 70 71 @Override 72 public void write(byte[] b, int off, int len) throws IOException { 73 super.write(b, off, len); 74 if (isActive) { 75 payloadCollector.append(new String(b, off, len, charsetName)); 76 } 77 } 78 }