2017-04-10 3 views
0

Apache http 클라이언트 라이브러리를 사용하여 클라이언트를 구성 할 때 기본 기본 URI 경로를 설정하려고합니다. 그러나, 나는 이것에 대해 어떻게 가야하는지에 대한 정보를 찾을 수 없다.Apache HTTP 클라이언트 라이브러리를 사용하여 HTTP 요청 경로 앞에 기본값을 붙입니다.

본질적으로 내가하고 싶은 것은 기본적으로 지정된 요청 경로에 기본 경로를 삽입/추가하는 것입니다. 그래서 요청 경로가 "/ employees/1024"와 같은 경우 "/ api/v1/employees/1024"의 URI 경로로 끝날 수 있도록 "/ api/v1"과 함께 경로를 앞에두고 싶습니다. 요청이 실행될 때

HttpClient 개체를 만들 때이 작업을 수행하려고합니다. 확실히이 논리를 내 스택 아래로 구현할 수는 있지만 가능한 경우이를 피하고 싶습니다.

누구든지 HttpClient 구성 중에 설정할 수 있는지 여부를 알고 있습니까? (설정 가능한 객체 방법 또는 다른 방법을 무시하여)

+0

나의 현재 용액 CloseableHttpClient 추상 클래스를 확장하여 실행 방법 오버라이드 구현시의 기본 경로를 앞에 추가 CloseableHttpClient의 서브 클래스 인스턴스의 조성물을 사용하는 것을 포함한다. –

답변

1

내 질문에 대한 직접적인 대답을 찾지 못했습니다. 내 솔루션은 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); 
    } 
} 
관련 문제