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