View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Factory methods for {@link CloseableHttpAsyncClient} instances.
57   *
58   * @since 5.0
59   */
60  public final class HttpAsyncClients {
61  
62      private HttpAsyncClients() {
63          super();
64      }
65  
66      /**
67       * Creates builder object for construction of custom
68       * {@link CloseableHttpAsyncClient} instances.
69       */
70      public static HttpAsyncClientBuilder custom() {
71          return HttpAsyncClientBuilder.create();
72      }
73  
74      /**
75       * Creates {@link CloseableHttpAsyncClient} instance with default configuration.
76       */
77      public static CloseableHttpAsyncClient createDefault() {
78          return HttpAsyncClientBuilder.create().build();
79      }
80  
81      /**
82       * Creates {@link CloseableHttpAsyncClient} instance with default
83       * configuration and system properties.
84       */
85      public static CloseableHttpAsyncClient createSystem() {
86          return HttpAsyncClientBuilder.create().useSystemProperties().build();
87      }
88  
89      /**
90       * Creates builder object for construction of custom HTTP/2
91       * {@link CloseableHttpAsyncClient} instances optimized for HTTP/2 protocol
92       * and message multiplexing
93       */
94      public static H2AsyncClientBuilder customHttp2() {
95          return H2AsyncClientBuilder.create();
96      }
97  
98      /**
99       * Creates HTTP/2 {@link CloseableHttpAsyncClient} instance with default configuration
100      * optimized for HTTP/2 protocol and message multiplexing.
101      */
102     public static CloseableHttpAsyncClient createHttp2Default() {
103         return H2AsyncClientBuilder.create().build();
104     }
105 
106     /**
107      * Creates HTTP/2 {@link CloseableHttpAsyncClient} instance with default configuration and
108      * system properties optimized for HTTP/2 protocol and message multiplexing.
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 H2RequestContent(),
117                 new H2RequestTargetHost(),
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
143      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
144      * functionality.
145      *
146      * @deprecated Use {@link #createMinimal(H2Config, Http1Config, IOReactorConfig, AsyncClientConnectionManager)}
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
173      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
174      * functionality.
175      *
176      * @since 5.2
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
201      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
202      * functionality.
203      *
204      * @deprecated Use {@link #createMinimal(H2Config, Http1Config, IOReactorConfig)}
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
218      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
219      * functionality.
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
231      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
232      * functionality.
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      * Creates {@link MinimalHttpAsyncClient} instance optimized for
240      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
241      * functionality.
242      */
243     public static MinimalHttpAsyncClient createMinimal() {
244         return createMinimal(H2Config.DEFAULT, Http1Config.DEFAULT);
245     }
246 
247     /**
248      * Creates {@link MinimalHttpAsyncClient} instance optimized for
249      * HTTP/1.1 and HTTP/2 message transport without advanced HTTP protocol
250      * functionality.
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      * Creates {@link MinimalH2AsyncClient} instance optimized for HTTP/2 multiplexing message
279      * transport without advanced HTTP protocol functionality.
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      * Creates {@link MinimalH2AsyncClient} instance optimized for HTTP/2 multiplexing message
301      * transport without advanced HTTP protocol functionality.
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      * Creates {@link MinimalH2AsyncClient} instance optimized for HTTP/2 multiplexing message
312      * transport without advanced HTTP protocol functionality.
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      * Creates {@link MinimalH2AsyncClient} instance optimized for HTTP/2 multiplexing message
322      * transport without advanced HTTP protocol functionality.
323      */
324     public static MinimalH2AsyncClient createHttp2Minimal(final H2Config h2Config) {
325         return createHttp2Minimal(h2Config, IOReactorConfig.DEFAULT);
326     }
327 
328     /**
329      * Creates {@link MinimalH2AsyncClient} instance optimized for HTTP/2 multiplexing message
330      * transport without advanced HTTP protocol functionality.
331      */
332     public static MinimalH2AsyncClient createHttp2Minimal() {
333         return createHttp2Minimal(H2Config.DEFAULT);
334     }
335 
336 }