내 질문에 대한 직접적인 대답을 찾지 못했습니다. 내 솔루션은 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의 서브 클래스 인스턴스의 조성물을 사용하는 것을 포함한다. –