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.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.ConnectionReuseStrategy;
35  import org.apache.hc.core5.http.ContentLengthStrategy;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.URIScheme;
39  import org.apache.hc.core5.http.config.CharCodingConfig;
40  import org.apache.hc.core5.http.config.Http1Config;
41  import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
42  import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
43  import org.apache.hc.core5.http.impl.Http1StreamListener;
44  import org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory;
45  import org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory;
46  import org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandler;
47  import org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexer;
48  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
49  import org.apache.hc.core5.http.nio.HandlerFactory;
50  import org.apache.hc.core5.http.nio.NHttpMessageParser;
51  import org.apache.hc.core5.http.nio.NHttpMessageParserFactory;
52  import org.apache.hc.core5.http.nio.NHttpMessageWriter;
53  import org.apache.hc.core5.http.nio.NHttpMessageWriterFactory;
54  import org.apache.hc.core5.http.protocol.HttpProcessor;
55  import org.apache.hc.core5.reactor.IOEventHandler;
56  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
57  import org.apache.hc.core5.reactor.ProtocolIOSession;
58  import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
59  import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
60  import org.apache.hc.core5.util.Args;
61  
62  /**
63   * @since 5.0
64   */
65  @Contract(threading = ThreadingBehavior.IMMUTABLE)
66  class InternalServerHttp1EventHandlerFactory implements IOEventHandlerFactory {
67  
68      private final HttpProcessor httpProcessor;
69      private final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
70      private final Http1Config http1Config;
71      private final CharCodingConfig charCodingConfig;
72      private final ConnectionReuseStrategy connectionReuseStrategy;
73      private final SSLContext sslContext;
74      private final SSLSessionInitializer sslSessionInitializer;
75      private final SSLSessionVerifier sslSessionVerifier;
76      private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
77      private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
78  
79      InternalServerHttp1EventHandlerFactory(
80              final HttpProcessor httpProcessor,
81              final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
82              final Http1Config http1Config,
83              final CharCodingConfig charCodingConfig,
84              final ConnectionReuseStrategy connectionReuseStrategy,
85              final SSLContext sslContext,
86              final SSLSessionInitializer sslSessionInitializer,
87              final SSLSessionVerifier sslSessionVerifier) {
88          this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
89          this.exchangeHandlerFactory = Args.notNull(exchangeHandlerFactory, "Exchange handler factory");
90          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
91          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
92          this.connectionReuseStrategy = connectionReuseStrategy != null ? connectionReuseStrategy :
93                  DefaultConnectionReuseStrategy.INSTANCE;
94          this.sslContext = sslContext;
95          this.sslSessionInitializer = sslSessionInitializer;
96          this.sslSessionVerifier = sslSessionVerifier;
97          this.requestParserFactory = new DefaultHttpRequestParserFactory(this.http1Config);
98          this.responseWriterFactory = DefaultHttpResponseWriterFactory.INSTANCE;
99      }
100 
101     protected ServerHttp1StreamDuplexer createServerHttp1StreamDuplexer(
102             final ProtocolIOSession ioSession,
103             final HttpProcessor httpProcessor,
104             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
105             final Http1Config http1Config,
106             final CharCodingConfig charCodingConfig,
107             final ConnectionReuseStrategy connectionReuseStrategy,
108             final NHttpMessageParser<HttpRequest> incomingMessageParser,
109             final NHttpMessageWriter<HttpResponse> outgoingMessageWriter,
110             final ContentLengthStrategy incomingContentStrategy,
111             final ContentLengthStrategy outgoingContentStrategy,
112             final Http1StreamListener streamListener) {
113         return new ServerHttp1StreamDuplexer(ioSession, httpProcessor, exchangeHandlerFactory,
114                 sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id, http1Config,
115                 charCodingConfig, connectionReuseStrategy, incomingMessageParser, outgoingMessageWriter,
116                 incomingContentStrategy, outgoingContentStrategy, streamListener);
117     }
118 
119     @Override
120     public IOEventHandler createHandler(final ProtocolIOSession ioSession, final Object attachment) {
121         if (sslContext != null) {
122             ioSession.startTls(sslContext, null, null ,sslSessionInitializer, sslSessionVerifier, null);
123         }
124         final ServerHttp1StreamDuplexer streamDuplexer = createServerHttp1StreamDuplexer(
125                 ioSession,
126                 httpProcessor,
127                 exchangeHandlerFactory,
128                 http1Config,
129                 charCodingConfig,
130                 connectionReuseStrategy,
131                 requestParserFactory.create(),
132                 responseWriterFactory.create(),
133                 DefaultContentLengthStrategy.INSTANCE,
134                 DefaultContentLengthStrategy.INSTANCE,
135                 LoggingHttp1StreamListener.INSTANCE_SERVER);
136         return new ServerHttp1IOEventHandler(streamDuplexer);
137     }
138 
139 }