Search in sources :

Example 86 with Message

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

Example 87 with Message

use of org.apache.camel.Message in project camel by apache.

the class DefaultProducerTemplate method createBodyAndPropertyProcessor.

protected Processor createBodyAndPropertyProcessor(final Object body, final String property, final Object propertyValue) {
    return new Processor() {

        public void process(Exchange exchange) {
            exchange.setProperty(property, propertyValue);
            Message in = exchange.getIn();
            in.setBody(body);
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ConvertBodyProcessor(org.apache.camel.processor.ConvertBodyProcessor) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message)

Example 88 with Message

use of org.apache.camel.Message in project camel by apache.

the class RestBindingAdvice method setCORSHeaders.

private void setCORSHeaders(Exchange exchange, Map<String, Object> state) {
    // add the CORS headers after routing, but before the consumer writes the response
    Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
    // use default value if none has been configured
    String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin") : null;
    if (allowOrigin == null) {
        allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
    }
    String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods") : null;
    if (allowMethods == null) {
        allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
    }
    String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers") : null;
    if (allowHeaders == null) {
        allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
    }
    String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") : null;
    if (maxAge == null) {
        maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
    }
    String allowCredentials = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Credentials") : null;
    // Restrict the origin if credentials are allowed.
    // https://www.w3.org/TR/cors/ - section 6.1, point 3
    String origin = exchange.getIn().getHeader("Origin", String.class);
    if ("true".equalsIgnoreCase(allowCredentials) && "*".equals(allowOrigin) && origin != null) {
        allowOrigin = origin;
    }
    msg.setHeader("Access-Control-Allow-Origin", allowOrigin);
    msg.setHeader("Access-Control-Allow-Methods", allowMethods);
    msg.setHeader("Access-Control-Allow-Headers", allowHeaders);
    msg.setHeader("Access-Control-Max-Age", maxAge);
    if (allowCredentials != null) {
        msg.setHeader("Access-Control-Allow-Credentials", allowCredentials);
    }
}
Also used : Message(org.apache.camel.Message)

Example 89 with Message

use of org.apache.camel.Message in project camel by apache.

the class SetBodyProcessor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
        Object newBody = expression.evaluate(exchange, Object.class);
        if (exchange.getException() != null) {
            // the expression threw an exception so we should break-out
            callback.done(true);
            return true;
        }
        boolean out = exchange.hasOut();
        Message old = out ? exchange.getOut() : exchange.getIn();
        // create a new message container so we do not drag specialized message objects along
        // but that is only needed if the old message is a specialized message
        boolean copyNeeded = !(old.getClass().equals(DefaultMessage.class));
        if (copyNeeded) {
            Message msg = new DefaultMessage();
            msg.copyFromWithNewBody(old, newBody);
            // replace message on exchange
            ExchangeHelper.replaceMessage(exchange, msg, false);
        } else {
            // no copy needed so set replace value directly
            old.setBody(newBody);
        }
    } catch (Throwable e) {
        exchange.setException(e);
    }
    callback.done(true);
    return true;
}
Also used : DefaultMessage(org.apache.camel.impl.DefaultMessage) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage)

Example 90 with Message

use of org.apache.camel.Message in project camel by apache.

the class SetHeaderProcessor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
        Object newHeader = expression.evaluate(exchange, Object.class);
        if (exchange.getException() != null) {
            // the expression threw an exception so we should break-out
            callback.done(true);
            return true;
        }
        boolean out = exchange.hasOut();
        Message old = out ? exchange.getOut() : exchange.getIn();
        String key = headerName.evaluate(exchange, String.class);
        old.setHeader(key, newHeader);
    } catch (Throwable e) {
        exchange.setException(e);
    }
    callback.done(true);
    return true;
}
Also used : Message(org.apache.camel.Message)

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