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.io;
29  
30  import java.io.IOException;
31  import java.net.Socket;
32  
33  import javax.net.ssl.SSLSocket;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.http.ClassicHttpRequest;
38  import org.apache.hc.core5.http.ClassicHttpResponse;
39  import org.apache.hc.core5.http.ContentLengthStrategy;
40  import org.apache.hc.core5.http.config.CharCodingConfig;
41  import org.apache.hc.core5.http.config.Http1Config;
42  import org.apache.hc.core5.http.impl.CharCodingSupport;
43  import org.apache.hc.core5.http.io.HttpConnectionFactory;
44  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
45  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
46  import org.apache.hc.core5.http.io.ResponseOutOfOrderStrategy;
47  
48  /**
49   * Default factory for {@link org.apache.hc.core5.http.io.HttpClientConnection}s.
50   *
51   * @since 4.3
52   */
53  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54  public class DefaultBHttpClientConnectionFactory
55          implements HttpConnectionFactory<DefaultBHttpClientConnection> {
56  
57      private final Http1Config http1Config;
58      private final CharCodingConfig charCodingConfig;
59      private final ContentLengthStrategy incomingContentStrategy;
60      private final ContentLengthStrategy outgoingContentStrategy;
61      private final ResponseOutOfOrderStrategy responseOutOfOrderStrategy;
62      private final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
63      private final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
64  
65      private DefaultBHttpClientConnectionFactory(
66              final Http1Config http1Config,
67              final CharCodingConfig charCodingConfig,
68              final ContentLengthStrategy incomingContentStrategy,
69              final ContentLengthStrategy outgoingContentStrategy,
70              final ResponseOutOfOrderStrategy responseOutOfOrderStrategy,
71              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
72              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
73          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
74          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
75          this.incomingContentStrategy = incomingContentStrategy;
76          this.outgoingContentStrategy = outgoingContentStrategy;
77          this.responseOutOfOrderStrategy = responseOutOfOrderStrategy;
78          this.requestWriterFactory = requestWriterFactory != null ? requestWriterFactory :
79                  new DefaultHttpRequestWriterFactory(this.http1Config) ;
80          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
81                  new DefaultHttpResponseParserFactory(this.http1Config);
82      }
83  
84      public DefaultBHttpClientConnectionFactory(
85              final Http1Config http1Config,
86              final CharCodingConfig charCodingConfig,
87              final ContentLengthStrategy incomingContentStrategy,
88              final ContentLengthStrategy outgoingContentStrategy,
89              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
90              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
91          this(
92                  http1Config,
93                  charCodingConfig,
94                  incomingContentStrategy,
95                  outgoingContentStrategy,
96                  null,
97                  requestWriterFactory,
98                  responseParserFactory);
99      }
100 
101     public DefaultBHttpClientConnectionFactory(
102             final Http1Config http1Config,
103             final CharCodingConfig charCodingConfig,
104             final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
105             final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
106         this(http1Config, charCodingConfig, null, null, requestWriterFactory, responseParserFactory);
107     }
108 
109     public DefaultBHttpClientConnectionFactory(
110             final Http1Config http1Config,
111             final CharCodingConfig charCodingConfig) {
112         this(http1Config, charCodingConfig, null, null, null, null);
113     }
114 
115     public DefaultBHttpClientConnectionFactory() {
116         this(null, null, null, null, null, null);
117     }
118 
119     DefaultBHttpClientConnection createDetached() {
120         return new DefaultBHttpClientConnection(
121                 this.http1Config,
122                 CharCodingSupport.createDecoder(this.charCodingConfig),
123                 CharCodingSupport.createEncoder(this.charCodingConfig),
124                 this.incomingContentStrategy,
125                 this.outgoingContentStrategy,
126                 this.responseOutOfOrderStrategy,
127                 this.requestWriterFactory,
128                 this.responseParserFactory);
129     }
130 
131     @Override
132     public DefaultBHttpClientConnection createConnection(final Socket socket) throws IOException {
133         final DefaultBHttpClientConnection conn = createDetached();
134         conn.bind(socket);
135         return conn;
136     }
137 
138     /**
139      * @since 5.3
140      */
141     @Override
142     public DefaultBHttpClientConnection createConnection(final SSLSocket sslSocket, final Socket socket) throws IOException {
143         final DefaultBHttpClientConnection conn = createDetached();
144         conn.bind(sslSocket, socket);
145         return conn;
146     }
147 
148     /**
149      * Create a new {@link Builder}.
150      *
151      * @since 5.1
152      */
153     public static Builder builder() {
154         return new Builder();
155     }
156 
157     /**
158      * Builder for {@link DefaultBHttpClientConnectionFactory}.
159      *
160      * @since 5.1
161      */
162     public static final class Builder {
163         private Http1Config http1Config;
164         private CharCodingConfig charCodingConfig;
165         private ContentLengthStrategy incomingContentLengthStrategy;
166         private ContentLengthStrategy outgoingContentLengthStrategy;
167         private ResponseOutOfOrderStrategy responseOutOfOrderStrategy;
168         private HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
169         private HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
170 
171         private Builder() {}
172 
173         public Builder http1Config(final Http1Config http1Config) {
174             this.http1Config = http1Config;
175             return this;
176         }
177 
178         public Builder charCodingConfig(final CharCodingConfig charCodingConfig) {
179             this.charCodingConfig = charCodingConfig;
180             return this;
181         }
182 
183         public Builder incomingContentLengthStrategy(final ContentLengthStrategy incomingContentLengthStrategy) {
184             this.incomingContentLengthStrategy = incomingContentLengthStrategy;
185             return this;
186         }
187 
188         public Builder outgoingContentLengthStrategy(final ContentLengthStrategy outgoingContentLengthStrategy) {
189             this.outgoingContentLengthStrategy = outgoingContentLengthStrategy;
190             return this;
191         }
192 
193         public Builder responseOutOfOrderStrategy(final ResponseOutOfOrderStrategy responseOutOfOrderStrategy) {
194             this.responseOutOfOrderStrategy = responseOutOfOrderStrategy;
195             return this;
196         }
197 
198         public Builder requestWriterFactory(
199                 final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory) {
200             this.requestWriterFactory = requestWriterFactory;
201             return this;
202         }
203 
204         public Builder responseParserFactory(
205                 final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
206             this.responseParserFactory = responseParserFactory;
207             return this;
208         }
209 
210         public DefaultBHttpClientConnectionFactory build() {
211             return new DefaultBHttpClientConnectionFactory(
212                     http1Config,
213                     charCodingConfig,
214                     incomingContentLengthStrategy,
215                     outgoingContentLengthStrategy,
216                     responseOutOfOrderStrategy,
217                     requestWriterFactory,
218                     responseParserFactory);
219         }
220     }
221 }