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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.health;
import org.apache.http.HttpResponse;
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.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
/**
* @author hakonhall
*/
class ApacheHttpClient implements AutoCloseable {
private final URL url;
private final CloseableHttpClient client;
@FunctionalInterface
interface Handler<T> {
T handle(CloseableHttpResponse httpResponse) throws Exception;
}
static CloseableHttpClient makeCloseableHttpClient(URL url, Duration timeout, Duration keepAlive, ConnectionSocketFactory socketFactory) {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(url.getProtocol(),socketFactory)
.build();
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout((int) timeout.toMillis()) // establishment of connection
.setConnectionRequestTimeout((int) timeout.toMillis()) // connection from connection manager
.setSocketTimeout((int) timeout.toMillis()) // waiting for data
.build();
ConnectionKeepAliveStrategy keepAliveStrategy =
new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long keepAliveMillis = super.getKeepAliveDuration(response, context);
if (keepAliveMillis == -1) {
keepAliveMillis = keepAlive.toMillis();
}
return keepAliveMillis;
}
};
return HttpClients.custom()
.setKeepAliveStrategy(keepAliveStrategy)
.setConnectionManager(connectionManager)
.disableAutomaticRetries()
.setDefaultRequestConfig(requestConfig)
.build();
}
ApacheHttpClient(URL url, Duration timeout, Duration keepAlive, ConnectionSocketFactory socketFactory) {
this(url, makeCloseableHttpClient(url, timeout, keepAlive, socketFactory));
}
ApacheHttpClient(URL url, CloseableHttpClient client) {
this.url = url;
this.client = client;
}
<T> T get(Handler<T> handler) throws Exception {
try (CloseableHttpResponse httpResponse = client.execute(new HttpGet(url.toString()))) {
return handler.handle(httpResponse);
}
}
@Override
public void close() throws IOException {
client.close();
}
}
|