KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.net.ServerSocket JavaDoc;
18 import java.net.Socket JavaDoc;
19 import java.util.Timer JavaDoc;
20 import java.util.TimerTask JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
24  */

25 public class SimpleHTTPServer extends Thread JavaDoc
26 {
27    private byte[] content;
28    private byte[] header;
29    private int port = 80;
30    private static boolean timeout = false;
31
32    public SimpleHTTPServer(String JavaDoc data, String JavaDoc encoding, String JavaDoc MIMEType, int port)
33          throws UnsupportedEncodingException JavaDoc
34    {
35       this(data.getBytes(encoding), encoding, MIMEType, port);
36    }
37
38    public SimpleHTTPServer(byte[] data, String JavaDoc encoding, String JavaDoc MIMEType, int port)
39          throws UnsupportedEncodingException JavaDoc
40    {
41       this.content = data;
42       this.port = port;
43       String JavaDoc header = "HTTP/1.1 200 OK\r\n" +
44                       "Server: SimpleHTTPServer 1.0\r\n" +
45                       "Content-length: " + this.content.length + "\r\n" +
46                       "Content-type: " + MIMEType + "\r\n\r\n";
47       this.header = header.getBytes("ASCII");
48    }
49
50
51    protected void startTimer(Thread JavaDoc currentThread)
52    {
53       Timer JavaDoc timer = new Timer JavaDoc(false);
54       timer.schedule(new TimeoutTimerTask(currentThread), 3000);
55    }
56
57    public void run()
58    {
59       try
60       {
61          ServerSocket JavaDoc server = new ServerSocket JavaDoc(this.port);
62          System.out.println("Accepting connections on port " + server.getLocalPort());
63          System.out.println("Data to be sent: ");
64          System.out.write(this.content);
65
66          while(true)
67          {
68             Socket JavaDoc connection = null;
69             try
70             {
71                connection = server.accept();
72                OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(connection.getOutputStream());
73                InputStream JavaDoc in = new BufferedInputStream JavaDoc(connection.getInputStream());
74
75                //startTimer(Thread.currentThread());
76

77                StringBuffer JavaDoc request = new StringBuffer JavaDoc(80);
78                try
79                {
80                   int c = in.read();
81                   while(c != -1 && !timeout)
82                   {
83                      int q = 0;
84                      if(request.length() > 0)
85                      {
86                         q = request.charAt(request.length() - 1);
87                      }
88                      request.append((char) c);
89                      //System.out.println(request);
90
int n = in.read();
91
92                      if(c == '\r' && n == '\n')
93                      {
94                         if(q == '\n')
95                         {
96                            break;
97                         }
98                         else
99                         {
100                            c = n;
101                         }
102                      }
103                      else
104                      {
105                         c = n;
106                      }
107                      //Thread.sleep(50);
108
}
109                }
110                catch(Exception JavaDoc e)
111                {
112                   e.printStackTrace();
113                }
114                System.out.println("\n\nHTTP Request:\n\n" + request + "\n\n");
115
116                StringBuffer JavaDoc requestHeader = new StringBuffer JavaDoc(80);
117                for(int x = 0; x < request.length(); x++)
118                {
119                   char cr = request.charAt(x);
120                   if(cr == '\r' || cr == '\n' || cr == -1)
121                   {
122                      break;
123                   }
124                   requestHeader.append(cr);
125                }
126                if(requestHeader.toString().indexOf("HTTP/") != -1)
127                {
128                   out.write(this.header);
129                }
130                out.write(this.content);
131                out.flush();
132             }
133             catch(IOException JavaDoc e)
134             {
135             }
136             finally
137             {
138                if(connection != null)
139                {
140                   connection.close();
141                }
142             }
143          }
144       }
145       catch(IOException JavaDoc e)
146       {
147          System.err.println("Could not start server. Port " + this.port + " occupied.");
148       }
149
150    }
151
152    public class TimeoutTimerTask extends TimerTask JavaDoc
153    {
154       private Thread JavaDoc curThread;
155
156       public TimeoutTimerTask(Thread JavaDoc current)
157       {
158          this.curThread = current;
159       }
160
161       public void run()
162       {
163          timeout = true;
164          curThread.interrupt();
165       }
166    }
167
168    public static void main(String JavaDoc[] args)
169    {
170       try
171       {
172          String JavaDoc contentType = "text/plain";
173          if(args[0].endsWith(".html") || args[0].endsWith(".htm"))
174          {
175             contentType = "text/html";
176          }
177
178          InputStream JavaDoc in = new FileInputStream JavaDoc(args[0]);
179          ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
180          int b;
181          while((b = in.read()) != -1)
182          {
183             out.write(b);
184          }
185          byte[] data = out.toByteArray();
186
187          int port;
188          try
189          {
190             port = Integer.parseInt(args[1]);
191             if(port < 1 || port > 65535)
192             {
193                port = 80;
194             }
195          }
196          catch(Exception JavaDoc e)
197          {
198             port = 80;
199          }
200
201          String JavaDoc encoding = "ASCII";
202          if(args.length >= 2)
203          {
204             encoding = args[2];
205          }
206          Thread JavaDoc t = new SimpleHTTPServer(data, encoding, contentType, port);
207          t.start();
208       }
209       catch(ArrayIndexOutOfBoundsException JavaDoc e)
210       {
211          System.out.println("Usage: java SimpleHTTPServer filename port encoding");
212       }
213       catch(Exception JavaDoc e)
214       {
215          System.err.println(e);
216       }
217    }
218 }
Popular Tags