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