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 org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.ContentLengthStrategy;
38  import org.apache.hc.core5.http.config.CharCodingConfig;
39  import org.apache.hc.core5.http.config.Http1Config;
40  import org.apache.hc.core5.http.impl.CharCodingSupport;
41  import org.apache.hc.core5.http.io.HttpConnectionFactory;
42  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
43  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
44  
45  /**
46   * Default factory for {@link org.apache.hc.core5.http.io.HttpClientConnection}s.
47   *
48   * @since 4.3
49   */
50  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
51  public class DefaultBHttpClientConnectionFactory
52          implements HttpConnectionFactory<DefaultBHttpClientConnection> {
53  
54      private final Http1Config http1Config;
55      private final CharCodingConfig charCodingConfig;
56      private final ContentLengthStrategy incomingContentStrategy;
57      private final ContentLengthStrategy outgoingContentStrategy;
58      private final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
59      private final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
60  
61      public DefaultBHttpClientConnectionFactory(
62              final Http1Config http1Config,
63              final CharCodingConfig charCodingConfig,
64              final ContentLengthStrategy incomingContentStrategy,
65              final ContentLengthStrategy outgoingContentStrategy,
66              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
67              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
68          super();
69          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
70          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
71          this.incomingContentStrategy = incomingContentStrategy;
72          this.outgoingContentStrategy = outgoingContentStrategy;
73          this.requestWriterFactory = requestWriterFactory;
74          this.responseParserFactory = responseParserFactory;
75      }
76  
77      public DefaultBHttpClientConnectionFactory(
78              final Http1Config http1Config,
79              final CharCodingConfig charCodingConfig,
80              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
81              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
82          this(http1Config, charCodingConfig, null, null, requestWriterFactory, responseParserFactory);
83      }
84  
85      public DefaultBHttpClientConnectionFactory(
86              final Http1Config http1Config,
87              final CharCodingConfig charCodingConfig) {
88          this(http1Config, charCodingConfig, null, null, null, null);
89      }
90  
91      public DefaultBHttpClientConnectionFactory() {
92          this(null, null, null, null, null, null);
93      }
94  
95      @Override
96      public DefaultBHttpClientConnection createConnection(final Socket socket) throws IOException {
97          final DefaultBHttpClientConnectiontBHttpClientConnection.html#DefaultBHttpClientConnection">DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(
98                  this.http1Config,
99                  CharCodingSupport.createDecoder(this.charCodingConfig),
100                 CharCodingSupport.createEncoder(this.charCodingConfig),
101                 this.incomingContentStrategy,
102                 this.outgoingContentStrategy,
103                 this.requestWriterFactory,
104                 this.responseParserFactory);
105         conn.bind(socket);
106         return conn;
107     }
108 
109 }