1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
55
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 ManagedHttpClientConnectionFactoryctory.html#ManagedHttpClientConnectionFactory">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
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 }