~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/http/RawHTTPServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
 
 
23
package org.jboss.test.remoting.transport.http;
 
24
 
 
25
import java.io.BufferedInputStream;
 
26
import java.io.BufferedOutputStream;
 
27
import java.io.IOException;
 
28
import java.io.InputStream;
 
29
import java.io.OutputStream;
 
30
import java.net.ServerSocket;
 
31
import java.net.Socket;
 
32
import java.util.ArrayList;
 
33
import java.util.Collections;
 
34
import java.util.Iterator;
 
35
import java.util.List;
 
36
 
 
37
/**
 
38
 * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
 
39
 */
 
40
public class RawHTTPServer
 
41
{
 
42
   private ServerSocket socket;
 
43
   private int port;
 
44
   private boolean started = false;
 
45
   private List clients = Collections.synchronizedList(new ArrayList());
 
46
 
 
47
   private static int clientCount = 0;
 
48
 
 
49
   private static final int DEFAULT_PORT = 80;
 
50
   private Listener listener;
 
51
 
 
52
   public RawHTTPServer()
 
53
   {
 
54
      this(DEFAULT_PORT);
 
55
   }
 
56
 
 
57
   public RawHTTPServer(int port)
 
58
   {
 
59
      this.port = port;
 
60
   }
 
61
 
 
62
   public void start()
 
63
   {
 
64
      if(!started)
 
65
      {
 
66
         System.out.println("Starting raw http server.");
 
67
         try
 
68
         {
 
69
            this.socket = new ServerSocket(this.port);
 
70
            this.port = socket.getLocalPort();
 
71
            this.started = true;
 
72
            this.listener = new Listener();
 
73
            this.listener.start();
 
74
            System.out.println("Server started.");
 
75
         }
 
76
         catch(IOException e)
 
77
         {
 
78
            System.out.println("Server start failed.");
 
79
            e.printStackTrace();
 
80
         }
 
81
      }
 
82
   }
 
83
 
 
84
   public synchronized void stop()
 
85
   {
 
86
      if(started)
 
87
      {
 
88
         this.started = false;
 
89
         this.listener.interrupt();
 
90
         try
 
91
         {
 
92
            this.listener.join(500);
 
93
         }
 
94
         catch(Throwable ig)
 
95
         {
 
96
         }
 
97
         Iterator iter = clients.iterator();
 
98
         while(iter.hasNext())
 
99
         {
 
100
            Client client = (Client) iter.next();
 
101
            client.interrupt();
 
102
         }
 
103
         clients.clear();
 
104
         try
 
105
         {
 
106
            this.socket.close();
 
107
         }
 
108
         catch(Throwable ig)
 
109
         {
 
110
         }
 
111
         this.socket = null;
 
112
         this.listener = null;
 
113
      }
 
114
   }
 
115
 
 
116
 
 
117
   private final class Client extends Thread
 
118
   {
 
119
      Socket socket;
 
120
      InputStream input;
 
121
      OutputStream output;
 
122
      String MIMEType = "text/html";
 
123
      String content = "<HTML><BODY>This is test results page from RawHTTPServer.</BODY></HTML>";
 
124
      String header = "HTTP 1.0 200 OK\r\n" +
 
125
                      "Server: RawHTTPServer\r\n" +
 
126
                      "Content-length: " + this.content.getBytes().length + "\r\n" +
 
127
                      "Content-type: " + MIMEType + "\r\n\rn\n";
 
128
 
 
129
 
 
130
      Client(Socket socket)
 
131
            throws IOException
 
132
      {
 
133
         super("RawHTTPServer-Client [" + (++clientCount) + "]");
 
134
         setDaemon(true);
 
135
         this.socket = socket;
 
136
         this.input = new BufferedInputStream(socket.getInputStream());
 
137
         this.output = new BufferedOutputStream(socket.getOutputStream());
 
138
         clients.add(this);
 
139
      }
 
140
 
 
141
      public void run()
 
142
      {
 
143
         while(started)
 
144
         {
 
145
            try
 
146
            {
 
147
               StringBuffer request = new StringBuffer(80);
 
148
               while(true)
 
149
               {
 
150
                  int c = input.read();
 
151
                  if(c == '\r' || c == '\n' || c == -1)
 
152
                  {
 
153
                     break;
 
154
                  }
 
155
                  request.append((char) c);
 
156
               }
 
157
               if(request.toString().indexOf("HTTP/") != -1)
 
158
               {
 
159
                  output.write(this.header.getBytes());
 
160
               }
 
161
               output.write(this.content.getBytes());
 
162
               output.flush();
 
163
            }
 
164
            catch(IOException e)
 
165
            {
 
166
               e.printStackTrace();
 
167
            }
 
168
            finally
 
169
            {
 
170
               if(socket != null)
 
171
               {
 
172
                  try
 
173
                  {
 
174
                     socket.close();
 
175
                  }
 
176
                  catch(IOException e)
 
177
                  {
 
178
                     e.printStackTrace();  //TODO: -TME Implement
 
179
                  }
 
180
               }
 
181
            }
 
182
 
 
183
/*
 
184
            try
 
185
            {
 
186
               int n = 0;
 
187
               byte[] buffer = new byte[1024];
 
188
               while (n != -1 || input.available() > 0)
 
189
               {
 
190
                  n = input.read(buffer);
 
191
                  System.out.println(new String(buffer));
 
192
               }
 
193
            }
 
194
            catch (IOException e)
 
195
            {
 
196
               e.printStackTrace();
 
197
            }
 
198
*/
 
199
//            try
 
200
//            {
 
201
//               output.write(new StringBuffer().append(200).toString().getBytes());
 
202
//               output.flush();
 
203
//            }
 
204
//            catch (IOException e)
 
205
//            {
 
206
//               e.printStackTrace();  //TODO: -TME Implement
 
207
//            }
 
208
 
 
209
         }
 
210
         clients.remove(this);
 
211
      }
 
212
   }
 
213
 
 
214
   private final class Listener extends Thread
 
215
   {
 
216
      public Listener()
 
217
      {
 
218
         super("RawHTTPServer-Listener");
 
219
         //setDaemon(true);
 
220
         setDaemon(false);
 
221
      }
 
222
 
 
223
      public void run()
 
224
      {
 
225
         while(started)
 
226
         {
 
227
            try
 
228
            {
 
229
               // blocks until a new client arrives
 
230
               Socket client = socket.accept();
 
231
               if(client != null)
 
232
               {
 
233
                  // make this a thread pool task
 
234
                  new Client(client).start();
 
235
               }
 
236
            }
 
237
            catch(Exception ex)
 
238
            {
 
239
               if(started)
 
240
               {
 
241
                  ex.printStackTrace();
 
242
               }
 
243
            }
 
244
         }
 
245
      }
 
246
   }
 
247
 
 
248
 
 
249
   public static void main(String[] args)
 
250
   {
 
251
      int port = RawHTTPServer.DEFAULT_PORT;
 
252
      if(args != null && args.length > 0)
 
253
      {
 
254
         port = Integer.parseInt(args[0]);
 
255
      }
 
256
      RawHTTPServer server = new RawHTTPServer(port);
 
257
      server.start();
 
258
   }
 
259
 
 
260
}
 
 
b'\\ No newline at end of file'