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.protocol.HttpClientContext;
41  import org.apache.hc.client5.http.routing.HttpRoutePlanner;
42  import org.apache.hc.core5.annotation.Contract;
43  import org.apache.hc.core5.annotation.Internal;
44  import org.apache.hc.core5.annotation.ThreadingBehavior;
45  import org.apache.hc.core5.http.HttpException;
46  import org.apache.hc.core5.http.HttpHost;
47  import org.apache.hc.core5.http.config.Lookup;
48  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
49  import org.apache.hc.core5.http.nio.HandlerFactory;
50  import org.apache.hc.core5.http2.nio.pool.H2ConnPool;
51  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Internal implementation of HTTP/2 only {@link CloseableHttpAsyncClient}.
57   * <p>
58   * Concurrent message exchanges with the same connection route executed by
59   * this client will get automatically multiplexed over a single physical HTTP/2
60   * connection.
61   * </p>
62   *
63   * @since 5.0
64   */
65  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
66  @Internal
67  public final class InternalH2AsyncClient extends InternalAbstractHttpAsyncClient {
68  
69      private static final Logger LOG = LoggerFactory.getLogger(InternalH2AsyncClient.class);
70      private final HttpRoutePlanner routePlanner;
71      private final H2ConnPool connPool;
72  
73      InternalH2AsyncClient(
74              final DefaultConnectingIOReactor ioReactor,
75              final AsyncExecChainElement execChain,
76              final AsyncPushConsumerRegistry pushConsumerRegistry,
77              final ThreadFactory threadFactory,
78              final H2ConnPool connPool,
79              final HttpRoutePlanner routePlanner,
80              final Lookup<CookieSpecFactory> cookieSpecRegistry,
81              final Lookup<AuthSchemeFactory> authSchemeRegistry,
82              final CookieStore cookieStore,
83              final CredentialsProvider credentialsProvider,
84              final RequestConfig defaultConfig,
85              final List<Closeable> closeables) {
86          super(ioReactor, pushConsumerRegistry, threadFactory, execChain,
87                  cookieSpecRegistry, authSchemeRegistry, cookieStore, credentialsProvider, defaultConfig, closeables);
88          this.connPool = connPool;
89          this.routePlanner = routePlanner;
90      }
91  
92      @Override
93      AsyncExecRuntime createAsyncExecRuntime(final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
94                                              final HttpRoute route) {
95          return new InternalH2AsyncExecRuntime(LOG, connPool, pushHandlerFactory);
96      }
97  
98      @Override
99      HttpRoute determineRoute(final HttpHost httpHost, final HttpClientContext clientContext) throws HttpException {
100         final HttpRoute route = routePlanner.determineRoute(httpHost, clientContext);
101         if (route.isTunnelled()) {
102             throw new HttpException("HTTP/2 tunneling not supported");
103         }
104         return route;
105     }
106 
107 }