Search in sources :

Example 1 with InputStreamCache

use of org.apache.camel.converter.stream.InputStreamCache in project camel by apache.

the class HttpRouteTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            port1 = getPort();
            port2 = getNextPort();
            port3 = getNextPort();
            port4 = getNextPort();
            // enable stream cache
            context.setStreamCaching(true);
            from("jetty:http://localhost:" + port1 + "/test").to("mock:a");
            Processor proc = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    try {
                        HttpMessage message = (HttpMessage) exchange.getIn();
                        HttpSession session = message.getRequest().getSession();
                        assertNotNull("we should get session here", session);
                    } catch (Exception e) {
                        exchange.getOut().setFault(true);
                        exchange.getOut().setBody(e);
                    }
                    exchange.getOut().setBody("<b>Hello World</b>");
                }
            };
            from("jetty:http://localhost:" + port1 + "/responseCode").setHeader(Exchange.HTTP_RESPONSE_CODE, simple("400"));
            Processor printProcessor = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Message out = exchange.getOut();
                    out.copyFrom(exchange.getIn());
                    log.info("The body's object is " + exchange.getIn().getBody());
                    log.info("Process body = " + exchange.getIn().getBody(String.class));
                    InputStreamCache cache = out.getBody(InputStreamCache.class);
                    cache.reset();
                }
            };
            from("jetty:http://localhost:" + port2 + "/hello?sessionSupport=true").process(proc);
            from("jetty:http://localhost:" + port1 + "/echo").process(printProcessor).process(printProcessor);
            Processor procParameters = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // As the request input stream is cached by DefaultHttpBinding,
                    // HttpServletRequest can't get the parameters of post message
                    String value = exchange.getIn().getHeader("request", String.class);
                    if (value != null) {
                        assertNotNull("The value of the parameter should not be null", value);
                        exchange.getOut().setBody(value);
                    } else {
                        exchange.getOut().setBody("Can't get a right parameter");
                    }
                }
            };
            from("jetty:http://localhost:" + port1 + "/parameter").process(procParameters);
            from("jetty:http://localhost:" + port1 + "/postxml").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String value = exchange.getIn().getBody(String.class);
                    assertEquals("The response message is wrong", value, POST_MESSAGE);
                    exchange.getOut().setBody("OK");
                }
            });
            from("jetty:http://localhost:" + port3 + "/noStreamCache?disableStreamCache=true").noStreamCaching().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    InputStream is = (InputStream) exchange.getIn().getBody();
                    assertTrue("It should be a raw inputstream", is instanceof org.eclipse.jetty.server.HttpInput);
                    String request = exchange.getIn().getBody(String.class);
                    assertEquals("Got a wrong request", "This is a test", request);
                    exchange.getOut().setBody("OK");
                }
            });
            from("jetty:http://localhost:" + port4 + "/requestBufferSize").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String string = exchange.getIn().getBody(String.class);
                    exchange.getOut().setBody(string);
                }
            });
        }
    };
}
Also used : Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message) HttpMessage(org.apache.camel.http.common.HttpMessage) HttpSession(javax.servlet.http.HttpSession) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Exchange(org.apache.camel.Exchange) InputStreamCache(org.apache.camel.converter.stream.InputStreamCache) HttpMessage(org.apache.camel.http.common.HttpMessage)

Example 2 with InputStreamCache

use of org.apache.camel.converter.stream.InputStreamCache in project camel by apache.

the class OnExceptionUseOriginalMessageTest method testOnExceptionStreamReset.

public void testOnExceptionStreamReset() throws Exception {
    getMockEndpoint("mock:middle").expectedMessageCount(1);
    getMockEndpoint("mock:middle").message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class);
    getMockEndpoint("mock:end").expectedMessageCount(1);
    getMockEndpoint("mock:end").message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class);
    InputStreamCache cache = new InputStreamCache(TEST_STRING.getBytes());
    template.sendBody("direct:a", cache);
    assertMockEndpointsSatisfied();
    // To make sure we can read something from the InputStream
    String result = getMockEndpoint("mock:end").getExchanges().get(0).getIn().getBody(String.class);
    assertTrue(result.contains("<firstName>James</firstName>"));
}
Also used : InputStreamCache(org.apache.camel.converter.stream.InputStreamCache)

Example 3 with InputStreamCache

use of org.apache.camel.converter.stream.InputStreamCache in project camel by apache.

the class ZipDataFormatTest method testStreamCacheUnzip.

public void testStreamCacheUnzip() throws Exception {
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").streamCaching().marshal().zip().unmarshal().zip().to("mock:result");
        }
    });
    context.start();
    MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedBodiesReceived(TEXT);
    sendText();
    result.assertIsSatisfied();
    List<Exchange> exchangeList = result.getExchanges();
    assertTrue(exchangeList.get(0).getIn().getBody() instanceof InputStreamCache);
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStreamCache(org.apache.camel.converter.stream.InputStreamCache)

Aggregations

InputStreamCache (org.apache.camel.converter.stream.InputStreamCache)3 Exchange (org.apache.camel.Exchange)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpSession (javax.servlet.http.HttpSession)1 Message (org.apache.camel.Message)1 Processor (org.apache.camel.Processor)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 HttpMessage (org.apache.camel.http.common.HttpMessage)1