use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.
the class CxfConverter method soapMessageToInputStream.
@Converter
public static InputStream soapMessageToInputStream(final SOAPMessage soapMessage, Exchange exchange) throws SOAPException, IOException {
CachedOutputStream cos = new CachedOutputStream(exchange);
soapMessage.writeTo(cos);
InputStream in = cos.getInputStream();
return in;
}
use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.
the class HttpProducer method doExtractResponseBodyAsStream.
private static InputStream doExtractResponseBodyAsStream(InputStream is, Exchange exchange) throws IOException {
// As httpclient is using a AutoCloseInputStream, it will be closed when the connection is closed
// we need to cache the stream for it.
CachedOutputStream cos = null;
try {
// This CachedOutputStream will not be closed when the exchange is onCompletion
cos = new CachedOutputStream(exchange, false);
IOHelper.copy(is, cos);
// When the InputStream is closed, the CachedOutputStream will be closed
return cos.getWrappedInputStream();
} catch (IOException ex) {
// try to close the CachedOutputStream when we get the IOException
try {
cos.close();
} catch (IOException ignore) {
//do nothing here
}
throw ex;
} finally {
IOHelper.close(is, "Extracting response body", LOG);
}
}
use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.
the class DefaultHttpBinding method doWriteDirectResponse.
protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
// if content type is serialized Java object, then serialize and write it to the response
String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
if (allowJavaSerializedObject || isTransferException()) {
try {
Object object = message.getMandatoryBody(Serializable.class);
HttpHelper.writeObjectToServletResponse(response, object);
// object is written so return
return;
} catch (InvalidPayloadException e) {
throw new IOException(e);
}
} else {
throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
}
}
// prefer streaming
InputStream is = null;
if (checkChunked(message, exchange)) {
is = message.getBody(InputStream.class);
} else {
// try to use input stream first, so we can copy directly
if (!isText(contentType)) {
is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, message.getBody());
}
}
if (is != null) {
ServletOutputStream os = response.getOutputStream();
if (!checkChunked(message, exchange)) {
CachedOutputStream stream = new CachedOutputStream(exchange);
try {
// copy directly from input stream to the cached output stream to get the content length
int len = copyStream(is, stream, response.getBufferSize());
// we need to setup the length if message is not chucked
response.setContentLength(len);
OutputStream current = stream.getCurrentStream();
if (current instanceof ByteArrayOutputStream) {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming (direct) response in non-chunked mode with content-length {}");
}
ByteArrayOutputStream bos = (ByteArrayOutputStream) current;
bos.writeTo(os);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming response in non-chunked mode with content-length {} and buffer size: {}", len, len);
}
copyStream(stream.getInputStream(), os, len);
}
} finally {
IOHelper.close(is, os);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming response in chunked mode with buffer size {}", response.getBufferSize());
}
copyStream(is, os, response.getBufferSize());
}
} else {
// not convertable as a stream so fallback as a String
String data = message.getBody(String.class);
if (data != null) {
// set content length and encoding before we write data
String charset = IOHelper.getCharsetName(exchange, true);
final int dataByteLength = data.getBytes(charset).length;
response.setCharacterEncoding(charset);
response.setContentLength(dataByteLength);
if (LOG.isDebugEnabled()) {
LOG.debug("Writing response in non-chunked mode as plain text with content-length {} and buffer size: {}", dataByteLength, response.getBufferSize());
}
try {
response.getWriter().print(data);
} finally {
response.getWriter().flush();
}
}
}
}
use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.
the class HttpHelper method readResponseBodyFromInputStream.
/**
* Reads the response body from the given input stream.
*
* @param is the input stream
* @param exchange the exchange
* @return the response body, can be <tt>null</tt> if no body
* @throws IOException is thrown if error reading response body
*/
public static Object readResponseBodyFromInputStream(InputStream is, Exchange exchange) throws IOException {
if (is == null) {
return null;
}
// convert the input stream to StreamCache if the stream cache is not disabled
if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
return is;
} else {
CachedOutputStream cos = new CachedOutputStream(exchange);
IOHelper.copyAndCloseInput(is, cos);
return cos.newStreamCache();
}
}
use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.
the class HttpProducer method doExtractResponseBodyAsStream.
private static InputStream doExtractResponseBodyAsStream(InputStream is, Exchange exchange) throws IOException {
// As httpclient is using a AutoCloseInputStream, it will be closed when the connection is closed
// we need to cache the stream for it.
CachedOutputStream cos = null;
try {
// This CachedOutputStream will not be closed when the exchange is onCompletion
cos = new CachedOutputStream(exchange, false);
IOHelper.copy(is, cos);
// When the InputStream is closed, the CachedOutputStream will be closed
return cos.getWrappedInputStream();
} catch (IOException ex) {
// try to close the CachedOutputStream when we get the IOException
try {
cos.close();
} catch (IOException ignore) {
//do nothing here
}
throw ex;
} finally {
IOHelper.close(is, "Extracting response body", LOG);
}
}
Aggregations