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.async;
29
30 import org.apache.hc.client5.http.DnsResolver;
31 import org.apache.hc.client5.http.SchemePortResolver;
32 import org.apache.hc.client5.http.SystemDefaultDnsResolver;
33 import org.apache.hc.client5.http.config.TlsConfig;
34 import org.apache.hc.client5.http.impl.DefaultClientConnectionReuseStrategy;
35 import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
36 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
37 import org.apache.hc.client5.http.nio.AsyncClientConnectionManager;
38 import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
39 import org.apache.hc.core5.concurrent.DefaultThreadFactory;
40 import org.apache.hc.core5.http.config.CharCodingConfig;
41 import org.apache.hc.core5.http.config.Http1Config;
42 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
43 import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
44 import org.apache.hc.core5.http.protocol.HttpProcessor;
45 import org.apache.hc.core5.http.protocol.RequestUserAgent;
46 import org.apache.hc.core5.http2.HttpVersionPolicy;
47 import org.apache.hc.core5.http2.config.H2Config;
48 import org.apache.hc.core5.http2.protocol.H2RequestConnControl;
49 import org.apache.hc.core5.http2.protocol.H2RequestContent;
50 import org.apache.hc.core5.http2.protocol.H2RequestTargetHost;
51 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
52 import org.apache.hc.core5.reactor.IOReactorConfig;
53 import org.apache.hc.core5.util.VersionInfo;
54
55
56
57
58
59
60 public final class HttpAsyncClients {
61
62 private HttpAsyncClients() {
63 super();
64 }
65
66
67
68
69
70 public static HttpAsyncClientBuilder custom() {
71 return HttpAsyncClientBuilder.create();
72 }
73
74
75
76
77 public static CloseableHttpAsyncClient createDefault() {
78 return HttpAsyncClientBuilder.create().build();
79 }
80
81
82
83
84
85 public static CloseableHttpAsyncClient createSystem() {
86 return HttpAsyncClientBuilder.create().useSystemProperties().build();
87 }
88
89
90
91
92
93
94 public static H2AsyncClientBuilder customHttp2() {
95 return H2AsyncClientBuilder.create();
96 }
97
98
99
100
101
102 public static CloseableHttpAsyncClient createHttp2Default() {
103 return H2AsyncClientBuilder.create().build();
104 }
105
106
107
108
109
110 public static CloseableHttpAsyncClient createHttp2System() {
111 return H2AsyncClientBuilder.create().useSystemProperties().build();
112 }
113
114 private static HttpProcessor createMinimalProtocolProcessor() {
115 return new DefaultHttpProcessor(
116 new H2RequestTargetHost(),
117 new H2RequestContent(),
118 new H2RequestConnControl(),
119 new RequestUserAgent(VersionInfo.getSoftwareInfo(
120 "Apache-HttpAsyncClient", "org.apache.hc.client5", HttpAsyncClients.class)));
121 }
122
123 private static MinimalHttpAsyncClient createMinimalHttpAsyncClientImpl(
124 final IOEventHandlerFactory eventHandlerFactory,
125 final AsyncPushConsumerRegistry pushConsumerRegistry,
126 final IOReactorConfig ioReactorConfig,
127 final AsyncClientConnectionManager connmgr,
128 final SchemePortResolver schemePortResolver,
129 final TlsConfig tlsConfig) {
130 return new MinimalHttpAsyncClient(
131 eventHandlerFactory,
132 pushConsumerRegistry,
133 ioReactorConfig,
134 new DefaultThreadFactory("httpclient-main", true),
135 new DefaultThreadFactory("httpclient-dispatch", true),
136 connmgr,
137 schemePortResolver,
138 tlsConfig);
139 }
140
141
142
143
144
145
146
147
148 @Deprecated
149 public static MinimalHttpAsyncClient createMinimal(
150 final HttpVersionPolicy versionPolicy,
151 final H2Config h2Config,
152 final Http1Config h1Config,
153 final IOReactorConfig ioReactorConfig,
154 final AsyncClientConnectionManager connmgr) {
155 final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry();
156 return createMinimalHttpAsyncClientImpl(
157 new HttpAsyncClientProtocolNegotiationStarter(
158 createMinimalProtocolProcessor(),
159 (request, context) -> pushConsumerRegistry.get(request),
160 h2Config,
161 h1Config,
162 CharCodingConfig.DEFAULT,
163 DefaultClientConnectionReuseStrategy.INSTANCE),
164 pushConsumerRegistry,
165 ioReactorConfig,
166 connmgr,
167 DefaultSchemePortResolver.INSTANCE,
168 versionPolicy != null ? TlsConfig.custom().setVersionPolicy(versionPolicy).build() : null);
169 }
170
171
172
173
174
175
176
177
178 public static MinimalHttpAsyncClient createMinimal(
179 final H2Config h2Config,
180 final Http1Config h1Config,
181 final IOReactorConfig ioReactorConfig,
182 final AsyncClientConnectionManager connmgr) {
183 final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry();
184 return createMinimalHttpAsyncClientImpl(
185 new HttpAsyncClientProtocolNegotiationStarter(
186 createMinimalProtocolProcessor(),
187 (request, context) -> pushConsumerRegistry.get(request),
188 h2Config,
189 h1Config,
190 CharCodingConfig.DEFAULT,
191 DefaultClientConnectionReuseStrategy.INSTANCE),
192 pushConsumerRegistry,
193 ioReactorConfig,
194 connmgr,
195 DefaultSchemePortResolver.INSTANCE,
196 null);
197 }
198
199
200
201
202
203
204
205
206 @Deprecated
207 public static MinimalHttpAsyncClient createMinimal(
208 final HttpVersionPolicy versionPolicy,
209 final H2Config h2Config,
210 final Http1Config h1Config,
211 final IOReactorConfig ioReactorConfig) {
212 return createMinimal(versionPolicy, h2Config, h1Config, ioReactorConfig,
213 PoolingAsyncClientConnectionManagerBuilder.create().build());
214 }
215
216
217
218
219
220
221 public static MinimalHttpAsyncClient createMinimal(
222 final H2Config h2Config,
223 final Http1Config h1Config,
224 final IOReactorConfig ioReactorConfig) {
225 return createMinimal(h2Config, h1Config, ioReactorConfig,
226 PoolingAsyncClientConnectionManagerBuilder.create().build());
227 }
228
229
230
231
232
233
234 public static MinimalHttpAsyncClient createMinimal(final H2Config h2Config, final Http1Config h1Config) {
235 return createMinimal(HttpVersionPolicy.NEGOTIATE, h2Config, h1Config, IOReactorConfig.DEFAULT);
236 }
237
238
239
240
241
242
243 public static MinimalHttpAsyncClient createMinimal() {
244 return createMinimal(H2Config.DEFAULT, Http1Config.DEFAULT);
245 }
246
247
248
249
250
251
252 public static MinimalHttpAsyncClient createMinimal(final AsyncClientConnectionManager connManager) {
253 return createMinimal(
254 HttpVersionPolicy.NEGOTIATE,
255 H2Config.DEFAULT,
256 Http1Config.DEFAULT,
257 IOReactorConfig.DEFAULT,
258 connManager);
259 }
260
261 private static MinimalH2AsyncClient createMinimalHttp2AsyncClientImpl(
262 final IOEventHandlerFactory eventHandlerFactory,
263 final AsyncPushConsumerRegistry pushConsumerRegistry,
264 final IOReactorConfig ioReactorConfig,
265 final DnsResolver dnsResolver,
266 final TlsStrategy tlsStrategy) {
267 return new MinimalH2AsyncClient(
268 eventHandlerFactory,
269 pushConsumerRegistry,
270 ioReactorConfig,
271 new DefaultThreadFactory("httpclient-main", true),
272 new DefaultThreadFactory("httpclient-dispatch", true),
273 dnsResolver,
274 tlsStrategy);
275 }
276
277
278
279
280
281 public static MinimalH2AsyncClient createHttp2Minimal(
282 final H2Config h2Config,
283 final IOReactorConfig ioReactorConfig,
284 final DnsResolver dnsResolver,
285 final TlsStrategy tlsStrategy) {
286 final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry();
287 return createMinimalHttp2AsyncClientImpl(
288 new H2AsyncClientProtocolStarter(
289 createMinimalProtocolProcessor(),
290 (request, context) -> pushConsumerRegistry.get(request),
291 h2Config,
292 CharCodingConfig.DEFAULT),
293 pushConsumerRegistry,
294 ioReactorConfig,
295 dnsResolver,
296 tlsStrategy);
297 }
298
299
300
301
302
303 public static MinimalH2AsyncClient createHttp2Minimal(
304 final H2Config h2Config,
305 final IOReactorConfig ioReactorConfig,
306 final TlsStrategy tlsStrategy) {
307 return createHttp2Minimal(h2Config, ioReactorConfig, SystemDefaultDnsResolver.INSTANCE, tlsStrategy);
308 }
309
310
311
312
313
314 public static MinimalH2AsyncClient createHttp2Minimal(
315 final H2Config h2Config,
316 final IOReactorConfig ioReactorConfig) {
317 return createHttp2Minimal(h2Config, ioReactorConfig, DefaultClientTlsStrategy.getDefault());
318 }
319
320
321
322
323
324 public static MinimalH2AsyncClient createHttp2Minimal(final H2Config h2Config) {
325 return createHttp2Minimal(h2Config, IOReactorConfig.DEFAULT);
326 }
327
328
329
330
331
332 public static MinimalH2AsyncClient createHttp2Minimal() {
333 return createHttp2Minimal(H2Config.DEFAULT);
334 }
335
336 }