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