blob: 9c77442dcc8195891dd3a82bbaa012fec072bc27 (
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
|
// 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.http;
import ai.vespa.util.http.VespaHttpClientBuilder;
import com.yahoo.container.jdisc.HttpResponse;
import java.util.logging.Level;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Logger;
public class SimpleHttpFetcher implements HttpFetcher {
private static final Logger logger = Logger.getLogger(SimpleHttpFetcher.class.getName());
private final CloseableHttpClient client = VespaHttpClientBuilder.create().build();
@Override
public HttpResponse get(Params params, URL url) {
try {
HttpGet request = new HttpGet(url.toURI());
request.addHeader("Connection", "Close");
request.setConfig(
RequestConfig.custom()
.setConnectTimeout(params.readTimeoutMs)
.setSocketTimeout(params.readTimeoutMs)
.build());
try (CloseableHttpResponse response = client.execute(request)) {
HttpEntity entity = response.getEntity();
return new StaticResponse(
response.getStatusLine().getStatusCode(),
entity.getContentType().getValue(),
EntityUtils.toString(entity));
}
} catch (ConnectTimeoutException | 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);
} catch (URISyntaxException e) {
String message = "Invalid URL: " + e.getMessage();
logger.log(Level.WARNING, message, e);
throw new InternalServerException(message, e);
}
}
}
|