1 7 package org.jboss.test.remoting.transport.http; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.OutputStream ; 14 import java.net.ServerSocket ; 15 import java.net.Socket ; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 24 public class RawHTTPServer 25 { 26 private ServerSocket socket; 27 private int port; 28 private boolean started = false; 29 private List clients = Collections.synchronizedList(new ArrayList ()); 30 31 private static int clientCount = 0; 32 33 private static final int DEFAULT_PORT = 80; 34 private Listener listener; 35 36 public RawHTTPServer() 37 { 38 this(DEFAULT_PORT); 39 } 40 41 public RawHTTPServer(int port) 42 { 43 this.port = port; 44 } 45 46 public void start() 47 { 48 if(!started) 49 { 50 System.out.println("Starting raw http server."); 51 try 52 { 53 this.socket = new ServerSocket (this.port); 54 this.port = socket.getLocalPort(); 55 this.started = true; 56 this.listener = new Listener(); 57 this.listener.start(); 58 System.out.println("Server started."); 59 } 60 catch(IOException e) 61 { 62 System.out.println("Server start failed."); 63 e.printStackTrace(); 64 } 65 } 66 } 67 68 public synchronized void stop() 69 { 70 if(started) 71 { 72 this.started = false; 73 this.listener.interrupt(); 74 try 75 { 76 this.listener.join(500); 77 } 78 catch(Throwable ig) 79 { 80 } 81 Iterator iter = clients.iterator(); 82 while(iter.hasNext()) 83 { 84 Client client = (Client) iter.next(); 85 client.interrupt(); 86 } 87 clients.clear(); 88 try 89 { 90 this.socket.close(); 91 } 92 catch(Throwable ig) 93 { 94 } 95 this.socket = null; 96 this.listener = null; 97 } 98 } 99 100 101 private final class Client extends Thread 102 { 103 Socket socket; 104 InputStream input; 105 OutputStream output; 106 String MIMEType = "text/html"; 107 String content = "<HTML><BODY>This is test results page from RawHTTPServer.</BODY></HTML>"; 108 String header = "HTTP 1.0 200 OK\r\n" + 109 "Server: RawHTTPServer\r\n" + 110 "Content-length: " + this.content.getBytes().length + "\r\n" + 111 "Content-type: " + MIMEType + "\r\n\rn\n"; 112 113 114 Client(Socket socket) 115 throws IOException 116 { 117 super("RawHTTPServer-Client [" + (++clientCount) + "]"); 118 setDaemon(true); 119 this.socket = socket; 120 this.input = new BufferedInputStream (socket.getInputStream()); 121 this.output = new BufferedOutputStream (socket.getOutputStream()); 122 clients.add(this); 123 } 124 125 public void run() 126 { 127 while(started) 128 { 129 try 130 { 131 StringBuffer request = new StringBuffer (80); 132 while(true) 133 { 134 int c = input.read(); 135 if(c == '\r' || c == '\n' || c == -1) 136 { 137 break; 138 } 139 request.append((char) c); 140 } 141 if(request.toString().indexOf("HTTP/") != -1) 142 { 143 output.write(this.header.getBytes()); 144 } 145 output.write(this.content.getBytes()); 146 output.flush(); 147 } 148 catch(IOException e) 149 { 150 e.printStackTrace(); 151 } 152 finally 153 { 154 if(socket != null) 155 { 156 try 157 { 158 socket.close(); 159 } 160 catch(IOException e) 161 { 162 e.printStackTrace(); } 164 } 165 } 166 167 183 193 } 194 clients.remove(this); 195 } 196 } 197 198 private final class Listener extends Thread 199 { 200 public Listener() 201 { 202 super("RawHTTPServer-Listener"); 203 setDaemon(false); 205 } 206 207 public void run() 208 { 209 while(started) 210 { 211 try 212 { 213 Socket client = socket.accept(); 215 if(client != null) 216 { 217 new Client(client).start(); 219 } 220 } 221 catch(Exception ex) 222 { 223 if(started) 224 { 225 ex.printStackTrace(); 226 } 227 } 228 } 229 } 230 } 231 232 233 public static void main(String [] args) 234 { 235 int port = RawHTTPServer.DEFAULT_PORT; 236 if(args != null && args.length > 0) 237 { 238 port = Integer.parseInt(args[0]); 239 } 240 RawHTTPServer server = new RawHTTPServer(port); 241 server.start(); 242 } 243 244 } | Popular Tags |