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