0

I am using Apache Camel 2.13.1 as a HTTP proxy using the http4 component:

.from("servlet://*?matchOnUriPrefix=true")
.to("http4://example.com/?bridgeEndpoint=true&httpClient.redirectsEnabled=false")

Now I need to set the Host HTTP header parameter of the outgoing proxy request to the backend system because since it is used by the proxied application to generate absolute links, it has to match the public/frontend url. Using the naive approach of simply setting the Host header in the camel message .setHeader("Host", "foo.com") fails due to the http4 component overriding this with the hostname of the proxied host example.com.

Further research revealed, that HTTPClient used to do this via the virtual-host param. Camel's http4 component supports pass-through HTTPClient params using the httpClient param. However since version 2.13.0, camel http4 uses HTTPClient 4.3's builder api (http://camel.apache.org/http4.html) to pass through the httpClient.* params from the endpoint configuration and unfortunately HTTPClient 4.3's builder api does not include the virtual-host param anymore. Judging by this HTTPClient mailing list reply (https://mail-archives.apache.org/mod_mbox//hc-httpclient-users/201312.mbox/%3C1387792931.6163.17.camel@ubuntu%3E) it looks like I may have to set the virtual host, which is appears to be called target host, on the HttpClientContext via setTargetHost. How can I do this via camel in between?

So to sum up: I am using camel's http4 component and need to change the Host HTTP header value of the outgoing proxy request.

BlueDog
  • 966
  • 8
  • 15

2 Answers2

1

You can setup the HttpContext instance by using the httpContext option like this. Please make sure the HttpContext instance is bind with "customerContext" in the Registry.

http4://localhost:8081?httpBindingRef=customBinding&httpClientConfigurerRef=customConfigurer&httpContext=#customContext
Willem Jiang
  • 3,291
  • 1
  • 14
  • 14
  • I'm accepting this as the answer since it documents how to gain access to the HttpClient internals from Camel which should eventually lead to success. However I couldn't get it to work myself, because the HttpClient configuration is very complicated and I ran out of time. – BlueDog Mar 25 '15 at 10:46
  • HttpClient configuration doesn't help you with that, you need to apply a customContext to setup the Host value as you want. – Willem Jiang Mar 26 '15 at 14:18
  • This solved it for me: http://stackoverflow.com/questions/9499697/configuring-apache-httpclient-to-access-service-through-proxy-load-balancer-ove – jpt Sep 09 '16 at 19:11
0

Following this post Configuring Apache HttpClient to access service through proxy/load-balancer (overriding Host header)

This snippet worked for me:

            HttpComponent http4 = camelContext.getComponent("http4", HttpComponent.class);

            http4.setHttpClientConfigurer(new HttpClientConfigurer() {

                @Override
                public void configureHttpClient(HttpClientBuilder builder) {

                    builder.addInterceptorFirst(new HttpRequestInterceptor() {
                        @Override
                        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                            request.setHeader(HTTP.TARGET_HOST, publicUrl);
                        }
                    });
                }

            });
Community
  • 1
  • 1
jpt
  • 453
  • 7
  • 12