summaryrefslogtreecommitdiffstats
path: root/tenant-cd/src/main/java/ai/vespa/hosted/cd/http/HttpEndpoint.java
blob: a9d8f2e7cc5a6f821aad3632432c68fb57b6e736 (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
package ai.vespa.hosted.cd.http;

import ai.vespa.hosted.api.Authenticator;
import ai.vespa.hosted.cd.Digest;
import ai.vespa.hosted.cd.Feed;
import ai.vespa.hosted.cd.Query;
import ai.vespa.hosted.cd.Search;
import ai.vespa.hosted.cd.Selection;
import ai.vespa.hosted.cd.TestEndpoint;
import ai.vespa.hosted.cd.Visit;
import ai.vespa.hosted.cd.metric.Metrics;

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 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 TestEndpoint {

    static final String metricsPath = "/state/v1/metrics"; // TODO metrics/v1/values?
    static final String documentApiPath = "/document/v1";
    static final String searchApiPath = "/search";

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

    public HttpEndpoint(URI endpoint, Authenticator authenticator) {
        this.endpoint = requireNonNull(endpoint);
        this.authenticator = requireNonNull(authenticator);
        this.client = HttpClient.newBuilder()
                                .sslContext(authenticator.sslContext())
                                .connectTimeout(Duration.ofSeconds(5))
                                .version(HttpClient.Version.HTTP_1_1)
                                .build();
    }

    @Override
    public Digest digest(Feed feed) {
        return null;
    }

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

    @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(e);
        }
    }

    @Override
    public Search search(Query query) {
        try {
            URI target = endpoint.resolve(searchApiPath).resolve("?" + query.rawQuery());
            HttpResponse<byte[]> response = send(HttpRequest.newBuilder(target)
                                                            .timeout(query.timeout().orElse(Duration.ofMillis(500))
                                                                          .plus(Duration.ofSeconds(1))),
                                                 HttpResponse.BodyHandlers.ofByteArray());
            if (response.statusCode() / 100 != 2) // TODO consider allowing 504 if specified.
                throw new RuntimeException("Non-OK status code " + response.statusCode() + " at " + target +
                                           ", with response \n" + new String(response.body()));

            return toSearch(response.body());
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    static Search toSearch(byte[] body) {
        // TODO jvenstad
        // Inspector rootObject = new JsonDecoder().decode(new Slime(), body).get();
        return new Search(new String(body, UTF_8));
    }

    @Override
    public Visit visit(Selection selection) {
        return null;
    }

    @Override
    public Metrics metrics() {
        return null;
    }

}