aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/SimpleHttpFetcher.java
blob: 4d7cfe28676590cc6d29d2d7527272017801053c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import ai.vespa.util.http.hc5.VespaHttpClientBuilder;
import com.yahoo.config.provision.security.NodeHostnameVerifier;
import com.yahoo.container.jdisc.HttpResponse;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.util.Timeout;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.time.Duration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SimpleHttpFetcher implements HttpFetcher {

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

    private final CloseableHttpClient client;

    public SimpleHttpFetcher(Duration connectTimeout) {
        this(connectTimeout, null);
    }
    public SimpleHttpFetcher(Duration connectTimeout, NodeHostnameVerifier verifier) {
        VespaHttpClientBuilder builder = VespaHttpClientBuilder.custom().connectTimeout(Timeout.of(connectTimeout));
        if (verifier != null) {
            builder.hostnameVerifier(verifier::verify);
        }
        this.client = builder.buildClient();
    }

    @Override
    @SuppressWarnings("deprecation")
    public HttpResponse get(Params params, URI url) {
        try {
            HttpGet request = new HttpGet(url);
            request.addHeader("Connection", "Close");
            request.setConfig(
                    RequestConfig.custom()
                            .setResponseTimeout(Timeout.ofMilliseconds(params.readTimeoutMs))
                            .build());
            try (CloseableHttpResponse response = client.execute(request)) {
                HttpEntity entity = response.getEntity();
                return new StaticResponse(
                        response.getCode(),
                        entity.getContentType(),
                        entity.getContent().readAllBytes());
            }
        } catch (SocketTimeoutException e) {
            String message = "Timed out after " + params.readTimeoutMs + " ms reading response from " + url;
            logger.log(Level.WARNING, message, e);
            throw new RequestTimeoutException(message);
        } catch (IOException e) {
            String message = "Failed to get response from " + url;
            logger.log(Level.WARNING, message, e);
            throw new InternalServerException(message);
        }
    }

}