Search in sources :

Example 21 with DefaultExchange

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message)

Example 22 with DefaultExchange

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;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange)

Example 23 with DefaultExchange

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) URL(java.net.URL)

Example 24 with DefaultExchange

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Templates(javax.xml.transform.Templates) File(java.io.File) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) FileInputStream(java.io.FileInputStream) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 25 with DefaultExchange

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
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ExpectedBodyTypeException(org.apache.camel.ExpectedBodyTypeException) URL(java.net.URL)

Aggregations

DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Exchange (org.apache.camel.Exchange)381 Test (org.junit.Test)254 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)127 CamelContext (org.apache.camel.CamelContext)54 RegisteredDelivery (org.jsmpp.bean.RegisteredDelivery)39 HashMap (java.util.HashMap)33 Message (org.apache.camel.Message)32 Before (org.junit.Before)32 Tx (org.nhindirect.common.tx.model.Tx)31 ESMClass (org.jsmpp.bean.ESMClass)30 Processor (org.apache.camel.Processor)22 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)22 Expression (org.apache.camel.Expression)21 File (java.io.File)20 DefaultMessage (org.apache.camel.impl.DefaultMessage)20 ArrayList (java.util.ArrayList)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 URL (java.net.URL)16 Date (java.util.Date)16