Search in sources :

Example 1 with AhcComponent

use of org.apache.camel.component.ahc.AhcComponent in project camel by apache.

the class AhcProduceJavaBodyTest method testHttpSendStringAndReceiveJavaBody.

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

        @Override
        public void configure() throws Exception {
            from(getTestServerEndpointUri()).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, AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean reply = template.requestBody(getAhcEndpointUri(), "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) AhcComponent(org.apache.camel.component.ahc.AhcComponent) BaseAhcTest(org.apache.camel.component.ahc.BaseAhcTest) Test(org.junit.Test)

Example 2 with AhcComponent

use of org.apache.camel.component.ahc.AhcComponent in project camel by apache.

the class AhcProduceJavaBodyTest method testNotAllowedReceive.

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

        @Override
        public void configure() throws Exception {
            from(getTestServerEndpointUri()).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, AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    Object reply = template.requestBody(getAhcEndpointUri(), "Hello World", Object.class);
    MyCoolBean bean = context.getTypeConverter().convertTo(MyCoolBean.class, reply);
    assertNull(bean);
}
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) AhcComponent(org.apache.camel.component.ahc.AhcComponent) BaseAhcTest(org.apache.camel.component.ahc.BaseAhcTest) Test(org.junit.Test)

Example 3 with AhcComponent

use of org.apache.camel.component.ahc.AhcComponent in project camel by apache.

the class AhcProduceJavaBodyTest method testHttpSendJavaBodyAndReceiveJavaBody.

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

        @Override
        public void configure() throws Exception {
            from(getTestServerEndpointUri()).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, AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    MyCoolBean reply = template.requestBodyAndHeader(getAhcEndpointUri(), cool, Exchange.CONTENT_TYPE, AhcConstants.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) AhcComponent(org.apache.camel.component.ahc.AhcComponent) BaseAhcTest(org.apache.camel.component.ahc.BaseAhcTest) Test(org.junit.Test)

Example 4 with AhcComponent

use of org.apache.camel.component.ahc.AhcComponent in project camel by apache.

the class AhcProduceJavaBodyTest method testNotAllowed.

@Test
public void testNotAllowed() throws Exception {
    JettyHttpComponent jetty = context.getComponent("jetty", JettyHttpComponent.class);
    jetty.setAllowJavaSerializedObject(false);
    AhcComponent ahc = context.getComponent("ahc", AhcComponent.class);
    ahc.setAllowJavaSerializedObject(false);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from(getTestServerEndpointUri()).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, AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    try {
        template.requestBodyAndHeader(getAhcEndpointUri(), cool, Exchange.CONTENT_TYPE, AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, MyCoolBean.class);
        fail("Should fail");
    } catch (Exception e) {
        assertTrue(e.getCause().getMessage().startsWith("Content-type application/x-java-serialized-object is not allowed"));
    }
}
Also used : Exchange(org.apache.camel.Exchange) JettyHttpComponent(org.apache.camel.component.jetty.JettyHttpComponent) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) AhcComponent(org.apache.camel.component.ahc.AhcComponent) BaseAhcTest(org.apache.camel.component.ahc.BaseAhcTest) Test(org.junit.Test)

Example 5 with AhcComponent

use of org.apache.camel.component.ahc.AhcComponent in project camel by apache.

the class AhcComponentAutoConfiguration method configureAhcComponent.

@Lazy
@Bean(name = "ahc-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(AhcComponent.class)
public AhcComponent configureAhcComponent(CamelContext camelContext, AhcComponentConfiguration configuration) throws Exception {
    AhcComponent component = new AhcComponent();
    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) AhcComponent(org.apache.camel.component.ahc.AhcComponent) 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)

Aggregations

AhcComponent (org.apache.camel.component.ahc.AhcComponent)6 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 BaseAhcTest (org.apache.camel.component.ahc.BaseAhcTest)5 Test (org.junit.Test)5 HttpCommonComponent (org.apache.camel.http.common.HttpCommonComponent)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JettyHttpComponent (org.apache.camel.component.jetty.JettyHttpComponent)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