Search in sources :

Example 1 with CachedOutputStream

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;
}
Also used : InputStream(java.io.InputStream) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream) FallbackConverter(org.apache.camel.FallbackConverter) Converter(org.apache.camel.Converter) TypeConverter(org.apache.camel.TypeConverter)

Example 2 with CachedOutputStream

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);
    }
}
Also used : IOException(java.io.IOException) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 3 with CachedOutputStream

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();
            }
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Endpoint(org.apache.camel.Endpoint) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 4 with CachedOutputStream

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();
    }
}
Also used : CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 5 with CachedOutputStream

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);
    }
}
Also used : IOException(java.io.IOException) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Aggregations

CachedOutputStream (org.apache.camel.converter.stream.CachedOutputStream)9 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Exchange (org.apache.camel.Exchange)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 LinkedList (java.util.LinkedList)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Converter (org.apache.camel.Converter)1 Endpoint (org.apache.camel.Endpoint)1 FallbackConverter (org.apache.camel.FallbackConverter)1 InvalidPayloadException (org.apache.camel.InvalidPayloadException)1 Processor (org.apache.camel.Processor)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 TypeConverter (org.apache.camel.TypeConverter)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 StorageMetadata (org.jclouds.blobstore.domain.StorageMetadata)1 ListContainerOptions (org.jclouds.blobstore.options.ListContainerOptions)1