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