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;
29
30 import java.io.IOException;
31 import java.net.SocketTimeoutException;
32
33 import org.apache.http.HttpClientConnection;
34 import org.apache.http.HttpConnectionMetrics;
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpEntityEnclosingRequest;
37 import org.apache.http.HttpException;
38 import org.apache.http.HttpRequest;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.HttpResponseFactory;
41 import org.apache.http.annotation.NotThreadSafe;
42 import org.apache.http.entity.ContentLengthStrategy;
43 import org.apache.http.impl.entity.EntityDeserializer;
44 import org.apache.http.impl.entity.EntitySerializer;
45 import org.apache.http.impl.entity.LaxContentLengthStrategy;
46 import org.apache.http.impl.entity.StrictContentLengthStrategy;
47 import org.apache.http.impl.io.DefaultHttpResponseParser;
48 import org.apache.http.impl.io.HttpRequestWriter;
49 import org.apache.http.io.EofSensor;
50 import org.apache.http.io.HttpMessageParser;
51 import org.apache.http.io.HttpMessageWriter;
52 import org.apache.http.io.HttpTransportMetrics;
53 import org.apache.http.io.SessionInputBuffer;
54 import org.apache.http.io.SessionOutputBuffer;
55 import org.apache.http.message.LineFormatter;
56 import org.apache.http.message.LineParser;
57 import org.apache.http.params.HttpParams;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @NotThreadSafe
75 public abstract class AbstractHttpClientConnection implements HttpClientConnection {
76
77 private final EntitySerializer entityserializer;
78 private final EntityDeserializer entitydeserializer;
79
80 private SessionInputBuffer inbuffer = null;
81 private SessionOutputBuffer outbuffer = null;
82 private EofSensor eofSensor = null;
83 private HttpMessageParser<HttpResponse> responseParser = null;
84 private HttpMessageWriter<HttpRequest> requestWriter = null;
85 private HttpConnectionMetricsImpl metrics = null;
86
87
88
89
90
91
92
93
94
95 public AbstractHttpClientConnection() {
96 super();
97 this.entityserializer = createEntitySerializer();
98 this.entitydeserializer = createEntityDeserializer();
99 }
100
101
102
103
104
105
106 protected abstract void assertOpen() throws IllegalStateException;
107
108
109
110
111
112
113
114
115
116
117
118
119 protected EntityDeserializer createEntityDeserializer() {
120 return new EntityDeserializer(new LaxContentLengthStrategy());
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134 protected EntitySerializer createEntitySerializer() {
135 return new EntitySerializer(new StrictContentLengthStrategy());
136 }
137
138
139
140
141
142
143
144
145
146
147
148 protected HttpResponseFactory createHttpResponseFactory() {
149 return new DefaultHttpResponseFactory();
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 protected HttpMessageParser<HttpResponse> createResponseParser(
167 final SessionInputBuffer buffer,
168 final HttpResponseFactory responseFactory,
169 final HttpParams params) {
170 return new DefaultHttpResponseParser(buffer, null, responseFactory, params);
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 protected HttpMessageWriter<HttpRequest> createRequestWriter(
187 final SessionOutputBuffer buffer,
188 final HttpParams params) {
189 return new HttpRequestWriter(buffer, null, params);
190 }
191
192
193
194
195 protected HttpConnectionMetricsImpl createConnectionMetrics(
196 final HttpTransportMetrics inTransportMetric,
197 final HttpTransportMetrics outTransportMetric) {
198 return new HttpConnectionMetricsImpl(inTransportMetric, outTransportMetric);
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 protected void init(
218 final SessionInputBuffer inbuffer,
219 final SessionOutputBuffer outbuffer,
220 final HttpParams params) {
221 if (inbuffer == null) {
222 throw new IllegalArgumentException("Input session buffer may not be null");
223 }
224 if (outbuffer == null) {
225 throw new IllegalArgumentException("Output session buffer may not be null");
226 }
227 this.inbuffer = inbuffer;
228 this.outbuffer = outbuffer;
229 if (inbuffer instanceof EofSensor) {
230 this.eofSensor = (EofSensor) inbuffer;
231 }
232 this.responseParser = createResponseParser(
233 inbuffer,
234 createHttpResponseFactory(),
235 params);
236 this.requestWriter = createRequestWriter(
237 outbuffer, params);
238 this.metrics = createConnectionMetrics(
239 inbuffer.getMetrics(),
240 outbuffer.getMetrics());
241 }
242
243 public boolean isResponseAvailable(int timeout) throws IOException {
244 assertOpen();
245 try {
246 return this.inbuffer.isDataAvailable(timeout);
247 } catch (SocketTimeoutException ex) {
248 return false;
249 }
250 }
251
252 public void sendRequestHeader(final HttpRequest request)
253 throws HttpException, IOException {
254 if (request == null) {
255 throw new IllegalArgumentException("HTTP request may not be null");
256 }
257 assertOpen();
258 this.requestWriter.write(request);
259 this.metrics.incrementRequestCount();
260 }
261
262 public void sendRequestEntity(final HttpEntityEnclosingRequest request)
263 throws HttpException, IOException {
264 if (request == null) {
265 throw new IllegalArgumentException("HTTP request may not be null");
266 }
267 assertOpen();
268 if (request.getEntity() == null) {
269 return;
270 }
271 this.entityserializer.serialize(
272 this.outbuffer,
273 request,
274 request.getEntity());
275 }
276
277 protected void doFlush() throws IOException {
278 this.outbuffer.flush();
279 }
280
281 public void flush() throws IOException {
282 assertOpen();
283 doFlush();
284 }
285
286 public HttpResponse receiveResponseHeader()
287 throws HttpException, IOException {
288 assertOpen();
289 HttpResponse response = this.responseParser.parse();
290 if (response.getStatusLine().getStatusCode() >= 200) {
291 this.metrics.incrementResponseCount();
292 }
293 return response;
294 }
295
296 public void receiveResponseEntity(final HttpResponse response)
297 throws HttpException, IOException {
298 if (response == null) {
299 throw new IllegalArgumentException("HTTP response may not be null");
300 }
301 assertOpen();
302 HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response);
303 response.setEntity(entity);
304 }
305
306 protected boolean isEof() {
307 return this.eofSensor != null && this.eofSensor.isEof();
308 }
309
310 public boolean isStale() {
311 if (!isOpen()) {
312 return true;
313 }
314 if (isEof()) {
315 return true;
316 }
317 try {
318 this.inbuffer.isDataAvailable(1);
319 return isEof();
320 } catch (SocketTimeoutException ex) {
321 return false;
322 } catch (IOException ex) {
323 return true;
324 }
325 }
326
327 public HttpConnectionMetrics getMetrics() {
328 return this.metrics;
329 }
330
331 }