KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > http > RawHTTPServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.http;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.net.ServerSocket JavaDoc;
15 import java.net.Socket JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
23  */

24 public class RawHTTPServer
25 {
26    private ServerSocket JavaDoc socket;
27    private int port;
28    private boolean started = false;
29    private List JavaDoc clients = Collections.synchronizedList(new ArrayList JavaDoc());
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 JavaDoc(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 JavaDoc 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 JavaDoc ig)
79          {
80          }
81          Iterator JavaDoc 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 JavaDoc ig)
93          {
94          }
95          this.socket = null;
96          this.listener = null;
97       }
98    }
99
100
101    private final class Client extends Thread JavaDoc
102    {
103       Socket JavaDoc socket;
104       InputStream JavaDoc input;
105       OutputStream JavaDoc output;
106       String JavaDoc MIMEType = "text/html";
107       String JavaDoc content = "<HTML><BODY>This is test results page from RawHTTPServer.</BODY></HTML>";
108       String JavaDoc 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 JavaDoc socket)
115             throws IOException JavaDoc
116       {
117          super("RawHTTPServer-Client [" + (++clientCount) + "]");
118          setDaemon(true);
119          this.socket = socket;
120          this.input = new BufferedInputStream JavaDoc(socket.getInputStream());
121          this.output = new BufferedOutputStream JavaDoc(socket.getOutputStream());
122          clients.add(this);
123       }
124
125       public void run()
126       {
127          while(started)
128          {
129             try
130             {
131                StringBuffer JavaDoc request = new StringBuffer JavaDoc(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 JavaDoc e)
149             {
150                e.printStackTrace();
151             }
152             finally
153             {
154                if(socket != null)
155                {
156                   try
157                   {
158                      socket.close();
159                   }
160                   catch(IOException JavaDoc e)
161                   {
162                      e.printStackTrace(); //TODO: -TME Implement
163
}
164                }
165             }
166
167 /*
168             try
169             {
170                int n = 0;
171                byte[] buffer = new byte[1024];
172                while (n != -1 || input.available() > 0)
173                {
174                   n = input.read(buffer);
175                   System.out.println(new String(buffer));
176                }
177             }
178             catch (IOException e)
179             {
180                e.printStackTrace();
181             }
182 */

183 // try
184
// {
185
// output.write(new StringBuffer().append(200).toString().getBytes());
186
// output.flush();
187
// }
188
// catch (IOException e)
189
// {
190
// e.printStackTrace(); //TODO: -TME Implement
191
// }
192

193          }
194          clients.remove(this);
195       }
196    }
197
198    private final class Listener extends Thread JavaDoc
199    {
200       public Listener()
201       {
202          super("RawHTTPServer-Listener");
203          //setDaemon(true);
204
setDaemon(false);
205       }
206
207       public void run()
208       {
209          while(started)
210          {
211             try
212             {
213                // blocks until a new client arrives
214
Socket JavaDoc client = socket.accept();
215                if(client != null)
216                {
217                   // make this a thread pool task
218
new Client(client).start();
219                }
220             }
221             catch(Exception JavaDoc ex)
222             {
223                if(started)
224                {
225                   ex.printStackTrace();
226                }
227             }
228          }
229       }
230    }
231
232
233    public static void main(String JavaDoc[] 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