001: /*
002: * SimpleWebServer.java
003: *
004: * Created on 17. Oktober 2003, 15:43
005: */
006:
007: package org.jzonic.webtester;
008:
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.OutputStream;
014: import java.net.ServerSocket;
015: import java.net.Socket;
016: import java.net.URL;
017: import java.util.HashMap;
018: import java.util.Map;
019: import java.util.StringTokenizer;
020:
021: /**
022: * This is a simple web server implementation used for URL testing.
023: * The code is based on the article How a webserver works published
024: * by java world.
025: *
026: * @author Andreas Mecky andreasmecky@yahoo.de
027: */
028: public class SimpleWebServer extends Thread {
029:
030: private boolean active = false;
031: private ServerSocket socket;
032: private Map parameters;
033: private boolean shutdownRequested = false;
034:
035: public SimpleWebServer() {
036: try {
037: socket = new ServerSocket(8181);
038: active = true;
039: setDaemon(true);
040: // FIXME: move this to a separate method
041: start();
042: } catch (Exception e) {
043: e.printStackTrace();
044: }
045: }
046:
047: public void run() {
048: while (active) {
049: try {
050: Socket clientSocket = socket.accept();
051: clientSocket.setTcpNoDelay(true);
052: if (!shutdownRequested) {
053: handleConnection(clientSocket);
054: }
055: } catch (Exception ie) {
056: ie.printStackTrace();
057: }
058: }
059: System.out.println("Shutting down");
060: }
061:
062: public void shutdown() {
063: active = false;
064: shutdownRequested = true;
065: try {
066: new Socket(socket.getInetAddress(), socket.getLocalPort())
067: .close();
068: } catch (IOException e) {
069: e.printStackTrace();
070: }
071: }
072:
073: private void handleConnection(Socket clientSocket) {
074: try {
075: OutputStream os = clientSocket.getOutputStream();
076: InputStream input = clientSocket.getInputStream();
077: StringBuffer request = new StringBuffer(2048);
078: int i;
079: byte[] buffer = new byte[2048];
080: try {
081: i = input.read(buffer);
082: } catch (IOException e) {
083: e.printStackTrace();
084: i = -1;
085: }
086: for (int j = 0; j < i; j++) {
087: request.append((char) buffer[j]);
088: }
089: String fileName = parseUri(request.toString());
090: parameters = new HashMap();
091: if (fileName.indexOf("?") != -1) {
092: parameters = parseParameters(fileName);
093: fileName = fileName.substring(0, fileName.indexOf("?"));
094: }
095: sendResponse(os, fileName);
096:
097: os.close();
098: input.close();
099: clientSocket.close();
100:
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: }
105:
106: public Map getParameters() {
107: return parameters;
108: }
109:
110: /**
111: * @param string
112: * @return
113: */
114: private Map parseParameters(String line) {
115: Map parameters = new HashMap();
116: //System.out.println("line:"+line);
117: String tmp = line.substring(line.indexOf("?") + 1);
118: if (tmp.indexOf("&") == -1) {
119: String name = tmp.substring(0, tmp.indexOf("="));
120: String val = tmp.substring(tmp.indexOf("=") + 1);
121: parameters.put(name, val);
122: } else {
123: StringTokenizer sto = new StringTokenizer(tmp, "&");
124: while (sto.hasMoreElements()) {
125: String current = (String) sto.nextElement();
126: String name = current
127: .substring(0, current.indexOf("="));
128: String val = current
129: .substring(current.indexOf("=") + 1);
130: parameters.put(name, val);
131: }
132: }
133: return parameters;
134: }
135:
136: private void sendResponse(OutputStream output, String fileName)
137: throws IOException {
138: byte[] bytes = new byte[2048];
139: FileInputStream fis = null;
140: try {
141: String message = "HTTP/1.1 200"
142: + "Content-Type: text/html\r\n" + "\r\n";
143: output.write(message.getBytes());
144: File file = getFileFromInputStream(fileName);
145: if (file != null && file.exists()) {
146: fis = new FileInputStream(file);
147: int ch = fis.read(bytes, 0, 2048);
148: while (ch != -1) {
149: output.write(bytes, 0, ch);
150: ch = fis.read(bytes, 0, 2048);
151: }
152: } else {
153: // file not found
154: String errorMessage = "HTTP/1.1 404 File Not Found\r\n"
155: + "Content-Type: text/html\r\n"
156: + "Content-Length: 23\r\n" + "\r\n"
157: + "<h1>File Not Found</h1>";
158: output.write(errorMessage.getBytes());
159: }
160: } catch (Exception e) {
161: e.printStackTrace();
162: } finally {
163: if (fis != null)
164: fis.close();
165: }
166: }
167:
168: private File getFileFromInputStream(String fileName) {
169: ClassLoader cl = Thread.currentThread().getContextClassLoader();
170: URL url = cl.getResource(fileName);
171: if (url != null) {
172: File file = new File(url.getFile());
173: return file;
174: } else {
175: return null;
176: }
177: }
178:
179: private String parseUri(String requestString) {
180: int index1, index2;
181: index1 = requestString.indexOf(' ');
182: if (index1 != -1) {
183: index2 = requestString.indexOf(' ', index1 + 1);
184: if (index2 > index1)
185: return requestString.substring(index1 + 2, index2);
186: }
187: return null;
188: }
189: }
|