Search in sources :

Example 1 with HttpComponent

use of org.apache.camel.component.http.HttpComponent in project camel by apache.

the class HttpJavaBodyTest method testNotAllowedReceive.

@Test
public void testNotAllowedReceive() 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 {
            onException(Exception.class).to("mock:error");
            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();
    try {
        template.requestBody("http://localhost:{{port}}/myapp/myservice", "Hello World", MyCoolBean.class);
        fail("Should fail");
    } catch (Exception e) {
    // expected
    }
}
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 2 with HttpComponent

use of org.apache.camel.component.http.HttpComponent in project camel by apache.

the class HttpJavaBodyTest method testHttpSendJavaBodyAndReceiveJavaBody.

@Test
public void testHttpSendJavaBodyAndReceiveJavaBody() 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());
                    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("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) 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 3 with HttpComponent

use of org.apache.camel.component.http.HttpComponent in project camel by apache.

the class HttpComponentAutoConfiguration method configureHttpComponent.

@Lazy
@Bean(name = { "http-component", "https-component" })
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(HttpComponent.class)
public HttpComponent configureHttpComponent(CamelContext camelContext, HttpComponentConfiguration configuration) throws Exception {
    HttpComponent component = new HttpComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null, false);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : HashMap(java.util.HashMap) HttpComponent(org.apache.camel.component.http.HttpComponent) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with HttpComponent

use of org.apache.camel.component.http.HttpComponent in project camel by apache.

the class HttpBasicAuthComponentConfiguredTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            HttpConfiguration config = new HttpConfiguration();
            config.setAuthMethod("Basic");
            config.setAuthUsername("donald");
            config.setAuthPassword("duck");
            HttpComponent http = context.getComponent("http", HttpComponent.class);
            http.setHttpConfiguration(config);
            from("jetty://http://localhost:{{port}}/test?handlers=myAuthHandler").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
                    assertNotNull(req);
                    Principal user = req.getUserPrincipal();
                    assertNotNull(user);
                    assertEquals("donald", user.getName());
                }
            }).transform(constant("Bye World"));
            from("jetty://http://localhost:{{port}}/anotherTest?handlers=myAuthHandler").transform(constant("See you later"));
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) HttpServletRequest(javax.servlet.http.HttpServletRequest) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpConfiguration(org.apache.camel.http.common.HttpConfiguration) Principal(java.security.Principal)

Example 5 with HttpComponent

use of org.apache.camel.component.http.HttpComponent 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)

Aggregations

HttpComponent (org.apache.camel.component.http.HttpComponent)7 Exchange (org.apache.camel.Exchange)6 Processor (org.apache.camel.Processor)6 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 CamelExecutionException (org.apache.camel.CamelExecutionException)5 BaseJettyTest (org.apache.camel.component.jetty.BaseJettyTest)5 HttpCommonComponent (org.apache.camel.http.common.HttpCommonComponent)5 HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)5 Test (org.junit.Test)5 Principal (java.security.Principal)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpConfiguration (org.apache.camel.http.common.HttpConfiguration)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1