View Javadoc

1   package com.atlassian.webdriver.it;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.apache.commons.lang.Validate;
5   import org.mortbay.jetty.Handler;
6   import org.mortbay.jetty.Request;
7   import org.mortbay.jetty.Server;
8   import org.mortbay.jetty.handler.AbstractHandler;
9   
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.net.ServerSocket;
13  import java.util.HashMap;
14  import java.util.Map;
15  import javax.servlet.ServletException;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  /**
20   * Simple server for serving up html pages.
21   */
22  public class FileBasedServer
23  {
24      private int port = 0;
25      private Server server = null;
26  
27      /**
28       * Main method to run the server standalone.
29       *
30       * @param args program arguments
31       * @throws Exception any exception
32       */
33      public static void main(String... args) throws Exception
34      {
35          FileBasedServer server = new FileBasedServer(5555);
36          server.startServer();
37          int port = server.getPort();
38          System.out.println("Server started: " + "http://localhost:" + port);
39      }
40  
41      private final Map<String,String> urlMappings;
42  
43      public FileBasedServer(int port)
44      {
45          this(new HashMap<String, String>(), port);
46      }
47  
48      public FileBasedServer()
49      {
50          this(new HashMap<String, String>());
51      }
52  
53      public FileBasedServer(Map<String, String> urlMappings) {
54          checkPort();
55          this.urlMappings = urlMappings;
56      }
57  
58      public FileBasedServer(Map<String, String> urlMappings, int port)
59      {
60          Validate.isTrue(port > 0, "Port must be a positive number");
61          this.port = port;
62          this.urlMappings = urlMappings;
63  
64          checkPort();
65      }
66  
67      public void stopServer() throws Exception
68      {
69          if (server != null)
70          {
71              server.stop();
72          }
73      }
74  
75      public void startServer() throws Exception
76      {
77          Handler handler=new AbstractHandler()
78          {
79              public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
80                  throws IOException, ServletException
81              {
82  
83                  String uri = request.getRequestURI();
84  
85                  if(uri.endsWith("css"))
86                  {
87                      response.setContentType("text/css");
88                  }
89                  else  if(uri.endsWith("js"))
90                  {
91                      response.setContentType("application/javascript");
92                  }
93                  else
94                  {
95                      response.setContentType("text/html");                    
96                  }
97  
98  
99                  InputStream inputStream = null;
100                 inputStream = getClass().getClassLoader().getResourceAsStream(uri.substring(1));
101 
102                 if(inputStream == null && urlMappings.containsKey(uri))
103                 {
104                     String filename = urlMappings.get(uri);
105                     inputStream = getClass().getClassLoader().getResourceAsStream(filename);
106                 }
107 
108                 if(inputStream != null)
109                 {
110                     String contents = IOUtils.toString(inputStream);
111 
112                     if (contents != null)
113                     {
114                         response.getWriter().print(contents);
115                     }
116                     else
117                     {
118                         response.getWriter().println("<h1>Cannot read file at: " + uri + "</h1>");
119                     }
120                 }
121 
122                 else
123                 {
124                     response.getWriter().println("<h1>File not found at: " + uri + "</h1>");
125                 }
126 
127                 response.setStatus(HttpServletResponse.SC_OK);
128 
129                 ((Request)request).setHandled(true);
130             }
131         };
132 
133         server = new Server(port);
134         server.setHandler(handler);
135         server.start();
136     }
137 
138     public int getPort()
139     {
140         return port;
141     }
142 
143     private void checkPort()
144     {
145         ServerSocket socket = null;
146         try
147         {
148             socket = new ServerSocket(port);
149             this.port = socket.getLocalPort();
150             return;
151         }
152         catch (IOException e)
153         {
154             throw new RuntimeException("Error opening socket, port: " + port + " may already be in use", e);
155         }
156         finally
157         {
158             if (socket != null)
159             {
160                 try
161                 {
162                     socket.close();
163                 }
164                 catch (IOException e)
165                 {
166                     throw new RuntimeException("Error closing sockets", e);
167                 }
168             }
169         }
170     }
171 
172 }