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  
28  package org.apache.hc.core5.http2.impl.nio.bootstrap;
29  
30  import java.util.concurrent.Future;
31  
32  import org.apache.hc.core5.annotation.Internal;
33  import org.apache.hc.core5.concurrent.CallbackContribution;
34  import org.apache.hc.core5.concurrent.FutureCallback;
35  import org.apache.hc.core5.function.Callback;
36  import org.apache.hc.core5.function.Decorator;
37  import org.apache.hc.core5.http.HttpHost;
38  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
39  import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
40  import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
41  import org.apache.hc.core5.http2.HttpVersionPolicy;
42  import org.apache.hc.core5.http2.ssl.ApplicationProtocol;
43  import org.apache.hc.core5.net.NamedEndpoint;
44  import org.apache.hc.core5.pool.ManagedConnPool;
45  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
46  import org.apache.hc.core5.reactor.IOReactorConfig;
47  import org.apache.hc.core5.reactor.IOSession;
48  import org.apache.hc.core5.reactor.IOSessionListener;
49  import org.apache.hc.core5.reactor.ProtocolIOSession;
50  import org.apache.hc.core5.reactor.ssl.TlsDetails;
51  import org.apache.hc.core5.util.Timeout;
52  
53  /**
54   * Client side message exchange initiator capable of negotiating
55   * HTTP/2 or HTTP/1.1 compatible connections.
56   *
57   * @since 5.0
58   */
59  public class H2AsyncRequester extends HttpAsyncRequester {
60  
61      private final HttpVersionPolicy versionPolicy;
62  
63      /**
64       * Use {@link H2RequesterBootstrap} to create instances of this class.
65       */
66      @Internal
67      public H2AsyncRequester(
68              final HttpVersionPolicy versionPolicy,
69              final IOReactorConfig ioReactorConfig,
70              final IOEventHandlerFactory eventHandlerFactory,
71              final Decorator<IOSession> ioSessionDecorator,
72              final Callback<Exception> exceptionCallback,
73              final IOSessionListener sessionListener,
74              final ManagedConnPool<HttpHost, IOSession> connPool) {
75          super(ioReactorConfig, eventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool);
76          this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
77      }
78  
79      /**
80       * Use {@link H2RequesterBootstrap} to create instances of this class.
81       *
82       * @since 5.2
83       */
84      @Internal
85      public H2AsyncRequester(
86              final HttpVersionPolicy versionPolicy,
87              final IOReactorConfig ioReactorConfig,
88              final IOEventHandlerFactory eventHandlerFactory,
89              final Decorator<IOSession> ioSessionDecorator,
90              final Callback<Exception> exceptionCallback,
91              final IOSessionListener sessionListener,
92              final ManagedConnPool<HttpHost, IOSession> connPool,
93              final TlsStrategy tlsStrategy,
94              final Timeout handshakeTimeout) {
95          super(ioReactorConfig, eventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool,
96                  tlsStrategy, handshakeTimeout);
97          this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
98      }
99  
100     @Override
101     protected Future<AsyncClientEndpoint> doConnect(
102             final HttpHost host,
103             final Timeout timeout,
104             final Object attachment,
105             final FutureCallback<AsyncClientEndpoint> callback) {
106         return super.doConnect(host, timeout, attachment != null ? attachment : versionPolicy, callback);
107     }
108 
109     @Override
110     protected void doTlsUpgrade(final ProtocolIOSession ioSession,
111                                 final NamedEndpoint endpoint,
112                                 final FutureCallback<ProtocolIOSession> callback) {
113         super.doTlsUpgrade(ioSession, endpoint, new CallbackContribution<ProtocolIOSession>(callback) {
114 
115             @Override
116             public void completed(final ProtocolIOSession protocolSession) {
117                 final boolean switchProtocol;
118                 switch (versionPolicy) {
119                     case FORCE_HTTP_2:
120                         switchProtocol = true;
121                         break;
122                     case NEGOTIATE:
123                         final TlsDetails tlsDetails = protocolSession.getTlsDetails();
124                         final String appProtocol = tlsDetails != null ? tlsDetails.getApplicationProtocol() : null;
125                         switchProtocol = ApplicationProtocol.HTTP_2.id.equals(appProtocol);
126                         break;
127                     default:
128                         switchProtocol = false;
129                 }
130                 if (switchProtocol) {
131                     protocolSession.switchProtocol(ApplicationProtocol.HTTP_2.id, callback);
132                 } else {
133                     if (callback != null) {
134                         callback.completed(protocolSession);
135                     }
136                 }
137             }
138 
139         });
140     }
141 
142 }