Search in sources :

Example 6 with CachedOutputStream

use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.

the class JcloudsBlobStoreConsumer method poll.

@Override
protected int poll() throws Exception {
    shutdownRunningTask = null;
    pendingExchanges = 0;
    Queue<Exchange> queue = new LinkedList<Exchange>();
    String directory = endpoint.getDirectory();
    ListContainerOptions opt = new ListContainerOptions();
    if (!Strings.isNullOrEmpty(directory)) {
        opt = opt.inDirectory(directory);
    }
    for (StorageMetadata md : blobStore.list(container, opt.maxResults(maxMessagesPerPoll).recursive())) {
        String blobName = md.getName();
        if (md.getType().equals(StorageType.BLOB)) {
            if (!Strings.isNullOrEmpty(blobName)) {
                InputStream body = JcloudsBlobStoreHelper.readBlob(blobStore, container, blobName);
                if (body != null) {
                    Exchange exchange = endpoint.createExchange();
                    CachedOutputStream cos = new CachedOutputStream(exchange);
                    IOHelper.copy(body, cos);
                    exchange.getIn().setBody(cos.newStreamCache());
                    exchange.setProperty(JcloudsConstants.BLOB_NAME, blobName);
                    queue.add(exchange);
                }
            }
        }
    }
    return queue.isEmpty() ? 0 : processBatch(CastUtils.cast(queue));
}
Also used : Exchange(org.apache.camel.Exchange) ListContainerOptions(org.jclouds.blobstore.options.ListContainerOptions) StorageMetadata(org.jclouds.blobstore.domain.StorageMetadata) InputStream(java.io.InputStream) LinkedList(java.util.LinkedList) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 7 with CachedOutputStream

use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.

the class RestletHelper 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 java.io.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 8 with CachedOutputStream

use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.

the class HttpHelper method readRequestBodyFromInputStream.

/**
     * 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 readRequestBodyFromInputStream(InputStream is, Exchange exchange) throws IOException {
    if (is == null) {
        return null;
    }
    boolean disableStreamCaching = false;
    // Just take the consideration of the setting of Camel Context StreamCaching
    if (exchange.getContext() instanceof DefaultCamelContext) {
        DefaultCamelContext context = (DefaultCamelContext) exchange.getContext();
        disableStreamCaching = !context.isStreamCaching();
    }
    // convert the input stream to StreamCache if the stream cache is not disabled
    if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, disableStreamCaching, Boolean.class)) {
        return is;
    } else {
        CachedOutputStream cos = new CachedOutputStream(exchange);
        IOHelper.copyAndCloseInput(is, cos);
        return cos.newStreamCache();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 9 with CachedOutputStream

use of org.apache.camel.converter.stream.CachedOutputStream in project camel by apache.

the class JettyChuckedFalseTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/test?matchOnUriPrefix=true&chunked=false").to("http://localhost:{{port2}}/other?bridgeEndpoint=true");
            from("jetty:http://localhost:{{port2}}/other").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "image/jpeg");
                    CachedOutputStream stream = new CachedOutputStream(exchange);
                    stream.write("This is hello world.".getBytes());
                    exchange.getOut().setBody(stream.getInputStream());
                    IOHelper.close(stream);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) 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