Search in sources :

Example 6 with HttpCommonComponent

use of org.apache.camel.http.common.HttpCommonComponent in project camel by apache.

the class JettyHttpProducerJavaBodyTest method testHttpSendJavaBodyAndReceiveJavaBody.

@Test
public void testHttpSendJavaBodyAndReceiveJavaBody() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    MyCoolBean cool = exchange.getIn().getBody(MyCoolBean.class);
                    assertNotNull(cool);
                    assertEquals(123, cool.getId());
                    assertEquals("Camel", cool.getName());
                    MyCoolBean reply = new MyCoolBean(456, "Camel rocks");
                    exchange.getOut().setBody(reply);
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    MyCoolBean reply = template.requestBodyAndHeader("jetty:http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, MyCoolBean.class);
    assertEquals(456, reply.getId());
    assertEquals("Camel rocks", reply.getName());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 7 with HttpCommonComponent

use of org.apache.camel.http.common.HttpCommonComponent in project camel by apache.

the class JettyHttpProducerJavaBodyTest method testHttpSendJavaBodyAndReceiveString.

@Test
public void testHttpSendJavaBodyAndReceiveString() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    MyCoolBean cool = exchange.getIn().getBody(MyCoolBean.class);
                    assertNotNull(cool);
                    assertEquals(123, cool.getId());
                    assertEquals("Camel", cool.getName());
                    // we send back plain test
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/plain");
                    exchange.getOut().setBody("OK");
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    String reply = template.requestBodyAndHeader("jetty:http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, String.class);
    assertEquals("OK", reply);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 8 with HttpCommonComponent

use of org.apache.camel.http.common.HttpCommonComponent in project camel by apache.

the class HttpJavaBodyTest method testHttpSendStringAndReceiveJavaBody.

@Test
public void testHttpSendStringAndReceiveJavaBody() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(true);
    HttpComponent http = context.getComponent("http", HttpComponent.class);
    http.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String body = exchange.getIn().getBody(String.class);
                    assertNotNull(body);
                    assertEquals("Hello World", body);
                    MyCoolBean reply = new MyCoolBean(456, "Camel rocks");
                    exchange.getOut().setBody(reply);
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean reply = template.requestBody("http://localhost:{{port}}/myapp/myservice", "Hello World", MyCoolBean.class);
    assertEquals(456, reply.getId());
    assertEquals("Camel rocks", reply.getName());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExecutionException(org.apache.camel.CamelExecutionException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 9 with HttpCommonComponent

use of org.apache.camel.http.common.HttpCommonComponent in project camel by apache.

the class HttpJavaBodyTest method testNotAllowed.

@Test
public void testNotAllowed() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(false);
    HttpComponent http = context.getComponent("http", HttpComponent.class);
    http.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String body = exchange.getIn().getBody(String.class);
                    assertNotNull(body);
                    assertEquals("Hello World", body);
                    MyCoolBean reply = new MyCoolBean(456, "Camel rocks");
                    exchange.getOut().setBody(reply);
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    try {
        template.requestBodyAndHeader("http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, MyCoolBean.class);
        fail("Should fail");
    } catch (CamelExecutionException e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(415, cause.getStatusCode());
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExecutionException(org.apache.camel.CamelExecutionException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 10 with HttpCommonComponent

use of org.apache.camel.http.common.HttpCommonComponent in project camel by apache.

the class HttpJavaBodyTest method testHttpSendJavaBodyAndReceiveString.

@Test
public void testHttpSendJavaBodyAndReceiveString() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(true);
    HttpComponent http = context.getComponent("http", HttpComponent.class);
    http.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    MyCoolBean cool = exchange.getIn().getBody(MyCoolBean.class);
                    assertNotNull(cool);
                    assertEquals(123, cool.getId());
                    assertEquals("Camel", cool.getName());
                    // we send back plain test
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/plain");
                    exchange.getOut().setBody("OK");
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    String reply = template.requestBodyAndHeader("http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, String.class);
    assertEquals("OK", reply);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExecutionException(org.apache.camel.CamelExecutionException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Aggregations

Exchange (org.apache.camel.Exchange)12 Processor (org.apache.camel.Processor)12 RouteBuilder (org.apache.camel.builder.RouteBuilder)12 HttpCommonComponent (org.apache.camel.http.common.HttpCommonComponent)12 Test (org.junit.Test)12 BaseJettyTest (org.apache.camel.component.jetty.BaseJettyTest)8 CamelExecutionException (org.apache.camel.CamelExecutionException)5 HttpComponent (org.apache.camel.component.http.HttpComponent)5 HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)5 AhcComponent (org.apache.camel.component.ahc.AhcComponent)4 BaseAhcTest (org.apache.camel.component.ahc.BaseAhcTest)4