【问题标题】:Prepend default value to HTTP request path using Apache http client library使用 Apache http 客户端库将默认值添加到 HTTP 请求路径
【发布时间】:2017-09-05 19:10:56
【问题描述】:

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

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

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

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

【问题讨论】:

  • 我当前的解决方案涉及扩展 CloseableHttpClient 抽象类并使用 CloseableHttpClient 的子类实例的组合在执行方法覆盖实现期间预先添加基本路径。

标签: java apache configuration apache-httpclient-4.x


【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多