Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend default value to HTTP request path using Apache http client library

I am attempting to set a default base URI path at the time of client configuration using the Apache http client library. However, I cannot find any information on how to go about this.

Essentially, what I am looking to do is inject/prepend a base path onto a given request path by default. So if the request path is something like "/employees/1024", I would like to prepend the path with "/api/v1" so that I would end up with a URI path of "/api/v1/employees/1024" at time of request execution.

I am looking to do this at the time that I am building the HttpClient object. I can definitely implement this logic further down my stack, but I would like to avoid that if possible.

Does anyone have any idea about whether or not this is possible to set during HttpClient configuration? (Either by overriding a settable object method or otherwise)

like image 319
Joshua Jones Avatar asked Sep 13 '26 15:09

Joshua Jones


1 Answers

I never ended up finding a direct answer to my question. My solution was to extend the CloseableHttpClient abstract class, providing my path string to append along with a concrete instance of the CloseableHttpClient (used for composition) to the constructor. Then I prepended the path string onto the URL of the given HttpRequest objects in the overridden methods using the HttpRequestWrapper class.

Here is an example of my implementation:

class PureHttpClient extends CloseableHttpClient {
    private final CloseableHttpClient client;
    private final String service;

    PureHttpClient(CloseableHttpClient client, String service) {
        this.client = client;
        this.service = service;
    }

    @Override
    public void close() throws IOException {
        if (client != null)
            client.close();
    }

    private HttpUriRequest appendService(HttpRequest request, String service) throws ClientProtocolException {
        HttpRequestWrapper wrappedRequest = HttpRequestWrapper.wrap(request);

        try {
            URI uri = wrappedRequest.getURI();
            URI newUri = new URIBuilder(uri)
                    .setPath(service + uri.getPath())
                    .build();
            wrappedRequest.setURI(newUri);
        } catch (URISyntaxException e) {
            throw new ClientProtocolException(e.getMessage(), e);
        }
        return wrappedRequest;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public HttpParams getParams() {
        return client.getParams();
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return client.getConnectionManager();
    }

    @Override
    public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
        return client.execute(appendService(request, service));
    }

    @Override
    public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
        return client.execute(appendService(request, service), context);
    }

    @Override
    public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
        return client.execute(target, appendService(request, service));
    }

    @Override
    public CloseableHttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
        return client.execute(target, appendService(request, service), context);
    }

    @Override
    public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
        return client.execute(appendService(request, service), responseHandler);
    }

    @Override
    public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
        return client.execute(appendService(request, service), responseHandler);
    }

    @Override
    public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
        return client.execute(target, appendService(request, service), responseHandler);
    }

    @Override
    public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
        return client.execute(target, appendService(request, service), responseHandler);
    }

    @Override
    protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
        return this.execute(target, request, context);
    }
}
like image 69
Joshua Jones Avatar answered Sep 16 '26 04:09

Joshua Jones