[prev in list] [next in list] [prev in thread] [next in thread]
List: jakarta-commons-dev
Subject: cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server HttpServiceHand
From: olegk () apache ! org
Date: 2004-10-31 21:57:16
Message-ID: 20041031215716.73137.qmail () minotaur ! apache ! org
[Download RAW message or body]
olegk 2004/10/31 13:57:15
Modified: httpclient/src/test/org/apache/commons/httpclient/server
SimpleHttpServer.java
SimpleHttpServerConnection.java
Added: httpclient/src/test/org/apache/commons/httpclient/server
HttpServiceHandler.java
Log:
* SimpleHttpServerConnection is no longer coupled with SimpleHttpServer
* Request processing/response writing code factored out of SimpleHttpServer
Revision Changes Path
1.9 +19 -98 \
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
Index: SimpleHttpServer.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SimpleHttpServer.java 16 Oct 2004 22:40:08 -0000 1.8
+++ SimpleHttpServer.java 31 Oct 2004 21:57:15 -0000 1.9
@@ -25,8 +25,6 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
package org.apache.commons.httpclient.server;
@@ -39,8 +37,6 @@
import java.util.Iterator;
import java.util.Set;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -53,7 +49,7 @@
public class SimpleHttpServer implements Runnable {
private static final Log LOG = LogFactory.getLog(SimpleHttpServer.class);
- private ServerSocket server = null;
+ private ServerSocket listener = null;
private Thread t;
private ThreadGroup tg;
private boolean stopped = false;
@@ -61,7 +57,7 @@
private Set connections = new HashSet();
private HttpRequestHandler requestHandler = null;
- private HttpService serivce = null;
+ private HttpService service = null;
/**
* Creates a new HTTP server instance, using an arbitrary free TCP port
@@ -79,7 +75,7 @@
* @throws IOException if anything goes wrong during initialization
*/
public SimpleHttpServer(int port) throws IOException {
- server = new ServerSocket(port);
+ listener = new ServerSocket(port);
if(LOG.isDebugEnabled()) {
LOG.debug("Starting test HTTP server on port " + getLocalPort());
}
@@ -95,7 +91,7 @@
* @return TCP port, or -1 if not running
*/
public int getLocalPort() {
- return server.getLocalPort();
+ return listener.getLocalPort();
}
/**
@@ -103,7 +99,7 @@
* @return String representation of the IP address or <code>null</code> if not \
running
*/
public String getLocalAddress() {
- InetAddress address = server.getInetAddress();
+ InetAddress address = listener.getInetAddress();
// Ugly work-around for older JDKs
byte[] octets = address.getAddress();
if ((octets[0] == 0)
@@ -143,9 +139,9 @@
tg.interrupt();
- if (server != null) {
+ if (listener != null) {
try {
- server.close();
+ listener.close();
} catch(IOException e) {
}
@@ -177,102 +173,27 @@
}
public void setHttpService(HttpService service) {
- this.serivce = service;
+ this.service = service;
}
public void removeConnection(SimpleHttpServerConnection conn) {
connections.remove(conn);
}
- public void processRequest(
- final SimpleHttpServerConnection conn,
- final SimpleRequest request) throws IOException
- {
- if (conn == null) {
- throw new IllegalArgumentException("Connection may not be null");
- }
- if (request == null) {
- throw new IllegalArgumentException("Request may not be null");
- }
- boolean complete = false;
- if (this.requestHandler != null) {
- complete = requestHandler.processRequest(conn, request);
- if (complete) {
- return;
- }
- }
- SimpleResponse response = null;
- if (this.serivce != null) {
- response = new SimpleResponse();
- complete = this.serivce.process(request, response);
- }
- if (!complete) {
- response = ErrorResponse.getInstance().
- getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE);
- conn.connectionClose();
- }
- writeResponse(conn, response);
- }
-
- public void writeResponse(
- final SimpleHttpServerConnection conn,
- final SimpleResponse response) throws IOException
- {
- if (response == null) {
- return;
- }
- ResponseWriter out = conn.getWriter();
- if (!response.containsHeader("Content-Length")) {
- int len = 0;
- if (response.getBodyString() != null) {
- len = response.getBodyString().length();
- }
- response.addHeader(
- new Header("Content-Length", Integer.toString(len), true));
- }
- if (!response.containsHeader("Content-Type")) {
- StringBuffer buffer = new StringBuffer();
- if (response.getContentType() != null) {
- buffer.append(response.getContentType());
- if (out.getEncoding() != null) {
- buffer.append("; charset=");
- buffer.append(out.getEncoding());
- }
- }
- response.addHeader(
- new Header("Content-Type", buffer.toString(), true));
- }
- // @TODO implement HTTP/1.1 persistent connections
- if (!conn.isKeepAlive() && !response.containsHeader("Connection")) {
- response.setHeader(
- new Header("Connection", "close", true));
- }
- out.println(response.getStatusLine());
- Iterator item = response.getHeaderIterator();
- while (item.hasNext()) {
- Header header = (Header) item.next();
- out.print(header.toExternalForm());
- }
- out.println();
- if (response.getBodyString() != null) {
- out.print(response.getBodyString());
- }
- out.flush();
- }
-
public void run() {
try {
while (!Thread.interrupted()) {
- Socket socket = server.accept();
+ Socket socket = listener.accept();
try {
-
- SimpleHttpServerConnection conn =
- new SimpleHttpServerConnection(this, socket);
-
+ HttpRequestHandler handler = this.requestHandler;
+ if (handler == null && this.service != null) {
+ handler = new HttpServiceHandler(this.service);
+ }
+ SimpleHttpServerConnection conn =
+ new SimpleHttpServerConnection(socket, handler);
connections.add(conn);
- Thread t =
- new Thread(tg, conn, "SimpleHttpServer connection");
+ Thread t = new Thread(tg, conn, "SimpleHttpServer \
connection"); t.setDaemon(true);
t.start();
} catch (IOException e) {
1.13 +64 -37 \
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
Index: SimpleHttpServerConnection.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- SimpleHttpServerConnection.java 10 Oct 2004 15:16:44 -0000 1.12
+++ SimpleHttpServerConnection.java 31 Oct 2004 21:57:15 -0000 1.13
@@ -25,8 +25,6 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
package org.apache.commons.httpclient.server;
@@ -37,6 +35,7 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
+import java.util.Iterator;
import org.apache.commons.httpclient.ChunkedInputStream;
import org.apache.commons.httpclient.ContentLengthInputStream;
@@ -52,6 +51,7 @@
* A connection to the SimpleHttpServer.
*
* @author Christian Kohlschuetter
+ * @author Oleg Kalnichevski
*/
public class SimpleHttpServerConnection implements Runnable {
@@ -59,21 +59,23 @@
private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
- private SimpleHttpServer server;
private Socket socket;
private InputStream in;
private OutputStream out;
-
- private int requestNo = 0;
-
private boolean keepAlive = false;
+ private HttpRequestHandler handler = null;
- public SimpleHttpServerConnection(SimpleHttpServer server, Socket socket) \
throws IOException {
- this.server = server;
+ public SimpleHttpServerConnection(final Socket socket, final \
HttpRequestHandler handler) + throws IOException {
+ super();
+ if (handler == null) {
+ throw new IllegalArgumentException("Handler may not be null");
+ }
this.socket = socket;
this.socket.setSoTimeout(2000);
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
+ this.handler = handler;
}
public synchronized void destroy() {
@@ -87,16 +89,12 @@
} catch (IOException e) {
// fail("Unexpected exception: " + e);
}
- server.removeConnection(this);
}
public void run() {
- requestNo = 0;
try {
do {
keepAlive = false;
-
- ++this.requestNo;
readRequest();
} while (keepAlive);
} catch (IOException e) {
@@ -123,27 +121,17 @@
keepAlive = true;
}
- /**
- * Returns the ResponseWriter used to write the output to the socket.
- *
- * @return This connection's ResponseWriter
- */
- public ResponseWriter getWriter() {
- try {
- return new ResponseWriter(out);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e.toString());
- }
+ public boolean isKeepAlive() {
+ return this.keepAlive;
}
/**
- * Returns the number of requests processed (including the current one) for
- * this connection.
+ * Returns the ResponseWriter used to write the output to the socket.
*
- * @return
+ * @return This connection's ResponseWriter
*/
- public int getRequestNumber() {
- return requestNo;
+ public ResponseWriter getWriter() throws UnsupportedEncodingException {
+ return new ResponseWriter(out);
}
private void readRequest() throws IOException {
@@ -166,14 +154,57 @@
connectionClose();
SimpleResponse response = ErrorResponse.getInstance().
getResponse(HttpStatus.SC_BAD_REQUEST);
- server.writeResponse(this, response);
+ writeResponse(response);
return;
} catch (IOException e) {
connectionClose();
LOG.error("I/O error processing request", e);
return;
}
- server.processRequest(this, request);
+ handler.processRequest(this, request);
+ out.flush();
+ }
+
+ public void writeResponse(final SimpleResponse response) throws IOException {
+ if (response == null) {
+ return;
+ }
+ ResponseWriter out = getWriter();
+ if (!response.containsHeader("Content-Length")) {
+ int len = 0;
+ if (response.getBodyString() != null) {
+ len = response.getBodyString().length();
+ }
+ response.addHeader(
+ new Header("Content-Length", Integer.toString(len), true));
+ }
+ if (!response.containsHeader("Content-Type")) {
+ StringBuffer buffer = new StringBuffer();
+ if (response.getContentType() != null) {
+ buffer.append(response.getContentType());
+ if (out.getEncoding() != null) {
+ buffer.append("; charset=");
+ buffer.append(out.getEncoding());
+ }
+ }
+ response.addHeader(
+ new Header("Content-Type", buffer.toString(), true));
+ }
+ // @TODO implement HTTP/1.1 persistent connections
+ if (!isKeepAlive() && !response.containsHeader("Connection")) {
+ response.setHeader(
+ new Header("Connection", "close", true));
+ }
+ out.println(response.getStatusLine());
+ Iterator item = response.getHeaderIterator();
+ while (item.hasNext()) {
+ Header header = (Header) item.next();
+ out.print(header.toExternalForm());
+ }
+ out.println();
+ if (response.getBodyString() != null) {
+ out.print(response.getBodyString());
+ }
out.flush();
}
@@ -226,10 +257,6 @@
public OutputStream getOutputStream() {
return out;
- }
-
- public boolean isKeepAlive() {
- return this.keepAlive;
}
}
1.1 \
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java
Index: HttpServiceHandler.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java,v \
1.1 2004/10/31 21:57:15 olegk Exp $
* $Revision: 1.1 $
* $Date: 2004/10/31 21:57:15 $
*
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.server;
import java.io.IOException;
/**
* This request handler provides service interface similar to that of Servlet API.
*
* @author Oleg Kalnichevski
*/
public class HttpServiceHandler implements HttpRequestHandler {
private HttpService service = null;
public HttpServiceHandler(final HttpService service) {
super();
if (service == null) {
throw new IllegalArgumentException("Service may not be null");
}
this.service = service;
}
public boolean processRequest(
final SimpleHttpServerConnection conn,
final SimpleRequest request) throws IOException {
if (conn == null) {
throw new IllegalArgumentException("Connection may not be null");
}
if (request == null) {
throw new IllegalArgumentException("Request may not be null");
}
boolean complete = false;
SimpleResponse response = new SimpleResponse();
this.service.process(request, response);
conn.writeResponse(response);
return true;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic