use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class ExpressionBuilder method mandatoryBodyOgnlExpression.
/**
* Returns the expression for the exchanges inbound message body converted
* to the given type and invoking methods on the converted body defined in a simple OGNL notation
*/
public static Expression mandatoryBodyOgnlExpression(final String name, final String ognl) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = simpleExpression(name).evaluate(exchange, String.class);
Class<?> type;
try {
type = exchange.getContext().getClassResolver().resolveMandatoryClass(text);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
Object body;
try {
body = exchange.getIn().getMandatoryBody(type);
} catch (InvalidPayloadException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
// ognl is able to evaluate method name if it contains nested functions
// so we should not eager evaluate ognl as a string
MethodCallExpression call = new MethodCallExpression(exchange, ognl);
// set the instance to use
call.setInstance(body);
return call.evaluate(exchange);
}
@Override
public String toString() {
return "mandatoryBodyAs[" + name + "](" + ognl + ")";
}
};
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class FreemarkerTemplateInHeaderTest method assertRespondsWith.
protected void assertRespondsWith(final String headerName, final String headerValue, String expectedBody) throws InvalidPayloadException {
Exchange response = template.request("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setHeader(FreemarkerConstants.FREEMARKER_TEMPLATE, "<hello>${headers." + headerName + "}</hello>");
in.setHeader(headerName, headerValue);
}
});
assertOutMessageBodyEquals(response, expectedBody);
Object template = response.getOut().getHeader(FreemarkerConstants.FREEMARKER_TEMPLATE);
assertNull("Template header should have been removed", template);
Set<Entry<String, Object>> entrySet = response.getOut().getHeaders().entrySet();
boolean keyFound = false;
for (Entry<String, Object> entry : entrySet) {
if (entry.getKey().equals(headerName)) {
keyFound = true;
}
}
assertTrue("Header should been found", keyFound);
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class DefaultHttpBinding method doWriteGZIPResponse.
protected void doWriteGZIPResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
byte[] bytes;
try {
bytes = message.getMandatoryBody(byte[].class);
} catch (InvalidPayloadException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
byte[] data = GZIPHelper.compressGZIP(bytes);
ServletOutputStream os = response.getOutputStream();
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Streaming response as GZIP in non-chunked mode with content-length {} and buffer size: {}", data.length, response.getBufferSize());
}
response.setContentLength(data.length);
os.write(data);
os.flush();
} finally {
IOHelper.close(os);
}
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class SimulatorTest method assertRespondsWith.
protected void assertRespondsWith(final String value, String containedText) throws InvalidPayloadException {
Exchange response = template.request("direct:a", new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setBody("answer");
in.setHeader("cheese", value);
}
});
assertNotNull("Should receive a response!", response);
String text = response.getOut().getMandatoryBody(String.class);
assertStringContains(text, containedText);
}
use of org.apache.camel.InvalidPayloadException in project camel by apache.
the class IronMQProducer method process.
public void process(Exchange exchange) throws Exception {
IronMQConfiguration configuration = getEndpoint().getConfiguration();
if (IronMQConstants.CLEARQUEUE.equals(exchange.getIn().getHeader(IronMQConstants.OPERATION, String.class))) {
this.ironQueue.clear();
} else {
Object messageId = null;
Object body = exchange.getIn().getBody();
if (body instanceof String[]) {
messageId = this.ironQueue.pushMessages((String[]) body, configuration.getVisibilityDelay());
} else if (body instanceof String) {
if (configuration.isPreserveHeaders()) {
body = GsonUtil.getBodyFromMessage(exchange.getIn());
}
messageId = this.ironQueue.push((String) body, configuration.getVisibilityDelay());
} else {
throw new InvalidPayloadException(exchange, String.class);
}
LOG.trace("Send request [{}] from exchange [{}]...", body, exchange);
LOG.trace("Received messageId [{}]", messageId);
Message message = getMessageForResponse(exchange);
message.setHeader(IronMQConstants.MESSAGE_ID, messageId);
}
}
Aggregations