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