use of org.apache.camel.Message in project camel by apache.
the class UnmarshalProcessor method process.
public boolean process(Exchange exchange, AsyncCallback callback) {
ObjectHelper.notNull(dataFormat, "dataFormat");
InputStream stream = null;
Object result = null;
try {
stream = exchange.getIn().getMandatoryBody(InputStream.class);
// lets setup the out message before we invoke the dataFormat so that it can mutate it if necessary
Message out = exchange.getOut();
out.copyFrom(exchange.getIn());
result = dataFormat.unmarshal(exchange, stream);
if (result instanceof Exchange) {
if (result != exchange) {
// it's not allowed to return another exchange other than the one provided to dataFormat
throw new RuntimeCamelException("The returned exchange " + result + " is not the same as " + exchange + " provided to the DataFormat");
}
} else if (result instanceof Message) {
// the dataformat has probably set headers, attachments, etc. so let's use it as the outbound payload
exchange.setOut((Message) result);
} else {
out.setBody(result);
}
} catch (Throwable e) {
// remove OUT message, as an exception occurred
exchange.setOut(null);
exchange.setException(e);
} finally {
// The Iterator will close the stream itself
if (!(result instanceof Iterator)) {
IOHelper.close(stream, "input stream");
}
}
callback.done(true);
return true;
}
use of org.apache.camel.Message 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.Message in project camel by apache.
the class EndpointHelper method browseRangeMessagesAsXml.
/**
* Browses the {@link BrowsableEndpoint} within the given range, and returns the messages as a XML payload.
*
* @param endpoint the browsable endpoint
* @param fromIndex from range
* @param toIndex to range
* @param includeBody whether to include the message body in the XML payload
* @return XML payload with the messages
* @throws IllegalArgumentException if the from and to range is invalid
* @see MessageHelper#dumpAsXml(org.apache.camel.Message)
*/
public static String browseRangeMessagesAsXml(BrowsableEndpoint endpoint, Integer fromIndex, Integer toIndex, Boolean includeBody) {
if (fromIndex == null) {
fromIndex = 0;
}
if (toIndex == null) {
toIndex = Integer.MAX_VALUE;
}
if (fromIndex > toIndex) {
throw new IllegalArgumentException("From index cannot be larger than to index, was: " + fromIndex + " > " + toIndex);
}
List<Exchange> exchanges = endpoint.getExchanges();
if (exchanges.size() == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append("<messages>");
for (int i = fromIndex; i < exchanges.size() && i <= toIndex; i++) {
Exchange exchange = exchanges.get(i);
Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
String xml = MessageHelper.dumpAsXml(msg, includeBody);
sb.append("\n").append(xml);
}
sb.append("\n</messages>");
return sb.toString();
}
use of org.apache.camel.Message in project camel by apache.
the class PredicateBuilderTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
Message in = exchange.getIn();
in.setBody("Hello there!");
in.setHeader("name", "James");
in.setHeader("location", "Islington,London,UK");
in.setHeader("size", 10);
}
use of org.apache.camel.Message in project camel by apache.
the class ExpressionClauseTest method testAttachments.
public void testAttachments() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
mock.expectedBodiesReceivedInAnyOrder("log4j2.properties", "jndi-example.properties");
template.send("direct:begin", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message m = exchange.getIn();
m.setBody("Hello World");
m.addAttachmentObject("log4j", new DefaultAttachment(new FileDataSource("src/test/resources/log4j2.properties")));
m.addAttachment("jndi-example", new DataHandler(new FileDataSource("src/test/resources/jndi-example.properties")));
}
});
assertMockEndpointsSatisfied();
Map<String, Attachment> attachments = mock.getExchanges().get(0).getIn().getAttachmentObjects();
assertTrue(attachments == null || attachments.size() == 0);
}
Aggregations