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      public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
88          final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
89          CharsetDecoder chardecoder = null;
90          CharsetEncoder charencoder = null;
91          final Charset charset = cconfig.getCharset();
92          final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ?
93                  cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
94          final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ?
95                  cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
96          if (charset != null) {
97              chardecoder = charset.newDecoder();
98              chardecoder.onMalformedInput(malformedInputAction);
99              chardecoder.onUnmappableCharacter(unmappableInputAction);
100             charencoder = charset.newEncoder();
101             charencoder.onMalformedInput(malformedInputAction);
102             charencoder.onUnmappableCharacter(unmappableInputAction);
103         }
104         final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
105         return new LoggingManagedHttpClientConnection(
106                 id,
107                 log,
108                 headerlog,
109                 wirelog,
110                 cconfig.getBufferSize(),
111                 cconfig.getFragmentSizeHint(),
112                 chardecoder,
113                 charencoder,
114                 cconfig.getMessageConstraints(),
115                 null,
116                 null,
117                 requestWriterFactory,
118                 responseParserFactory);
119     }
120 
121 }