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