2017-04-10 56 views
0

我試圖在使用Apache http客戶端庫進行客戶端配置時設置默認基礎URI路徑。但是,我找不到任何有關如何解決此問題的信息。使用Apache http客戶端庫將默認值預先設置爲HTTP請求路徑

本質上,我正在尋找的是默認情況下在給定的請求路徑上插入/預先添加基本路徑。因此,如果請求路徑類似「/ employees/1024」,我想用「/ api/v1」來預先設置路徑,以便最終獲得「/ api/v1/employees/1024」的URI路徑在請求執行時。

我正在建立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); 
    } 
}