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;
}
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);
}
};
}
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);
}
}
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;
}
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;
}
Aggregations