HttpServicepublic class HttpService extends Object Minimalistic server-side implementation of an HTTP processor. |
Fields Summary |
---|
private HttpParams | params | private HttpProcessor | processor | private HttpRequestHandlerResolver | handlerResolver | private ConnectionReuseStrategy | connStrategy | private HttpResponseFactory | responseFactory | private HttpExpectationVerifier | expectationVerifier |
Methods Summary |
---|
protected void | doService(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, org.apache.http.protocol.HttpContext context)
HttpRequestHandler handler = null;
if (this.handlerResolver != null) {
String requestURI = request.getRequestLine().getUri();
handler = this.handlerResolver.lookup(requestURI);
}
if (handler != null) {
handler.handle(request, response, context);
} else {
response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
}
| public org.apache.http.params.HttpParams | getParams()
return this.params;
| protected void | handleException(org.apache.http.HttpException ex, org.apache.http.HttpResponse response)
if (ex instanceof MethodNotSupportedException) {
response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
} else if (ex instanceof UnsupportedHttpVersionException) {
response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
} else if (ex instanceof ProtocolException) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
} else {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
ByteArrayEntity entity = new ByteArrayEntity(msg);
entity.setContentType("text/plain; charset=US-ASCII");
response.setEntity(entity);
| public void | handleRequest(org.apache.http.HttpServerConnection conn, org.apache.http.protocol.HttpContext context)
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
HttpResponse response = null;
try {
HttpRequest request = conn.receiveRequestHeader();
request.setParams(
new DefaultedHttpParams(request.getParams(), this.params));
ProtocolVersion ver =
request.getRequestLine().getProtocolVersion();
if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
// Downgrade protocol version if greater than HTTP/1.1
ver = HttpVersion.HTTP_1_1;
}
if (request instanceof HttpEntityEnclosingRequest) {
if (((HttpEntityEnclosingRequest) request).expectContinue()) {
response = this.responseFactory.newHttpResponse(ver,
HttpStatus.SC_CONTINUE, context);
response.setParams(
new DefaultedHttpParams(response.getParams(), this.params));
if (this.expectationVerifier != null) {
try {
this.expectationVerifier.verify(request, response, context);
} catch (HttpException ex) {
response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0,
HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
response.setParams(
new DefaultedHttpParams(response.getParams(), this.params));
handleException(ex, response);
}
}
if (response.getStatusLine().getStatusCode() < 200) {
// Send 1xx response indicating the server expections
// have been met
conn.sendResponseHeader(response);
conn.flush();
response = null;
conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
}
} else {
conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
}
}
if (response == null) {
response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
response.setParams(
new DefaultedHttpParams(response.getParams(), this.params));
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
this.processor.process(request, context);
doService(request, response, context);
}
// Make sure the request content is fully consumed
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
if (entity != null) {
entity.consumeContent();
}
}
} catch (HttpException ex) {
response = this.responseFactory.newHttpResponse
(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR,
context);
response.setParams(
new DefaultedHttpParams(response.getParams(), this.params));
handleException(ex, response);
}
this.processor.process(response, context);
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
if (!this.connStrategy.keepAlive(response, context)) {
conn.close();
}
| public void | setConnReuseStrategy(org.apache.http.ConnectionReuseStrategy connStrategy)
if (connStrategy == null) {
throw new IllegalArgumentException("Connection reuse strategy may not be null");
}
this.connStrategy = connStrategy;
| public void | setExpectationVerifier(org.apache.http.protocol.HttpExpectationVerifier expectationVerifier)
this.expectationVerifier = expectationVerifier;
| public void | setHandlerResolver(org.apache.http.protocol.HttpRequestHandlerResolver handlerResolver)
this.handlerResolver = handlerResolver;
| public void | setHttpProcessor(org.apache.http.protocol.HttpProcessor processor)
if (processor == null) {
throw new IllegalArgumentException("HTTP processor may not be null.");
}
this.processor = processor;
| public void | setParams(org.apache.http.params.HttpParams params)
this.params = params;
| public void | setResponseFactory(org.apache.http.HttpResponseFactory responseFactory)
if (responseFactory == null) {
throw new IllegalArgumentException("Response factory may not be null");
}
this.responseFactory = responseFactory;
|
|