1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v 1.1.2.4 2003/11/24 23:14:33 oglueck Exp $
3 * $Revision: 1.1.2.4 $
4 * $Date: 2003/11/24 23:14:33 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.server;
65
66 import java.io.IOException;
67 import java.net.ServerSocket;
68 import java.net.Socket;
69 import java.net.SocketException;
70 import java.util.HashSet;
71 import java.util.Iterator;
72 import java.util.Set;
73
74 import org.apache.commons.httpclient.HttpStatus;
75 import org.apache.commons.logging.Log;
76 import org.apache.commons.logging.LogFactory;
77
78 /***
79 * A simple, but extensible HTTP server, mostly for testing purposes.
80 *
81 * @author Christian Kohlschuetter
82 */
83 public class SimpleHttpServer implements Runnable {
84 private static final Log LOG = LogFactory.getLog(SimpleHttpServer.class);
85
86 private ServerSocket server = null;
87 private Thread t;
88 private ThreadGroup tg;
89 private boolean stopped = false;
90
91 private Set connections = new HashSet();
92
93 private HttpRequestHandler requestHandler = null;
94
95 /***
96 * Creates a new HTTP server instance, using an arbitrary free TCP port
97 *
98 * @throws IOException if anything goes wrong during initialization
99 */
100 public SimpleHttpServer() throws IOException {
101 this(0);
102 }
103
104 /***
105 * Creates a new HTTP server instance, using the specified TCP port
106 *
107 * @param port Desired TCP port
108 * @throws IOException if anything goes wrong during initialization
109 */
110 public SimpleHttpServer(int port) throws IOException {
111 server = new ServerSocket(port);
112 if(LOG.isInfoEnabled()) {
113 LOG.info("New SimpleHttpServer on port " + getLocalPort());
114 }
115 tg = new ThreadGroup("SimpleHttpServer group");
116 t = new Thread(tg, this, "SimpleHttpServer connection handler");
117 t.setDaemon(true);
118 t.start();
119 }
120
121 /***
122 * Returns the TCP port that this HTTP server instance is bound to.
123 *
124 * @return TCP port, or -1 if not running
125 */
126 public int getLocalPort() {
127 return server.getLocalPort();
128 }
129
130 /***
131 * Returns the IP address that this HTTP server instance is bound to.
132 * @return String representation of the IP address or <code>null</code> if not running
133 */
134 public String getLocalAddress() {
135 return server.getInetAddress().getHostAddress();
136 }
137
138 /***
139 * Checks if this HTTP server instance is running.
140 *
141 * @return true/false
142 */
143 public boolean isRunning() {
144 if(t == null) {
145 return false;
146 }
147 return t.isAlive();
148 }
149
150 /***
151 * Stops this HTTP server instance.
152 */
153 public void destroy() {
154 if (stopped) {
155 return;
156 }
157
158 stopped = true;
159 if(LOG.isInfoEnabled()) {
160 LOG.info("Stopping SimpleHttpServer on port " + getLocalPort());
161 }
162
163 tg.interrupt();
164
165 if (server != null) {
166 try {
167 server.close();
168 } catch(IOException e) {
169
170 }
171 }
172
173 for (Iterator it = connections.iterator(); it.hasNext();) {
174 SimpleHttpServerConnection conn =
175 (SimpleHttpServerConnection) it.next();
176 conn.destroy();
177 }
178 }
179
180 /***
181 * Returns the currently used HttpRequestHandler by this SimpleHttpServer
182 *
183 * @return The used HttpRequestHandler, or null.
184 */
185 public HttpRequestHandler getRequestHandler() {
186 return requestHandler;
187 }
188
189 /***
190 * Sets the HttpRequestHandler to be used for this SimpleHttpServer.
191 *
192 * @param rh Request handler to be used, or null to disable.
193 */
194 public void setRequestHandler(HttpRequestHandler rh) {
195 requestHandler = rh;
196 }
197
198 public void removeConnection(SimpleHttpServerConnection conn) {
199 connections.remove(conn);
200 }
201
202 public void processRequest(SimpleHttpServerConnection conn)
203 throws IOException {
204
205 boolean complete = false;
206 if (requestHandler != null) {
207 complete = requestHandler.processRequest(conn);
208 }
209 if (!complete) {
210 conn.connectionClose();
211 ErrorResponse.getInstance().getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE).processRequest(conn);
212 }
213 }
214
215 public void run() {
216 try {
217 while (!Thread.interrupted()) {
218 Socket socket = server.accept();
219 try {
220
221 SimpleHttpServerConnection conn =
222 new SimpleHttpServerConnection(this, socket);
223
224 connections.add(conn);
225
226 Thread t =
227 new Thread(tg, conn, "SimpleHttpServer connection");
228 t.setDaemon(true);
229 t.start();
230 } catch (IOException e) {
231 LOG.error("SimpleHttpServer error", e);
232 throw new RuntimeException(e.getMessage());
233 }
234 Thread.sleep(100);
235 }
236 } catch (InterruptedException accept) {
237 } catch (SocketException e) {
238 if (!stopped) {
239 LOG.error("SimpleHttpServer error", e);
240 throw new RuntimeException(e.getMessage());
241 }
242 } catch (IOException e) {
243 LOG.error("SimpleHttpServer error", e);
244 throw new RuntimeException(e.getMessage());
245 } finally {
246 destroy();
247 }
248 }
249 }
This page was automatically generated by Maven