View Javadoc

1   package com.atlassian.pageobjects.elements.test;
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 javax.servlet.ServletException;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.net.ServerSocket;
16  import java.util.HashMap;
17  import java.util.Map;
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          new FileBasedServer().startServer();
36      }
37  
38      private final Map<String,String> urlMappings;
39  
40      public FileBasedServer()
41      {
42          this(new HashMap<String, String>());
43      }
44  
45      public FileBasedServer(Map<String, String> urlMappings) {
46          checkPort();
47          this.urlMappings = urlMappings;
48      }
49  
50      public FileBasedServer(Map<String, String> urlMappings, int port)
51      {
52          Validate.isTrue(port > 0, "Port must be a positive number");
53          this.port = port;
54          this.urlMappings = urlMappings;
55  
56          checkPort();
57      }
58  
59      public void stopServer() throws Exception
60      {
61          if (server != null)
62          {
63              server.stop();
64          }
65      }
66  
67      public void startServer() throws Exception
68      {
69          Handler handler=new AbstractHandler()
70          {
71              public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
72                  throws IOException, ServletException
73              {
74  
75                  String uri = request.getRequestURI();
76  
77                  if(uri.endsWith("css"))
78                  {
79                      response.setContentType("text/css");
80                  }
81                  else  if(uri.endsWith("js"))
82                  {
83                      response.setContentType("application/javascript");
84                  }
85                  else
86                  {
87                      response.setContentType("text/html");                    
88                  }
89  
90  
91                  InputStream inputStream = null;
92                  inputStream = getClass().getClassLoader().getResourceAsStream(uri.substring(1));
93  
94                  if(inputStream == null && urlMappings.containsKey(uri))
95                  {
96                      String filename = urlMappings.get(uri);
97                      inputStream = getClass().getClassLoader().getResourceAsStream(filename);
98                  }
99  
100                 if(inputStream != null)
101                 {
102                     String contents = IOUtils.toString(inputStream);
103 
104                     if (contents != null)
105                     {
106                         response.getWriter().print(contents);
107                     }
108                     else
109                     {
110                         response.getWriter().println("<h1>Cannot read file at: " + uri + "</h1>");
111                     }
112                 }
113 
114                 else
115                 {
116                     response.getWriter().println("<h1>File not found at: " + uri + "</h1>");
117                 }
118 
119                 response.setStatus(HttpServletResponse.SC_OK);
120 
121                 ((Request)request).setHandled(true);
122             }
123         };
124 
125         server = new Server(port);
126         server.setHandler(handler);
127         server.start();
128     }
129 
130     public int getPort()
131     {
132         return port;
133     }
134 
135     private void checkPort()
136     {
137         ServerSocket socket = null;
138         try
139         {
140             socket = new ServerSocket(port);
141             this.port = socket.getLocalPort();
142             return;
143         }
144         catch (IOException e)
145         {
146             throw new RuntimeException("Error opening socket", e);
147         }
148         finally
149         {
150             if (socket != null)
151             {
152                 try
153                 {
154                     socket.close();
155                 }
156                 catch (IOException e)
157                 {
158                     throw new RuntimeException("Error closing socket", e);
159                 }
160             }
161         }
162     }
163 
164 }