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.Immutable;
41  import org.apache.http.config.ConnectionConfig;
42  import org.apache.http.conn.HttpConnectionFactory;
43  import org.apache.http.conn.ManagedHttpClientConnection;
44  import org.apache.http.conn.routing.HttpRoute;
45  import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
46  import org.apache.http.io.HttpMessageParserFactory;
47  import org.apache.http.io.HttpMessageWriterFactory;
48  
49  /**
50   * Factory for {@link ManagedHttpClientConnection} instances.
51   * @since 4.3
52   */
53  @Immutable
54  public class ManagedHttpClientConnectionFactory
55          implements HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> {
56  
57      private static final AtomicLong COUNTER = new AtomicLong();
58  
59      public static final ManagedHttpClientConnectionFactory INSTANCE = new ManagedHttpClientConnectionFactory();
60  
61      private final Log log = LogFactory.getLog(DefaultManagedHttpClientConnection.class);
62      private final Log headerlog = LogFactory.getLog("org.apache.http.headers");
63      private final Log wirelog = LogFactory.getLog("org.apache.http.wire");
64  
65      private final HttpMessageWriterFactory<HttpRequest> requestWriterFactory;
66      private final HttpMessageParserFactory<HttpResponse> responseParserFactory;
67  
68      public ManagedHttpClientConnectionFactory(
69              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
70              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
71          super();
72          this.requestWriterFactory = requestWriterFactory != null ? requestWriterFactory :
73              DefaultHttpRequestWriterFactory.INSTANCE;
74          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
75              DefaultHttpResponseParserFactory.INSTANCE;
76      }
77  
78      public ManagedHttpClientConnectionFactory(
79              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
80          this(null, responseParserFactory);
81      }
82  
83      public ManagedHttpClientConnectionFactory() {
84          this(null, null);
85      }
86  
87      @Override
88      public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
89          final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
90          CharsetDecoder chardecoder = null;
91          CharsetEncoder charencoder = null;
92          final Charset charset = cconfig.getCharset();
93          final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ?
94                  cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
95          final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ?
96                  cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
97          if (charset != null) {
98              chardecoder = charset.newDecoder();
99              chardecoder.onMalformedInput(malformedInputAction);
100             chardecoder.onUnmappableCharacter(unmappableInputAction);
101             charencoder = charset.newEncoder();
102             charencoder.onMalformedInput(malformedInputAction);
103             charencoder.onUnmappableCharacter(unmappableInputAction);
104         }
105         final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
106         return new LoggingManagedHttpClientConnection(
107                 id,
108                 log,
109                 headerlog,
110                 wirelog,
111                 cconfig.getBufferSize(),
112                 cconfig.getFragmentSizeHint(),
113                 chardecoder,
114                 charencoder,
115                 cconfig.getMessageConstraints(),
116                 null,
117                 null,
118                 requestWriterFactory,
119                 responseParserFactory);
120     }
121 
122 }