2017-04-10 11 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); 
    } 
} 
関連する問題