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