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.testing.nio;
29  
30  import javax.net.ssl.SSLContext;
31  
32  import org.apache.hc.core5.http.config.CharCodingConfig;
33  import org.apache.hc.core5.http.config.Http1Config;
34  import org.apache.hc.core5.http.impl.HttpProcessors;
35  import org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler;
36  import org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory;
37  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
38  import org.apache.hc.core5.http.nio.HandlerFactory;
39  import org.apache.hc.core5.http.protocol.HttpProcessor;
40  import org.apache.hc.core5.http2.HttpVersionPolicy;
41  import org.apache.hc.core5.http2.config.H2Config;
42  import org.apache.hc.core5.http2.impl.H2Processors;
43  import org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler;
44  import org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory;
45  import org.apache.hc.core5.http2.impl.nio.ClientH2UpgradeHandler;
46  import org.apache.hc.core5.http2.impl.nio.ClientHttp1UpgradeHandler;
47  import org.apache.hc.core5.http2.impl.nio.HttpProtocolNegotiator;
48  import org.apache.hc.core5.http2.ssl.ApplicationProtocol;
49  import org.apache.hc.core5.reactor.IOEventHandler;
50  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
51  import org.apache.hc.core5.reactor.ProtocolIOSession;
52  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
53  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
54  import org.apache.hc.core5.util.Args;
55  
56  class InternalClientProtocolNegotiationStarter implements IOEventHandlerFactory {
57  
58      private final HttpProcessor httpProcessor;
59      private final HandlerFactory<AsyncPushConsumer> exchangeHandlerFactory;
60      private final HttpVersionPolicy versionPolicy;
61      private final H2Config h2Config;
62      private final Http1Config http1Config;
63      private final CharCodingConfig charCodingConfig;
64      private final SSLContext sslContext;
65      private final SSLSessionInitializer sslSessionInitializer;
66      private final SSLSessionVerifier sslSessionVerifier;
67  
68      InternalClientProtocolNegotiationStarter(
69              final HttpProcessor httpProcessor,
70              final HandlerFactory<AsyncPushConsumer> exchangeHandlerFactory,
71              final HttpVersionPolicy versionPolicy,
72              final H2Config h2Config,
73              final Http1Config http1Config,
74              final CharCodingConfig charCodingConfig,
75              final SSLContext sslContext,
76              final SSLSessionInitializer sslSessionInitializer,
77              final SSLSessionVerifier sslSessionVerifier) {
78          this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
79          this.exchangeHandlerFactory = exchangeHandlerFactory;
80          this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
81          this.h2Config = h2Config != null ? h2Config : H2Config.DEFAULT;
82          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
83          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
84          this.sslContext = sslContext;
85          this.sslSessionInitializer = sslSessionInitializer;
86          this.sslSessionVerifier = sslSessionVerifier;
87      }
88  
89      @Override
90      public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
91          if (sslContext != null) {
92              ioSession.startTls(sslContext, null, null, sslSessionInitializer, sslSessionVerifier, null);
93          }
94          final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(
95                  httpProcessor != null ? httpProcessor : HttpProcessors.client(),
96                  http1Config,
97                  charCodingConfig,
98                  LoggingHttp1StreamListener.INSTANCE_CLIENT);
99          final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(
100                 httpProcessor != null ? httpProcessor : H2Processors.client(),
101                 exchangeHandlerFactory,
102                 h2Config,
103                 charCodingConfig,
104                 LoggingH2StreamListener.INSTANCE);
105         ioSession.registerProtocol(ApplicationProtocol.HTTP_1_1.id, new ClientHttp1UpgradeHandler(http1StreamHandlerFactory));
106         ioSession.registerProtocol(ApplicationProtocol.HTTP_2.id, new ClientH2UpgradeHandler(http2StreamHandlerFactory));
107 
108         switch (versionPolicy) {
109             case FORCE_HTTP_2:
110                 return new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, false);
111             case FORCE_HTTP_1:
112                 return new ClientHttp1IOEventHandler(http1StreamHandlerFactory.create(ioSession));
113             default:
114                 return new HttpProtocolNegotiator(ioSession, null);
115         }
116    }
117 
118 }