aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBase.java
blob: d824303df579cdacd9323e24e7c6a1c00e462c6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;

public class AsyncHttpClientWithBase<V extends HttpResult> implements AsyncHttpClient<V> {

    protected final AsyncHttpClient<V> client;
    private HttpRequest baseRequest = new HttpRequest();

    public AsyncHttpClientWithBase(AsyncHttpClient<V> client) {
        if (client == null) throw new IllegalArgumentException("HTTP client must be set.");
        this.client = client;
    }

    /**
     * If all your http requests have common features you want to set once, you can provide those values in a base
     * request. For instance, if you specify a host and a port using this function, all your requests will use that
     * host and port unless specified in the request you execute.
     */
    public void setHttpRequestBase(HttpRequest r) {
        this.baseRequest = (r == null ? new HttpRequest() : r.clone());
    }

    public HttpRequest getHttpRequestBase() {
        return baseRequest;
    }

    @Override
    public AsyncOperation<V> execute(HttpRequest r) {
        return client.execute(baseRequest.merge(r));
    }

    @Override
    public void close() {
        client.close();
    }

}