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.http.impl;
29  
30  import org.apache.http.HttpConnectionFactory;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.annotation.Immutable;
34  import org.apache.http.config.ConnectionConfig;
35  import org.apache.http.entity.ContentLengthStrategy;
36  import org.apache.http.io.HttpMessageParserFactory;
37  import org.apache.http.io.HttpMessageWriterFactory;
38  
39  import java.io.IOException;
40  import java.net.Socket;
41  
42  /**
43   * Default factory for {@link org.apache.http.HttpClientConnection}s.
44   *
45   * @since 4.3
46   */
47  @Immutable
48  public class DefaultBHttpClientConnectionFactory
49          implements HttpConnectionFactory<DefaultBHttpClientConnection> {
50  
51      public static final DefaultBHttpClientConnectionFactory INSTANCE = new DefaultBHttpClientConnectionFactory();
52  
53      private final ConnectionConfig cconfig;
54      private final ContentLengthStrategy incomingContentStrategy;
55      private final ContentLengthStrategy outgoingContentStrategy;
56      private final HttpMessageWriterFactory<HttpRequest> requestWriterFactory;
57      private final HttpMessageParserFactory<HttpResponse> responseParserFactory;
58  
59      public DefaultBHttpClientConnectionFactory(
60              final ConnectionConfig cconfig,
61              final ContentLengthStrategy incomingContentStrategy,
62              final ContentLengthStrategy outgoingContentStrategy,
63              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
64              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
65          super();
66          this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
67          this.incomingContentStrategy = incomingContentStrategy;
68          this.outgoingContentStrategy = outgoingContentStrategy;
69          this.requestWriterFactory = requestWriterFactory;
70          this.responseParserFactory = responseParserFactory;
71      }
72  
73      public DefaultBHttpClientConnectionFactory(
74              final ConnectionConfig cconfig,
75              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
76              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
77          this(cconfig, null, null, requestWriterFactory, responseParserFactory);
78      }
79  
80      public DefaultBHttpClientConnectionFactory(final ConnectionConfig cconfig) {
81          this(cconfig, null, null, null, null);
82      }
83  
84      public DefaultBHttpClientConnectionFactory() {
85          this(null, null, null, null, null);
86      }
87  
88      @Override
89      public DefaultBHttpClientConnection createConnection(final Socket socket) throws IOException {
90          final DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(
91                  this.cconfig.getBufferSize(),
92                  this.cconfig.getFragmentSizeHint(),
93                  ConnSupport.createDecoder(this.cconfig),
94                  ConnSupport.createEncoder(this.cconfig),
95                  this.cconfig.getMessageConstraints(),
96                  this.incomingContentStrategy,
97                  this.outgoingContentStrategy,
98                  this.requestWriterFactory,
99                  this.responseParserFactory);
100         conn.bind(socket);
101         return conn;
102     }
103 
104 }