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.http.impl.nio;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.http.ConnectionReuseStrategy;
33  import org.apache.hc.core5.http.ContentLengthStrategy;
34  import org.apache.hc.core5.http.HttpRequest;
35  import org.apache.hc.core5.http.HttpResponse;
36  import org.apache.hc.core5.http.config.CharCodingConfig;
37  import org.apache.hc.core5.http.config.H1Config;
38  import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
39  import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
40  import org.apache.hc.core5.http.impl.Http1StreamListener;
41  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
42  import org.apache.hc.core5.http.nio.HandlerFactory;
43  import org.apache.hc.core5.http.nio.NHttpMessageParserFactory;
44  import org.apache.hc.core5.http.nio.NHttpMessageWriterFactory;
45  import org.apache.hc.core5.http.protocol.HttpProcessor;
46  import org.apache.hc.core5.reactor.TlsCapableIOSession;
47  import org.apache.hc.core5.util.Args;
48  
49  /**
50   * @since 5.0
51   */
52  @Contract(threading = ThreadingBehavior.IMMUTABLE)
53  public final class ServerHttp1StreamDuplexerFactory {
54  
55      private final HttpProcessor httpProcessor;
56      private final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
57      private final ConnectionReuseStrategy connectionReuseStrategy;
58      private final H1Config h1Config;
59      private final CharCodingConfig charCodingConfig;
60      private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
61      private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
62      private final ContentLengthStrategy incomingContentStrategy;
63      private final ContentLengthStrategy outgoingContentStrategy;
64      private final Http1StreamListener streamListener;
65  
66      public ServerHttp1StreamDuplexerFactory(
67              final HttpProcessor httpProcessor,
68              final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
69              final H1Config h1Config,
70              final CharCodingConfig charCodingConfig,
71              final ConnectionReuseStrategy connectionReuseStrategy,
72              final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
73              final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
74              final ContentLengthStrategy incomingContentStrategy,
75              final ContentLengthStrategy outgoingContentStrategy,
76              final Http1StreamListener streamListener) {
77          this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
78          this.exchangeHandlerFactory = Args.notNull(exchangeHandlerFactory, "Exchange handler factory");
79          this.h1Config = h1Config != null ? h1Config : H1Config.DEFAULT;
80          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
81          this.connectionReuseStrategy = connectionReuseStrategy != null ? connectionReuseStrategy :
82                  DefaultConnectionReuseStrategy.INSTANCE;
83          this.requestParserFactory = requestParserFactory != null ? requestParserFactory :
84                  new DefaultHttpRequestParserFactory(h1Config);
85          this.responseWriterFactory = responseWriterFactory != null ? responseWriterFactory :
86                  DefaultHttpResponseWriterFactory.INSTANCE;
87          this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy :
88                  DefaultContentLengthStrategy.INSTANCE;
89          this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
90                  DefaultContentLengthStrategy.INSTANCE;
91          this.streamListener = streamListener;
92      }
93  
94      public ServerHttp1StreamDuplexerFactory(
95              final HttpProcessor httpProcessor,
96              final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
97              final H1Config h1Config,
98              final CharCodingConfig charCodingConfig,
99              final ConnectionReuseStrategy connectionReuseStrategy,
100             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
101             final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
102             final Http1StreamListener streamListener) {
103         this(httpProcessor, exchangeHandlerFactory, h1Config, charCodingConfig,
104                 connectionReuseStrategy, requestParserFactory, responseWriterFactory,
105                 null, null, streamListener);
106     }
107 
108     public ServerHttp1StreamDuplexerFactory(
109             final HttpProcessor httpProcessor,
110             final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
111             final H1Config h1Config,
112             final CharCodingConfig charCodingConfig,
113             final Http1StreamListener streamListener) {
114         this(httpProcessor, exchangeHandlerFactory, h1Config, charCodingConfig, null, null ,null, streamListener);
115     }
116 
117     public ServerHttp1StreamDuplexer create(final String scheme, final TlsCapableIOSession ioSession) {
118         return new ServerHttp1StreamDuplexer(ioSession, httpProcessor, exchangeHandlerFactory,
119                 scheme,
120                 h1Config,
121                 charCodingConfig,
122                 connectionReuseStrategy,
123                 requestParserFactory.create(),
124                 responseWriterFactory.create(),
125                 incomingContentStrategy,
126                 outgoingContentStrategy,
127                 streamListener);
128     }
129 
130 }