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());
}
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;
}
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;
}
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);
}
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);
}
Aggregations