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  package org.apache.hc.client5.http.impl.async;
28  
29  import java.io.Closeable;
30  import java.util.List;
31  import java.util.concurrent.ThreadFactory;
32  import java.util.function.Function;
33  
34  import org.apache.hc.client5.http.HttpRoute;
35  import org.apache.hc.client5.http.async.AsyncExecRuntime;
36  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
37  import org.apache.hc.client5.http.auth.CredentialsProvider;
38  import org.apache.hc.client5.http.config.RequestConfig;
39  import org.apache.hc.client5.http.config.TlsConfig;
40  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
41  import org.apache.hc.client5.http.cookie.CookieStore;
42  import org.apache.hc.client5.http.nio.AsyncClientConnectionManager;
43  import org.apache.hc.client5.http.protocol.HttpClientContext;
44  import org.apache.hc.client5.http.routing.HttpRoutePlanner;
45  import org.apache.hc.core5.annotation.Contract;
46  import org.apache.hc.core5.annotation.Internal;
47  import org.apache.hc.core5.annotation.ThreadingBehavior;
48  import org.apache.hc.core5.http.HttpException;
49  import org.apache.hc.core5.http.HttpHost;
50  import org.apache.hc.core5.http.HttpRequest;
51  import org.apache.hc.core5.http.config.Lookup;
52  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
53  import org.apache.hc.core5.http.nio.HandlerFactory;
54  import org.apache.hc.core5.http.protocol.HttpContext;
55  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Internal implementation of {@link CloseableHttpAsyncClient} that can negotiate
61   * the most optimal HTTP protocol version during during the {@code TLS} handshake
62   * with {@code ALPN} extension if supported by the Java runtime.
63   * <p>
64   * Concurrent message exchanges executed by this client will get assigned to
65   * separate connections leased from the connection pool.
66   * </p>
67   *
68   * @since 5.0
69   */
70  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
71  @Internal
72  public final class InternalHttpAsyncClient extends InternalAbstractHttpAsyncClient {
73  
74      private static final Logger LOG = LoggerFactory.getLogger(InternalHttpAsyncClient.class);
75      private final AsyncClientConnectionManager manager;
76      private final HttpRoutePlanner routePlanner;
77      private final TlsConfig tlsConfig;
78  
79      InternalHttpAsyncClient(
80              final DefaultConnectingIOReactor ioReactor,
81              final AsyncExecChainElement execChain,
82              final AsyncPushConsumerRegistry pushConsumerRegistry,
83              final ThreadFactory threadFactory,
84              final AsyncClientConnectionManager manager,
85              final HttpRoutePlanner routePlanner,
86              final TlsConfig tlsConfig,
87              final Lookup<CookieSpecFactory> cookieSpecRegistry,
88              final Lookup<AuthSchemeFactory> authSchemeRegistry,
89              final CookieStore cookieStore,
90              final CredentialsProvider credentialsProvider,
91              final Function<HttpContext, HttpClientContext> contextAdaptor,
92              final RequestConfig defaultConfig,
93              final List<Closeable> closeables) {
94          super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
95                  cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, contextAdaptor,
96                  defaultConfig, closeables);
97          this.manager = manager;
98          this.routePlanner = routePlanner;
99          this.tlsConfig = tlsConfig;
100     }
101 
102     @Override
103     AsyncExecRuntime createAsyncExecRuntime(final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) {
104         return new InternalHttpAsyncExecRuntime(LOG, manager, getConnectionInitiator(), pushHandlerFactory, tlsConfig);
105     }
106 
107     @Override
108     HttpRoute determineRoute(final HttpHost httpHost, final HttpRequest request, final HttpClientContext clientContext) throws HttpException {
109         return routePlanner.determineRoute(httpHost, request, clientContext);
110     }
111 
112 }