Search in sources :

Example 91 with Message

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;
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) InputStream(java.io.InputStream) Iterator(java.util.Iterator) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 92 with Message

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;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) Message(org.apache.camel.Message) StreamCache(org.apache.camel.StreamCache) IOException(java.io.IOException)

Example 93 with Message

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();
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) DelegateEndpoint(org.apache.camel.DelegateEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint)

Example 94 with Message

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

Example 95 with Message

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);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) DataHandler(javax.activation.DataHandler)

Aggregations

Message (org.apache.camel.Message)721 Exchange (org.apache.camel.Exchange)341 Test (org.junit.Test)215 Processor (org.apache.camel.Processor)118 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)104 DefaultMessage (org.apache.camel.impl.DefaultMessage)51 DefaultExchange (org.apache.camel.impl.DefaultExchange)44 Endpoint (org.apache.camel.Endpoint)40 Response (javax.ws.rs.core.Response)38 InputStream (java.io.InputStream)36 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)26 RouteBuilder (org.apache.camel.builder.RouteBuilder)25 Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)25 ActionResponse (org.openstack4j.model.common.ActionResponse)25 IOException (java.io.IOException)24 Map (java.util.Map)24 DataHandler (javax.activation.DataHandler)21 Producer (org.apache.camel.Producer)21 CxfOperationException (org.apache.camel.component.cxf.CxfOperationException)19