Search in sources :

Example 21 with InvalidPayloadException

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 + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) MethodCallExpression(org.apache.camel.model.language.MethodCallExpression) InvalidPayloadException(org.apache.camel.InvalidPayloadException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 22 with InvalidPayloadException

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);
}
Also used : Exchange(org.apache.camel.Exchange) Entry(java.util.Map.Entry) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 23 with InvalidPayloadException

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);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 24 with InvalidPayloadException

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

Example 25 with InvalidPayloadException

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

Aggregations

InvalidPayloadException (org.apache.camel.InvalidPayloadException)26 InputStream (java.io.InputStream)10 Exchange (org.apache.camel.Exchange)9 Message (org.apache.camel.Message)9 Processor (org.apache.camel.Processor)6 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 Endpoint (org.apache.camel.Endpoint)3 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)3 Entry (java.util.Map.Entry)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 JAXBException (javax.xml.bind.JAXBException)2 GenericFileEndpoint (org.apache.camel.component.file.GenericFileEndpoint)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StopWatch (org.apache.camel.util.StopWatch)2 Builder (com.google.protobuf.Message.Builder)1 JSchException (com.jcraft.jsch.JSchException)1 SftpException (com.jcraft.jsch.SftpException)1