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.conn;
29  
30  import java.nio.charset.Charset;
31  import java.nio.charset.CharsetDecoder;
32  import java.nio.charset.CharsetEncoder;
33  import java.nio.charset.CodingErrorAction;
34  import java.util.concurrent.atomic.AtomicLong;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.http.HttpRequest;
39  import org.apache.http.HttpResponse;
40  import org.apache.http.annotation.Contract;
41  import org.apache.http.annotation.ThreadingBehavior;
42  import org.apache.http.config.ConnectionConfig;
43  import org.apache.http.conn.HttpConnectionFactory;
44  import org.apache.http.conn.ManagedHttpClientConnection;
45  import org.apache.http.conn.routing.HttpRoute;
46  import org.apache.http.entity.ContentLengthStrategy;
47  import org.apache.http.impl.entity.LaxContentLengthStrategy;
48  import org.apache.http.impl.entity.StrictContentLengthStrategy;
49  import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
50  import org.apache.http.io.HttpMessageParserFactory;
51  import org.apache.http.io.HttpMessageWriterFactory;
52  
53  /**
54   * Factory for {@link ManagedHttpClientConnection} instances.
55   * @since 4.3
56   */
57  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
58  public class ManagedHttpClientConnectionFactory
59          implements HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> {
60  
61      private static final AtomicLong COUNTER = new AtomicLong();
62  
63      public static final ManagedHttpClientConnectionFactory INSTANCE = new ManagedHttpClientConnectionFactory();
64  
65      private final Log log = LogFactory.getLog(DefaultManagedHttpClientConnection.class);
66      private final Log headerlog = LogFactory.getLog("org.apache.http.headers");
67      private final Log wirelog = LogFactory.getLog("org.apache.http.wire");
68  
69      private final HttpMessageWriterFactory<HttpRequest> requestWriterFactory;
70      private final HttpMessageParserFactory<HttpResponse> responseParserFactory;
71      private final ContentLengthStrategy incomingContentStrategy;
72      private final ContentLengthStrategy outgoingContentStrategy;
73  
74      /**
75       * @since 4.4
76       */
77      public ManagedHttpClientConnectionFactory(
78              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
79              final HttpMessageParserFactory<HttpResponse> responseParserFactory,
80              final ContentLengthStrategy incomingContentStrategy,
81              final ContentLengthStrategy outgoingContentStrategy) {
82          super();
83          this.requestWriterFactory = requestWriterFactory != null ? requestWriterFactory :
84                  DefaultHttpRequestWriterFactory.INSTANCE;
85          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
86                  DefaultHttpResponseParserFactory.INSTANCE;
87          this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy :
88                  LaxContentLengthStrategy.INSTANCE;
89          this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
90                  StrictContentLengthStrategy.INSTANCE;
91      }
92  
93      public ManagedHttpClientConnectionFactory(
94              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
95              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
96          this(requestWriterFactory, responseParserFactory, null, null);
97      }
98  
99      public ManagedHttpClientConnectionFactory(
100             final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
101         this(null, responseParserFactory);
102     }
103 
104     public ManagedHttpClientConnectionFactory() {
105         this(null, null);
106     }
107 
108     @Override
109     public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
110         final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
111         CharsetDecoder chardecoder = null;
112         CharsetEncoder charencoder = null;
113         final Charset charset = cconfig.getCharset();
114         final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ?
115                 cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
116         final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ?
117                 cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
118         if (charset != null) {
119             chardecoder = charset.newDecoder();
120             chardecoder.onMalformedInput(malformedInputAction);
121             chardecoder.onUnmappableCharacter(unmappableInputAction);
122             charencoder = charset.newEncoder();
123             charencoder.onMalformedInput(malformedInputAction);
124             charencoder.onUnmappableCharacter(unmappableInputAction);
125         }
126         final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
127         return new LoggingManagedHttpClientConnection(
128                 id,
129                 log,
130                 headerlog,
131                 wirelog,
132                 cconfig.getBufferSize(),
133                 cconfig.getFragmentSizeHint(),
134                 chardecoder,
135                 charencoder,
136                 cconfig.getMessageConstraints(),
137                 incomingContentStrategy,
138                 outgoingContentStrategy,
139                 requestWriterFactory,
140                 responseParserFactory);
141     }
142 
143 }