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