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)
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With