在Apache Camel的http4端点上使用http代理

时间:2017-02-27 10:34:47

标签: java apache-camel

我试图将http代理与Camel的http4组件一起使用。使用Intellij的HTTP代理"检查连接"选项。

但是,我不知道如何通过Camel正确配置它。运行以下集成测试时," ConnectException:Connection超时"被扔了。任何人都可以澄清如何正确设置代理详细信息吗?

public class SimpleHttpProxyIT extends CamelTestSupport {
    public static final String DIRECT_START = "direct:start";
    public static final String MOCK_RESULT = "mock:result";

    @Produce(uri = DIRECT_START)
    protected ProducerTemplate basic;

    @EndpointInject(uri = MOCK_RESULT)
    protected MockEndpoint resultEndpoint;

    @Test
    public void testBasic() throws Exception {
        basic.sendBody(null);
        resultEndpoint.setExpectedMessageCount(1);
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    public RouteBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(DIRECT_START)
                        .id("SunriseTest")
                        .log(LoggingLevel.INFO, "About to hit sunrise")
                        .setHeader(Exchange.HTTP_URI, simple("http://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400"))
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                exchange.getProperties().put("http.proxyAuthHost", "myproxy.company.org");
                                exchange.getProperties().put("http.proxyAuthPort", "10000");
                                exchange.getProperties().put("http.proxyAuthMethod", "Basic");
                                exchange.getProperties().put("http.proxyAuthUsername", "myusername");
                                exchange.getProperties().put("http.proxyAuthPassword", "mypassword");
                            }
                        })
                       .recipientList(simple("http4:dummyhost"))
                        .log(LoggingLevel.INFO, "Done")
                        .to(MOCK_RESULT);
            }
        };
    }
}

2 个答案:

答案 0 :(得分:0)

我认为它应该是exchange.setProperty(...)

答案 1 :(得分:0)

设置URI中的属性。我误读了"使用URI之外的代理设置" (http://camel.apache.org/http4.html)因为这指的是在上下文上设置它们,而不是交换

public class SimpleHttpProxyIT extends CamelTestSupport {
    public static final String DIRECT_START = "direct:start";
    public static final String MOCK_RESULT = "mock:result";

    @Produce(uri = DIRECT_START)
    protected ProducerTemplate basic;

    @EndpointInject(uri = MOCK_RESULT)
    protected MockEndpoint resultEndpoint;

    @Test
    public void testBasic() throws Exception {
        basic.sendBody(null);
        resultEndpoint.setExpectedMessageCount(1);
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    public RouteBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(DIRECT_START)
                        .id("SunriseTest")
                        .log(LoggingLevel.INFO, "About to hit sunrise")
                        .setHeader(Exchange.HTTP_URI, simple("http://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400"))
                       .recipientList(simple("http4:dummyhost?proxyAuthHost=myproxy.company.org&proxyAuthPort=10000&proxyAuthUsername=myusername&proxyAuthPassword=mypassword"))
                        .log(LoggingLevel.INFO, "Done")
                        .to(MOCK_RESULT);
            }
        };
    }
}