aboutsummaryrefslogtreecommitdiffstats
path: root/tenant-cd-commons/src/main/java/ai/vespa/hosted/cd/commons/HttpEndpoint.java
blob: b9983d3e188aa43f08ebe1ece40ca9ef3ff6016d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.cd.commons;

import ai.vespa.hosted.cd.Endpoint;
import ai.vespa.hosted.cd.EndpointAuthenticator;

import javax.net.ssl.SSLParameters;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Collectors;

import static java.net.URLEncoder.encode;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

/**
 * A remote endpoint in a {@link HttpDeployment} of a Vespa application, reachable over HTTP.
 *
 * @author jonmv
 */
public class HttpEndpoint implements Endpoint {

    private final URI endpoint;
    private final HttpClient client;
    private final EndpointAuthenticator authenticator;

    public HttpEndpoint(URI endpoint, EndpointAuthenticator authenticator) {
        this.endpoint = requireNonNull(endpoint);
        this.authenticator = requireNonNull(authenticator);
        SSLParameters sslParameters = new SSLParameters();
        sslParameters.setProtocols(new String[] { "TLSv1.2" });
        this.client = HttpClient.newBuilder()
                                .sslContext(authenticator.sslContext())
                                .connectTimeout(Duration.ofSeconds(5))
                                .version(HttpClient.Version.HTTP_1_1)
                                .sslParameters(sslParameters)
                                .build();
    }

    @Override
    public URI uri() {
        return endpoint;
    }

    @Override
    public EndpointAuthenticator authenticator() {
        return authenticator;
    }

    @Override
    public <T> HttpResponse<T> send(HttpRequest.Builder request, HttpResponse.BodyHandler<T> handler) {
        try {
            return client.send(authenticator.authenticated(request).build(), handler);
        }
        catch (IOException | InterruptedException e) {
            throw new RuntimeException(request.build() + " failed: " + e.getMessage(), e);
        }
    }

    @Override
    public HttpRequest.Builder request(String path, Map<String, String> properties) {
        return HttpRequest.newBuilder(endpoint.resolve(path +
                                                       properties.entrySet().stream()
                                                                 .map(entry -> encode(entry.getKey(), UTF_8) + "=" + encode(entry.getValue(), UTF_8))
                                                                 .collect(Collectors.joining("&", path.contains("?") ? "&" : "?", ""))));
    }

}