使用 Apache http 客户端库将默认值添加到 HTTP 请求路径

Prepend default value to HTTP request path using Apache http client library

我正在尝试使用 Apache http 客户端库在客户端配置时设置默认的基本 URI 路径。但是,我找不到有关如何处理此问题的任何信息。

本质上,我要做的是 inject/prepend 默认情况下给定请求路径上的基本路径。因此,如果请求路径类似于“/employees/1024”,我想在路径前加上“/api/v1”,这样我最终会得到一个 URI 路径“/api/v1/employees/1024" 在请求执行时。

我希望在构建 HttpClient 对象时执行此操作。我绝对可以在我的堆栈中进一步实现这个逻辑,但我想尽可能避免这种情况。

有没有人知道这是否可以在 HttpClient 配置期间设置? (通过覆盖可设置的对象方法或其他方式)

我从来没有找到我的问题的直接答案。我的解决方案是扩展 CloseableHttpClient 抽象 class,提供我的路径字符串以将 CloseableHttpClient(用于组合)的具体实例附加到构造函数。然后我使用 HttpRequestWrapper class.

将路径字符串添加到覆盖方法中给定 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);
    }
}