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
32 import org.apache.http.HttpConnectionMetrics;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpEntityEnclosingRequest;
35 import org.apache.http.HttpException;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.HttpRequestFactory;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpServerConnection;
40 import org.apache.http.impl.entity.DisallowIdentityContentLengthStrategy;
41 import org.apache.http.impl.entity.EntityDeserializer;
42 import org.apache.http.impl.entity.EntitySerializer;
43 import org.apache.http.impl.entity.LaxContentLengthStrategy;
44 import org.apache.http.impl.entity.StrictContentLengthStrategy;
45 import org.apache.http.impl.io.DefaultHttpRequestParser;
46 import org.apache.http.impl.io.HttpResponseWriter;
47 import org.apache.http.io.EofSensor;
48 import org.apache.http.io.HttpMessageParser;
49 import org.apache.http.io.HttpMessageWriter;
50 import org.apache.http.io.HttpTransportMetrics;
51 import org.apache.http.io.SessionInputBuffer;
52 import org.apache.http.io.SessionOutputBuffer;
53 import org.apache.http.params.HttpParams;
54 import org.apache.http.util.Args;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @Deprecated
74 public abstract class AbstractHttpServerConnection implements HttpServerConnection {
75
76 private final EntitySerializer entityserializer;
77 private final EntityDeserializer entitydeserializer;
78
79 private SessionInputBuffer inBuffer = null;
80 private SessionOutputBuffer outbuffer = null;
81 private EofSensor eofSensor = null;
82 private HttpMessageParser<HttpRequest> requestParser = null;
83 private HttpMessageWriter<HttpResponse> responseWriter = null;
84 private HttpConnectionMetricsImpl metrics = null;
85
86
87
88
89
90
91
92
93
94 public AbstractHttpServerConnection() {
95 super();
96 this.entityserializer = createEntitySerializer();
97 this.entitydeserializer = createEntityDeserializer();
98 }
99
100
101
102
103
104
105 protected abstract void assertOpen() throws IllegalStateException;
106
107
108
109
110
111
112
113
114
115
116
117
118 protected EntityDeserializer createEntityDeserializer() {
119 return new EntityDeserializer(new DisallowIdentityContentLengthStrategy(
120 new LaxContentLengthStrategy(0)));
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 HttpRequestFactory createHttpRequestFactory() {
149 return DefaultHttpRequestFactory.INSTANCE;
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 protected HttpMessageParser<HttpRequest> createRequestParser(
168 final SessionInputBuffer buffer,
169 final HttpRequestFactory requestFactory,
170 final HttpParams params) {
171 return new DefaultHttpRequestParser(buffer, null, requestFactory, params);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 protected HttpMessageWriter<HttpResponse> createResponseWriter(
189 final SessionOutputBuffer buffer,
190 final HttpParams params) {
191 return new HttpResponseWriter(buffer, null, params);
192 }
193
194
195
196
197 protected HttpConnectionMetricsImpl createConnectionMetrics(
198 final HttpTransportMetrics inTransportMetric,
199 final HttpTransportMetrics outTransportMetric) {
200 return new HttpConnectionMetricsImpl(inTransportMetric, outTransportMetric);
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 protected void init(
220 final SessionInputBuffer inBuffer,
221 final SessionOutputBuffer outbuffer,
222 final HttpParams params) {
223 this.inBuffer = Args.notNull(inBuffer, "Input session buffer");
224 this.outbuffer = Args.notNull(outbuffer, "Output session buffer");
225 if (inBuffer instanceof EofSensor) {
226 this.eofSensor = (EofSensor) inBuffer;
227 }
228 this.requestParser = createRequestParser(
229 inBuffer,
230 createHttpRequestFactory(),
231 params);
232 this.responseWriter = createResponseWriter(
233 outbuffer, params);
234 this.metrics = createConnectionMetrics(
235 inBuffer.getMetrics(),
236 outbuffer.getMetrics());
237 }
238
239 @Override
240 public HttpRequest receiveRequestHeader()
241 throws HttpException, IOException {
242 assertOpen();
243 final HttpRequest request = this.requestParser.parse();
244 this.metrics.incrementRequestCount();
245 return request;
246 }
247
248 @Override
249 public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
250 throws HttpException, IOException {
251 Args.notNull(request, "HTTP request");
252 assertOpen();
253 final HttpEntity entity = this.entitydeserializer.deserialize(this.inBuffer, request);
254 request.setEntity(entity);
255 }
256
257 protected void doFlush() throws IOException {
258 this.outbuffer.flush();
259 }
260
261 @Override
262 public void flush() throws IOException {
263 assertOpen();
264 doFlush();
265 }
266
267 @Override
268 public void sendResponseHeader(final HttpResponse response)
269 throws HttpException, IOException {
270 Args.notNull(response, "HTTP response");
271 assertOpen();
272 this.responseWriter.write(response);
273 if (response.getStatusLine().getStatusCode() >= 200) {
274 this.metrics.incrementResponseCount();
275 }
276 }
277
278 @Override
279 public void sendResponseEntity(final HttpResponse response)
280 throws HttpException, IOException {
281 if (response.getEntity() == null) {
282 return;
283 }
284 this.entityserializer.serialize(
285 this.outbuffer,
286 response,
287 response.getEntity());
288 }
289
290 protected boolean isEof() {
291 return this.eofSensor != null && this.eofSensor.isEof();
292 }
293
294 @Override
295 public boolean isStale() {
296 if (!isOpen()) {
297 return true;
298 }
299 if (isEof()) {
300 return true;
301 }
302 try {
303 this.inBuffer.isDataAvailable(1);
304 return isEof();
305 } catch (final IOException ex) {
306 return true;
307 }
308 }
309
310 @Override
311 public HttpConnectionMetrics getMetrics() {
312 return this.metrics;
313 }
314
315 }