use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class ProcessorTransformer method transform.
/**
* Perform data transformation with specified from/to type using Processor.
* @param message message to apply transformation
* @param from 'from' data type
* @param to 'to' data type
*/
@Override
public void transform(Message message, DataType from, DataType to) throws Exception {
Exchange exchange = message.getExchange();
CamelContext context = exchange.getContext();
if (from.isJavaType()) {
Object input = message.getBody();
Class<?> fromClass = context.getClassResolver().resolveClass(from.getName());
if (!fromClass.isAssignableFrom(input.getClass())) {
LOG.debug("Converting to '{}'", fromClass.getName());
input = context.getTypeConverter().mandatoryConvertTo(fromClass, input);
message.setBody(input);
}
}
LOG.debug("Sending to transform processor '{}'", processor);
DefaultExchange transformExchange = new DefaultExchange(exchange);
transformExchange.setIn(message);
transformExchange.setProperties(exchange.getProperties());
processor.process(transformExchange);
Message answer = transformExchange.hasOut() ? transformExchange.getOut() : transformExchange.getIn();
if (to.isJavaType()) {
Object answerBody = answer.getBody();
Class<?> toClass = context.getClassResolver().resolveClass(to.getName());
if (!toClass.isAssignableFrom(answerBody.getClass())) {
LOG.debug("Converting to '{}'", toClass.getName());
answerBody = context.getTypeConverter().mandatoryConvertTo(toClass, answerBody);
answer.setBody(answerBody);
}
}
message.copyFrom(answer);
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class Enricher method createResourceExchange.
/**
* Creates a new {@link DefaultExchange} instance from the given
* <code>exchange</code>. The resulting exchange's pattern is defined by
* <code>pattern</code>.
*
* @param source exchange to copy from.
* @param pattern exchange pattern to set.
* @return created exchange.
*/
protected Exchange createResourceExchange(Exchange source, ExchangePattern pattern) {
// copy exchange, and do not share the unit of work
Exchange target = ExchangeHelper.createCorrelatedCopy(source, false);
target.setPattern(pattern);
// if we share unit of work, we need to prepare the resource exchange
if (isShareUnitOfWork()) {
target.setProperty(Exchange.PARENT_UNIT_OF_WORK, source.getUnitOfWork());
// and then share the unit of work
target.setUnitOfWork(source.getUnitOfWork());
}
return target;
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class XsltBuilderTest method testXsltTransformerUrl.
public void testXsltTransformerUrl() throws Exception {
URL styleSheet = getClass().getResource("example.xsl");
XsltBuilder builder = new XsltBuilder();
builder.setTransformerURL(styleSheet);
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("<hello>world!</hello>");
builder.process(exchange);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class XsltBuilderTest method testXsltTemplates.
public void testXsltTemplates() throws Exception {
File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
Source source = new SAXSource(new InputSource(new FileInputStream(file)));
XmlConverter converter = new XmlConverter();
Templates styleSheet = converter.getTransformerFactory().newTemplates(source);
XsltBuilder builder = XsltBuilder.xslt(styleSheet);
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("<hello>world!</hello>");
builder.process(exchange);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class XsltBuilderTest method testFailNullBody.
public void testFailNullBody() throws Exception {
URL styleSheet = getClass().getResource("example.xsl");
XsltBuilder builder = XsltBuilder.xslt(styleSheet);
builder.setFailOnNullBody(true);
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody(null);
try {
builder.process(exchange);
fail("Should thrown an exception");
} catch (ExpectedBodyTypeException e) {
// expected
}
}
Aggregations