forked from splunk/kafka-connect-splunk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHttpClientBuilder.java
144 lines (125 loc) · 5.16 KB
/
HttpClientBuilder.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2017 Splunk, Inc..
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.splunk.hecclient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.security.cert.X509Certificate;
public final class HttpClientBuilder {
private int maxConnectionPoolSizePerDestination = 4;
private int maxConnectionPoolSize = 4 * 2;
private int socketTimeout = 60; // in seconds
private int connectionRequestTimeout = 60; // in seconds
private int connectionTimeout = 60; // in seconds
private int socketSendBufferSize = 8 * 1024 * 1024; // in bytes
private boolean disableSSLCertVerification = false;
private SSLContext sslContext = null;
public HttpClientBuilder setMaxConnectionPoolSizePerDestination(int connections) {
this.maxConnectionPoolSizePerDestination = connections;
return this;
}
public HttpClientBuilder setMaxConnectionPoolSize(int connections) {
this.maxConnectionPoolSize = connections;
return this;
}
public HttpClientBuilder setSocketTimeout(int timeout /*seconds*/) {
this.socketTimeout = timeout;
return this;
}
public HttpClientBuilder setConnectionRequestTimeout(int timeout /*seconds*/) {
this.connectionRequestTimeout = timeout;
return this;
}
public HttpClientBuilder setConnectionTimeout(int timeout /*seconds*/) {
this.connectionTimeout = timeout;
return this;
}
public HttpClientBuilder setSocketSendBufferSize(int bufSize /*bytes*/) {
this.socketSendBufferSize = bufSize;
return this;
}
public HttpClientBuilder setDisableSSLCertVerification(boolean disableVerification) {
disableSSLCertVerification = disableVerification;
return this;
}
public HttpClientBuilder setSslContext(SSLContext context) {
this.sslContext = context;
return this;
}
public CloseableHttpClient build() {
SSLConnectionSocketFactory sslFactory = getSSLConnectionFactory();
SocketConfig config = SocketConfig.custom()
.setSndBufSize(socketSendBufferSize)
.setSoTimeout(socketTimeout * 1000)
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout * 1000)
.setConnectionRequestTimeout(connectionRequestTimeout * 1000)
.setConnectTimeout(connectionTimeout * 1000)
.setCookieSpec(CookieSpecs.STANDARD)
.build();
return HttpClients.custom()
.useSystemProperties()
.setSSLSocketFactory(sslFactory)
.setMaxConnPerRoute(maxConnectionPoolSizePerDestination)
.setMaxConnTotal(maxConnectionPoolSize)
.setDefaultSocketConfig(config)
.setDefaultRequestConfig(requestConfig)
.build();
}
private SSLConnectionSocketFactory getSSLConnectionFactory() {
if (disableSSLCertVerification) {
return getUnsecureSSLConnectionSocketFactory();
} else {
return getSecureSSLConnectionFactory();
}
}
private SSLConnectionSocketFactory getUnsecureSSLConnectionSocketFactory() {
TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) {
return true;
}
};
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
try {
this.sslContext = new SSLContextBuilder().loadTrustMaterial(trustStrategy).build();
} catch (Exception ex) {
throw new HecException("failed to create SSL connection factory", ex);
}
return new SSLConnectionSocketFactory(this.sslContext, hostnameVerifier);
}
private SSLConnectionSocketFactory getSecureSSLConnectionFactory() {
if (this.sslContext == null) {
return null; // use system default one
} else {
return new SSLConnectionSocketFactory(this.sslContext);
}
}
}