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