001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java,v 1.9 2004/11/13 22:38:27 mbecke Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: import org.apache.commons.httpclient.Header;
037: import org.apache.commons.httpclient.HttpVersion;
038:
039: /**
040: * This request handler provides service interface similar to that of Servlet API.
041: *
042: * @author Oleg Kalnichevski
043: */
044: public class HttpServiceHandler implements HttpRequestHandler {
045:
046: private HttpService service = null;
047:
048: public HttpServiceHandler(final HttpService service) {
049: super ();
050: if (service == null) {
051: throw new IllegalArgumentException(
052: "Service may not be null");
053: }
054: this .service = service;
055: }
056:
057: public boolean processRequest(
058: final SimpleHttpServerConnection conn,
059: final SimpleRequest request) throws IOException {
060: if (conn == null) {
061: throw new IllegalArgumentException(
062: "Connection may not be null");
063: }
064: if (request == null) {
065: throw new IllegalArgumentException(
066: "Request may not be null");
067: }
068: boolean complete = false;
069: SimpleResponse response = new SimpleResponse();
070: this .service.process(request, response);
071:
072: // Nake sure the request if fully consumed
073: request.getBodyBytes();
074:
075: // Ensure there's a content type header
076: if (!response.containsHeader("Content-Type")) {
077: response
078: .addHeader(new Header("Content-Type", "text/plain"));
079: }
080:
081: // Ensure there's a content length or transfer encoding header
082: if (!response.containsHeader("Content-Length")
083: && !response.containsHeader("Transfer-Encoding")) {
084: InputStream content = response.getBody();
085: if (content != null) {
086: long len = response.getContentLength();
087: if (len < 0) {
088: if (response.getHttpVersion().lessEquals(
089: HttpVersion.HTTP_1_0)) {
090: throw new IOException(
091: "Chunked encoding not supported for HTTP version "
092: + response.getHttpVersion());
093: }
094: Header header = new Header("Transfer-Encoding",
095: "chunked");
096: response.addHeader(header);
097: } else {
098: Header header = new Header("Content-Length", Long
099: .toString(len));
100: response.setHeader(header);
101: }
102: } else {
103: Header header = new Header("Content-Length", "0");
104: response.addHeader(header);
105: }
106: }
107:
108: if (!response.containsHeader("Connection")) {
109: // See if the the client explicitly handles connection persistence
110: Header connheader = request.getFirstHeader("Connection");
111: if (connheader != null) {
112: if (connheader.getValue()
113: .equalsIgnoreCase("keep-alive")) {
114: Header header = new Header("Connection",
115: "keep-alive");
116: response.addHeader(header);
117: conn.setKeepAlive(true);
118: }
119: if (connheader.getValue().equalsIgnoreCase("close")) {
120: Header header = new Header("Connection", "close");
121: response.addHeader(header);
122: conn.setKeepAlive(false);
123: }
124: } else {
125: // Use protocol default connection policy
126: if (response.getHttpVersion().greaterEquals(
127: HttpVersion.HTTP_1_1)) {
128: conn.setKeepAlive(true);
129: } else {
130: conn.setKeepAlive(false);
131: }
132: }
133: }
134: if ("HEAD".equalsIgnoreCase(request.getRequestLine()
135: .getMethod())) {
136: // this is a head request, we don't want to send the actualy content
137: response.setBody(null);
138: }
139: conn.writeResponse(response);
140: return true;
141: }
142:
143: }
|