Java 类org.apache.camel.component.http.HttpComponent 实例源码

项目:Camel    文件:HttpJavaBodyTest.java   
@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
    }
}
项目:Camel    文件:HttpJavaBodyTest.java   
@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());
    }
}
项目:Camel    文件:HttpBasicAuthComponentConfiguredTest.java   
@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"));
        }
    };
}
项目:Camel    文件:MultiThreadedHttpGetTest.java   
@Test
public void testHttpGetWithoutConversion() throws Exception {

    // This is needed as by default there are 2 parallel
    // connections to some host and there is nothing that
    // closes the http connection here.
    // Need to set the httpConnectionManager 
    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(5);
    context.getComponent("http", HttpComponent.class).setHttpConnectionManager(httpConnectionManager);


    String endpointName = "seda:withoutConversion?concurrentConsumers=5";
    sendMessagesTo(endpointName, 5);
}
项目:Camel    文件:HttpComponentAutoConfiguration.java   
@Bean
@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);
    IntrospectionSupport.setProperties(camelContext,
            camelContext.getTypeConverter(), component, parameters);
    return component;
}
项目:boundary-event-sdk    文件:BoundaryEventRouteBuilder.java   
/**
 * Configures the Camel route that receives {@link RawEvent}
 * and then sends to the Boundary API
 * 
 */
@Override
public void configure() {
    // Create the URL used to send events
    String url = String.format("https://%s:%d/v%d/events",host,port,version);
    LOG.debug("boundary event api url: " + url);

    // Configure our HTTP connection to use BASIC authentication
    HttpConfiguration config = new HttpConfiguration();
    config.setAuthMethod(AuthMethod.Basic);
    config.setAuthUsername(this.getUser());
    config.setAuthPassword(this.getPassword());

    HttpComponent http = this.getContext().getComponent("https",HttpComponent.class);
    http.setHttpConfiguration(config);

    from(fromUri)
        .startupOrder(startUpOrder)
        .routeId(routeId)
        .unmarshal().serialization()
        .marshal().json(JsonLibrary.Jackson)
        .log(INFO,"RawEvent: ${body}")
        .setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/json"))
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .to("log:com.boundary.sdk.event.BoundaryEventRouteBuilder?level=INFO&groupInterval=60000&groupDelay=60000&groupActiveOnly=false")
        .to(url.toString())
        .log(DEBUG,"HTTP Method: ${headers.CamelHttpMethod},AcceptContentType={headers.CamelAcceptContentType}")
        .log(INFO,"HTTP Response Code: ${headers.CamelHttpResponseCode},Location: ${headers.Location}")
        ;
}