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