aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identity/SiaBackedApacheHttpClient.java
blob: fb7798b8edffd8b30b886eb68aeb418069d39732 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identity;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
 * A thread-safe http client wrapper on Apache's {@link HttpClient} that handles TLS client authentication using {@link ServiceIdentityProvider}.
 *
 * @author bjorncs
 */
public class SiaBackedApacheHttpClient implements AutoCloseable {

    private final Object clientReferenceLock = new Object();
    private final Supplier<SSLContext> sslContextSupplier;
    private final HttpClientFactory httpClientFactory;
    private HttpClientHolder client;

    public SiaBackedApacheHttpClient(ServiceIdentityProvider identityProvider,
                                     HttpClientFactory httpClientFactory) {
        this(identityProvider::getIdentitySslContext, httpClientFactory);
    }

    public SiaBackedApacheHttpClient(SSLContext sslContext,
                                     HttpClientFactory httpClientFactory) {
        this(() -> sslContext, httpClientFactory);
    }

    public SiaBackedApacheHttpClient(Supplier<SSLContext> sslContextSupplier,
                                     HttpClientFactory httpClientFactory) {
        this.sslContextSupplier = sslContextSupplier;
        this.httpClientFactory = httpClientFactory;
        synchronized (clientReferenceLock) {
            this.client = new HttpClientHolder(httpClientFactory, sslContextSupplier);
        }
    }

    public <T> T execute(HttpUriRequest request, ResponseHandler<T> responseHandler) {
        HttpClientHolder client = getClient();
        try {
            // Note: Apache http client will handle response/connection cleanup for this overload of execute().
            return client.apacheClient.execute(request, responseHandler);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            client.release();
        }
    }

    private HttpClientHolder getClient() {
        HttpClientHolder client;
        synchronized (clientReferenceLock) {
            if (sslContextSupplier.get() != this.client.sslContext) {
                this.client.release();
                this.client = new HttpClientHolder(httpClientFactory, sslContextSupplier);
            }
            client = this.client;
        }
        return client.refer();
    }

    @Override
    public void close() {
        HttpClientHolder client;
        synchronized (clientReferenceLock) {
            client = this.client;
        }
        client.release();
    }

    private static class HttpClientHolder {
        final Supplier<SSLContext> sslContextSupplier;
        final CloseableHttpClient apacheClient;
        final SSLContext sslContext;
        final AtomicInteger referenceCount = new AtomicInteger(1); // Owner's reference implicitly counted

        HttpClientHolder(HttpClientFactory clientBuilder, Supplier<SSLContext> sslContextSupplier) {
            SSLContext sslContext = sslContextSupplier.get();
            this.sslContextSupplier = sslContextSupplier;
            this.apacheClient = clientBuilder.createHttpClient(sslContext);
            this.sslContext = sslContext;
        }

        HttpClientHolder refer() {
            if (referenceCount.get() == 0) throw new IllegalStateException("Client already closed!");
            referenceCount.incrementAndGet();
            return this;
        }

        void release() {
            if (referenceCount.decrementAndGet() == 0) {
                try {
                    apacheClient.close();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }
    }

    /**
     * A factory that returns a new instance of {@link CloseableHttpClient}. The implementor is responsible for configuring the {@link SSLContext}, e.g. using {@link HttpClientBuilder#setSSLContext(SSLContext)}.
     */
    @FunctionalInterface
    public interface HttpClientFactory {
        CloseableHttpClient createHttpClient(SSLContext sslContext);
    }
}