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