aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/application/HttpProxy.java
blob: 8c53a9bc9d51391f5074572de76f0adb2530477f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.application;

import com.google.inject.Inject;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.PortInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.container.jdisc.HttpResponse;
import java.util.logging.Level;
import com.yahoo.vespa.config.server.http.HttpErrorResponse;
import com.yahoo.vespa.config.server.http.HttpFetcher;
import com.yahoo.vespa.config.server.http.NotFoundException;
import com.yahoo.vespa.config.server.http.SimpleHttpFetcher;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class HttpProxy {

    private static Logger logger = Logger.getLogger(HttpProxy.class.getName());

    private final HttpFetcher fetcher;

    @Inject
    public HttpProxy() { this(new SimpleHttpFetcher()); }
    public HttpProxy(HttpFetcher fetcher) {
        this.fetcher = fetcher;
    }

    public HttpResponse get(Application application, String hostName, String serviceType, String relativePath) {
        HostInfo host = application.getModel().getHosts().stream()
                .filter(hostInfo -> hostInfo.getHostname().equals(hostName))
                .findFirst()
                .orElseThrow(() -> new NotFoundException("Failed to find host " + hostName));

        ServiceInfo service = host.getServices().stream()
                .filter(serviceInfo -> serviceType.equals(serviceInfo.getServiceType()))
                .findFirst()
                .orElseThrow(() -> new NotFoundException("Failed to find any service of type " + serviceType
                        + " on host " + hostName));

        // "http" and "state" seems to uniquely identify an interesting HTTP port on each service
        PortInfo port = service.getPorts().stream()
                .filter(portInfo -> portInfo.getTags().stream().collect(Collectors.toSet()).containsAll(
                        Stream.of("http", "state").collect(Collectors.toSet())))
                .findFirst()
                .orElseThrow(() -> new NotFoundException("Failed to find HTTP state port"));

        return internalGet(host.getHostname(), port.getPort(), relativePath);
    }

    private HttpResponse internalGet(String hostname, int port, String relativePath) {
        String urlString = "http://" + hostname + ":" + port + "/" + relativePath;
        URL url;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            logger.log(Level.WARNING, "Badly formed url: " + urlString, e);
            return HttpErrorResponse.internalServerError("Failed to construct URL for backend");
        }

        HttpFetcher.Params params = new HttpFetcher.Params(2000); // 2_000 ms read timeout
        return fetcher.get(params, url);
    }

}