私の質問に直接答えを見つけることは決してありませんでした。私の解決策は、CloseableHttpClient
抽象クラスを拡張し、コンストラクタにCloseableHttpClient
(コンポジションに使用)の具体的なインスタンスとともに追加するパス文字列を提供することでした。次に、HttpRequestWrapper
クラスを使用して、オーバーライドされたメソッド内の指定されたHttpRequest
オブジェクトのURLにパス文字列を追加しました。ここ
は、私の実装の例である:
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);
}
}
私の現在のソリューションは、CloseableHttpClient抽象クラスを拡張し、実行メソッドのオーバーライド実装時のベースパスを付加するCloseableHttpClientのサブクラスのインスタンスの組成物を使用することを含みます。 –