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