Search in sources :

Example 11 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class MessageHelperTest method testResetStreamCache.

/*
     * Tests the {@link MessageHelper#resetStreamCache(Message)} method
     */
public void testResetStreamCache() throws Exception {
    // should not throw exceptions when Message or message body is null
    MessageHelper.resetStreamCache(null);
    MessageHelper.resetStreamCache(message);
    // handle StreamCache
    final ValueHolder<Boolean> reset = new ValueHolder<Boolean>(Boolean.FALSE);
    message.setBody(new StreamCache() {

        @SuppressWarnings("deprecation")
        public void reset() {
            reset.set(Boolean.TRUE);
        }

        public void writeTo(OutputStream os) throws IOException {
        // noop
        }

        public StreamCache copy(Exchange exchange) throws IOException {
            return null;
        }

        public boolean inMemory() {
            return true;
        }

        @Override
        public long length() {
            return 0;
        }
    });
    MessageHelper.resetStreamCache(message);
    assertTrue("Should have reset the stream cache", reset.get());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) StreamCache(org.apache.camel.StreamCache) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 12 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class DefaultStreamCachingStrategy method cache.

public StreamCache cache(Exchange exchange) {
    Message message = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
    StreamCache cache = message.getBody(StreamCache.class);
    if (cache != null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Cached stream to {} -> {}", cache.inMemory() ? "memory" : "spool", cache);
        }
        if (statistics.isStatisticsEnabled()) {
            try {
                if (cache.inMemory()) {
                    statistics.updateMemory(cache.length());
                } else {
                    statistics.updateSpool(cache.length());
                }
            } catch (Exception e) {
                LOG.debug("Error updating cache statistics. This exception is ignored.", e);
            }
        }
    }
    return cache;
}
Also used : Message(org.apache.camel.Message) StreamCache(org.apache.camel.StreamCache)

Example 13 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class WireTapProcessor method configureExchange.

protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) throws IOException {
    Exchange answer;
    if (copy) {
        // use a copy of the original exchange
        answer = configureCopyExchange(exchange);
    } else {
        // use a new exchange
        answer = configureNewExchange(exchange);
    }
    // prepare the exchange
    if (newExchangeExpression != null) {
        Object body = newExchangeExpression.evaluate(answer, Object.class);
        if (body != null) {
            answer.getIn().setBody(body);
        }
    }
    if (newExchangeProcessors != null) {
        for (Processor processor : newExchangeProcessors) {
            try {
                processor.process(answer);
            } catch (Exception e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }
    }
    // if the body is a stream cache we must use a copy of the stream in the wire tapped exchange
    Message msg = answer.hasOut() ? answer.getOut() : answer.getIn();
    if (msg.getBody() instanceof StreamCache) {
        // in parallel processing case, the stream must be copied, therefore get the stream
        StreamCache cache = (StreamCache) msg.getBody();
        StreamCache copied = cache.copy(answer);
        if (copied != null) {
            msg.setBody(copied);
        }
    }
    // invoke on prepare on the exchange if specified
    if (onPrepare != null) {
        try {
            onPrepare.process(answer);
        } catch (Exception e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) Message(org.apache.camel.Message) StreamCache(org.apache.camel.StreamCache) IOException(java.io.IOException)

Example 14 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class StreamCachingInterceptor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    StreamCache newBody = exchange.getIn().getBody(StreamCache.class);
    if (newBody != null) {
        exchange.getIn().setBody(newBody);
    }
    MessageHelper.resetStreamCache(exchange.getIn());
    return processor.process(exchange, callback);
}
Also used : StreamCache(org.apache.camel.StreamCache)

Example 15 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class StreamCachingInterceptorTest method testConvertInputStreamWithRouteBuilderStreamCaching.

public void testConvertInputStreamWithRouteBuilderStreamCaching() throws Exception {
    a.expectedMessageCount(1);
    InputStream message = new ByteArrayInputStream(MESSAGE.getBytes());
    template.sendBody("direct:a", message);
    assertMockEndpointsSatisfied();
    assertTrue(a.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
    assertEquals(a.assertExchangeReceived(0).getIn().getBody(String.class), MESSAGE);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCache(org.apache.camel.StreamCache) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Aggregations

StreamCache (org.apache.camel.StreamCache)38 InputStream (java.io.InputStream)14 IOException (java.io.IOException)10 File (java.io.File)7 Exchange (org.apache.camel.Exchange)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)5 StreamSource (javax.xml.transform.stream.StreamSource)5 KeyValueAnnotation (com.github.kristofa.brave.KeyValueAnnotation)4 StringReader (java.io.StringReader)4 OutputStream (java.io.OutputStream)3 SAXSource (javax.xml.transform.sax.SAXSource)3 Message (org.apache.camel.Message)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)3 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)3 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 Writer (java.io.Writer)2 Source (javax.xml.transform.Source)2