1

I have a library which callers use to send HTTP requests by giving me HttpUriRequest (Apache HTTP client) objects. In some cases due to some tunneling which should be transparent to the caller, I need to modify the URL of the request to use HTTP instead of HTTPS.

How might I go about doing this? It seems like I can't just change it right on the object. The only thing I can see is to create a new HttpUriRequest object that is a clone of the other but with the URL changed.

Is there a better way?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
TR1096
  • 47
  • 8

1 Answers1

1

One could use a custom route planner to re-route outgoing requests to a different host / route

CloseableHttpClient client = HttpClients.custom()
        .setRoutePlanner(new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) {

            @Override
            public HttpRoute determineRoute(
                    final HttpHost target,
                    final HttpRequest request,
                    final HttpContext context) throws HttpException {
                return super.determineRoute(
                        target.getHostName().equals("overhere") ? new HttpHost("overthere", -1, "https") : target,
                        request,
                        context);
            }
        })
        .build();
ok2c
  • 26,450
  • 5
  • 63
  • 71
  • How about modifying whole request? 4.3 seems to lack suck func. I need to translate http://mytrigger.domain.com/path/val/val into http;//realdestination.com/realpath/val/val. – Antoniossss Nov 15 '19 at 08:43
  • `RequestBuilder` (introduced in 4.3) and `URIBuilder` (introduced in 4.2) to the rescue. – ok2c Nov 15 '19 at 10:06
  • And where can I modify that? The problem is I don't see any viable injection point. RoutePlanner is definetly not the place for it. (I am using dynamic proxy to wrap whole client right now) – Antoniossss Nov 15 '19 at 10:13
  • Request interceptor – ok2c Nov 15 '19 at 12:07