Search in sources :

Example 6 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 7 with InvalidPayloadException

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

the class DefaultHttpBinding method doWriteDirectResponse.

protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
    // if content type is serialized Java object, then serialize and write it to the response
    String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
    if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
        if (allowJavaSerializedObject || isTransferException()) {
            try {
                Object object = message.getMandatoryBody(Serializable.class);
                HttpHelper.writeObjectToServletResponse(response, object);
                // object is written so return
                return;
            } catch (InvalidPayloadException e) {
                throw new IOException(e);
            }
        } else {
            throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
        }
    }
    // prefer streaming
    InputStream is = null;
    if (checkChunked(message, exchange)) {
        is = message.getBody(InputStream.class);
    } else {
        // try to use input stream first, so we can copy directly
        if (!isText(contentType)) {
            is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, message.getBody());
        }
    }
    if (is != null) {
        ServletOutputStream os = response.getOutputStream();
        if (!checkChunked(message, exchange)) {
            CachedOutputStream stream = new CachedOutputStream(exchange);
            try {
                // copy directly from input stream to the cached output stream to get the content length
                int len = copyStream(is, stream, response.getBufferSize());
                // we need to setup the length if message is not chucked
                response.setContentLength(len);
                OutputStream current = stream.getCurrentStream();
                if (current instanceof ByteArrayOutputStream) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Streaming (direct) response in non-chunked mode with content-length {}");
                    }
                    ByteArrayOutputStream bos = (ByteArrayOutputStream) current;
                    bos.writeTo(os);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Streaming response in non-chunked mode with content-length {} and buffer size: {}", len, len);
                    }
                    copyStream(stream.getInputStream(), os, len);
                }
            } finally {
                IOHelper.close(is, os);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Streaming response in chunked mode with buffer size {}", response.getBufferSize());
            }
            copyStream(is, os, response.getBufferSize());
        }
    } else {
        // not convertable as a stream so fallback as a String
        String data = message.getBody(String.class);
        if (data != null) {
            // set content length and encoding before we write data
            String charset = IOHelper.getCharsetName(exchange, true);
            final int dataByteLength = data.getBytes(charset).length;
            response.setCharacterEncoding(charset);
            response.setContentLength(dataByteLength);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Writing response in non-chunked mode as plain text with content-length {} and buffer size: {}", dataByteLength, response.getBufferSize());
            }
            try {
                response.getWriter().print(data);
            } finally {
                response.getWriter().flush();
            }
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Endpoint(org.apache.camel.Endpoint) CachedOutputStream(org.apache.camel.converter.stream.CachedOutputStream)

Example 8 with InvalidPayloadException

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

the class TokenXMLExpressionIterator method doEvaluate.

/**
     * Strategy to evaluate the exchange
     *
     * @param exchange   the exchange
     * @param closeStream whether to close the stream before returning from this method.
     * @return the evaluated value
     */
protected Object doEvaluate(Exchange exchange, boolean closeStream) {
    InputStream in = null;
    try {
        in = exchange.getIn().getMandatoryBody(InputStream.class);
        // we may read from a file, and want to support custom charset defined on the exchange
        String charset = IOHelper.getCharsetName(exchange);
        return createIterator(exchange, in, charset);
    } catch (InvalidPayloadException e) {
        exchange.setException(e);
        // must close input stream
        IOHelper.close(in);
        return null;
    } finally {
        if (closeStream) {
            IOHelper.close(in);
        }
    }
}
Also used : InputStream(java.io.InputStream) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 9 with InvalidPayloadException

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

the class BeanProxyNoBindingTest method testBeanProxyFailureInvalidReturnType.

public void testBeanProxyFailureInvalidReturnType() throws Exception {
    Endpoint endpoint = context.getEndpoint("direct:start");
    OrderService service = ProxyHelper.createProxy(endpoint, false, OrderService.class);
    try {
        service.invalidReturnType("<order type=\"beer\">Carlsberg</order>");
        fail("Should have thrown exception");
    } catch (Exception e) {
        // expected
        InvalidPayloadException cause = assertIsInstanceOf(InvalidPayloadException.class, e.getCause());
        assertEquals(Integer.class, cause.getType());
    }
}
Also used : Endpoint(org.apache.camel.Endpoint) InvalidPayloadException(org.apache.camel.InvalidPayloadException) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 10 with InvalidPayloadException

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

the class VelocitySetHeaderTest method assertRespondsWith.

protected void assertRespondsWith(final String value, String expectedBody) throws InvalidPayloadException, InterruptedException {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedHeaderReceived("fruit", value);
    mock.expectedBodiesReceived(expectedBody);
    template.request("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            Message in = exchange.getIn();
            in.setBody(value);
        }
    });
    mock.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) 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