use of org.apache.camel.StreamCache in project camel by apache.
the class StreamCacheConverterTest method testConvertToStreamCache.
public void testConvertToStreamCache() throws Exception {
context.start();
ByteArrayInputStream inputStream = new ByteArrayInputStream(MESSAGE.getBytes());
StreamCache streamCache = StreamCacheConverter.convertToStreamCache(new SAXSource(new InputSource(inputStream)), exchange);
String message = exchange.getContext().getTypeConverter().convertTo(String.class, streamCache);
assertNotNull(message);
assertEquals("The converted message is wrong", MESSAGE, message);
}
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