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 package org.apache.hc.client5.http.impl.nio;
29
30 import org.apache.hc.client5.http.DnsResolver;
31 import org.apache.hc.client5.http.HttpRoute;
32 import org.apache.hc.client5.http.SchemePortResolver;
33 import org.apache.hc.client5.http.config.ConnectionConfig;
34 import org.apache.hc.client5.http.config.TlsConfig;
35 import org.apache.hc.client5.http.ssl.ConscryptClientTlsStrategy;
36 import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
37 import org.apache.hc.core5.function.Resolver;
38 import org.apache.hc.core5.http.HttpHost;
39 import org.apache.hc.core5.http.URIScheme;
40 import org.apache.hc.core5.http.config.RegistryBuilder;
41 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
42 import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
43 import org.apache.hc.core5.pool.PoolReusePolicy;
44 import org.apache.hc.core5.util.ReflectionUtils;
45 import org.apache.hc.core5.util.TimeValue;
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 public class PoolingAsyncClientConnectionManagerBuilder {
74
75 private TlsStrategy tlsStrategy;
76 private SchemePortResolver schemePortResolver;
77 private DnsResolver dnsResolver;
78 private PoolConcurrencyPolicy poolConcurrencyPolicy;
79 private PoolReusePolicy poolReusePolicy;
80
81 private boolean systemProperties;
82
83 private int maxConnTotal;
84 private int maxConnPerRoute;
85
86 private Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver;
87 private Resolver<HttpHost, TlsConfig> tlsConfigResolver;
88
89 public static PoolingAsyncClientConnectionManagerBuilder create() {
90 return new PoolingAsyncClientConnectionManagerBuilder();
91 }
92
93 PoolingAsyncClientConnectionManagerBuilder() {
94 super();
95 }
96
97
98
99
100 public final PoolingAsyncClientConnectionManagerBuilder setTlsStrategy(
101 final TlsStrategy tlsStrategy) {
102 this.tlsStrategy = tlsStrategy;
103 return this;
104 }
105
106
107
108
109 public final PoolingAsyncClientConnectionManagerBuilder setDnsResolver(final DnsResolver dnsResolver) {
110 this.dnsResolver = dnsResolver;
111 return this;
112 }
113
114
115
116
117 public final PoolingAsyncClientConnectionManagerBuilder setSchemePortResolver(final SchemePortResolver schemePortResolver) {
118 this.schemePortResolver = schemePortResolver;
119 return this;
120 }
121
122
123
124
125 public final PoolingAsyncClientConnectionManagerBuilder setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
126 this.poolConcurrencyPolicy = poolConcurrencyPolicy;
127 return this;
128 }
129
130
131
132
133 public final PoolingAsyncClientConnectionManagerBuilder setConnPoolPolicy(final PoolReusePolicy poolReusePolicy) {
134 this.poolReusePolicy = poolReusePolicy;
135 return this;
136 }
137
138
139
140
141 public final PoolingAsyncClientConnectionManagerBuilder setMaxConnTotal(final int maxConnTotal) {
142 this.maxConnTotal = maxConnTotal;
143 return this;
144 }
145
146
147
148
149 public final PoolingAsyncClientConnectionManagerBuilder setMaxConnPerRoute(final int maxConnPerRoute) {
150 this.maxConnPerRoute = maxConnPerRoute;
151 return this;
152 }
153
154
155
156
157
158
159 public final PoolingAsyncClientConnectionManagerBuilder setDefaultConnectionConfig(final ConnectionConfig config) {
160 this.connectionConfigResolver = (route) -> config;
161 return this;
162 }
163
164
165
166
167
168
169 public final PoolingAsyncClientConnectionManagerBuilder setConnectionConfigResolver(
170 final Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver) {
171 this.connectionConfigResolver = connectionConfigResolver;
172 return this;
173 }
174
175
176
177
178
179
180 public final PoolingAsyncClientConnectionManagerBuilder setDefaultTlsConfig(final TlsConfig config) {
181 this.tlsConfigResolver = (host) -> config;
182 return this;
183 }
184
185
186
187
188
189
190 public final PoolingAsyncClientConnectionManagerBuilder setTlsConfigResolver(
191 final Resolver<HttpHost, TlsConfig> tlsConfigResolver) {
192 this.tlsConfigResolver = tlsConfigResolver;
193 return this;
194 }
195
196
197
198
199
200
201 @Deprecated
202 public final PoolingAsyncClientConnectionManagerBuilder setConnectionTimeToLive(final TimeValue timeToLive) {
203 setDefaultConnectionConfig(ConnectionConfig.custom()
204 .setTimeToLive(timeToLive)
205 .build());
206 return this;
207 }
208
209
210
211
212
213
214
215 @Deprecated
216 public final PoolingAsyncClientConnectionManagerBuilder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
217 setDefaultConnectionConfig(ConnectionConfig.custom()
218 .setValidateAfterInactivity(validateAfterInactivity)
219 .build());
220 return this;
221 }
222
223
224
225
226
227 public final PoolingAsyncClientConnectionManagerBuilder useSystemProperties() {
228 this.systemProperties = true;
229 return this;
230 }
231
232 public PoolingAsyncClientConnectionManager build() {
233 final TlsStrategy tlsStrategyCopy;
234 if (tlsStrategy != null) {
235 tlsStrategyCopy = tlsStrategy;
236 } else {
237 if (ReflectionUtils.determineJRELevel() <= 8 && ConscryptClientTlsStrategy.isSupported()) {
238 if (systemProperties) {
239 tlsStrategyCopy = ConscryptClientTlsStrategy.getSystemDefault();
240 } else {
241 tlsStrategyCopy = ConscryptClientTlsStrategy.getDefault();
242 }
243 } else {
244 if (systemProperties) {
245 tlsStrategyCopy = DefaultClientTlsStrategy.createSystemDefault();
246 } else {
247 tlsStrategyCopy = DefaultClientTlsStrategy.createDefault();
248 }
249 }
250 }
251 final PoolingAsyncClientConnectionManager poolingmgr = new PoolingAsyncClientConnectionManager(
252 RegistryBuilder.<TlsStrategy>create()
253 .register(URIScheme.HTTPS.getId(), tlsStrategyCopy)
254 .build(),
255 poolConcurrencyPolicy,
256 poolReusePolicy,
257 null,
258 schemePortResolver,
259 dnsResolver);
260 poolingmgr.setConnectionConfigResolver(connectionConfigResolver);
261 poolingmgr.setTlsConfigResolver(tlsConfigResolver);
262 if (maxConnTotal > 0) {
263 poolingmgr.setMaxTotal(maxConnTotal);
264 }
265 if (maxConnPerRoute > 0) {
266 poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
267 }
268 return poolingmgr;
269 }
270
271 }