JAL-3253 preliminary static fixes for JavaScript part 3 of 3
[jalview.git] / src / jalview / httpserver / HttpServer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.httpserver;
22
23 import jalview.bin.Jalview;
24 import jalview.rest.RestHandler;
25
26 import java.net.BindException;
27 import java.net.URI;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.eclipse.jetty.server.Handler;
36 import org.eclipse.jetty.server.Server;
37 import org.eclipse.jetty.server.ServerConnector;
38 import org.eclipse.jetty.server.handler.ContextHandler;
39 import org.eclipse.jetty.server.handler.HandlerCollection;
40 import org.eclipse.jetty.util.thread.QueuedThreadPool;
41
42 /**
43  * An HttpServer built on Jetty. To use it
44  * <ul>
45  * <li>call getInstance() to create and start the server</li>
46  * <li>call registerHandler to add a handler for a path (below /jalview)</li>
47  * <li>when finished, call removedHandler</li>
48  * </ul>
49  * 
50  * @author gmcarstairs
51  * @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html
52  */
53 public class HttpServer
54 {
55   /*
56    * 'context root' - actually just prefixed to the path for each handler for
57    * now - see registerHandler
58    */
59   private static final String JALVIEW_PATH = "jalview";
60
61   /**
62    * Returns the singleton instance of this class.
63    * 
64    * @return
65    * @throws BindException
66    */
67   public static HttpServer getInstance() throws BindException
68   {
69     synchronized (HttpServer.class)
70     {
71       Jalview j = Jalview.getInstance();
72       return (j.httpServer == null ? j.httpServer = new HttpServer()
73               : j.httpServer);
74     }
75   }
76
77   /*
78    * The Http server
79    */
80   private Server server;
81
82   /*
83    * Registered handlers for context paths
84    */
85   private HandlerCollection contextHandlers;
86
87   /*
88    * Lookup of ContextHandler by its wrapped handler
89    */
90   Map<Handler, ContextHandler> myHandlers = new HashMap<>();
91
92   /*
93    * The context root for the server
94    */
95   private URI contextRoot;
96
97   /**
98    * Private constructor to enforce use of singleton
99    * 
100    * @throws BindException
101    *           if no free port can be assigned
102    */
103   private HttpServer() throws BindException
104   {
105     startServer();
106
107     /*
108      * Provides a REST server by default; add more programmatically as required
109      */
110     registerHandler(RestHandler.getInstance());
111   }
112
113   /**
114    * Start the http server
115    * 
116    * @throws BindException
117    */
118   private void startServer() throws BindException
119   {
120     try
121     {
122       /*
123        * Create a server with a small number of threads; jetty will allocate a
124        * free port
125        */
126       QueuedThreadPool tp = new QueuedThreadPool(4, 1); // max, min
127       server = new Server(tp);
128       // 2 selector threads to handle incoming connections
129       ServerConnector connector = new ServerConnector(server, 0, 2);
130       // restrict to localhost
131       connector.setHost("localhost");
132       server.addConnector(connector);
133
134       /*
135        * HttpServer shuts down with Jalview process
136        */
137       server.setStopAtShutdown(true);
138
139       /*
140        * Create a mutable set of handlers (can add handlers while the server is
141        * running). Using vanilla handlers here rather than servlets
142        */
143       // TODO how to properly configure context root "/jalview"
144       contextHandlers = new HandlerCollection(true);
145       server.setHandler(contextHandlers);
146       server.start();
147       // System.out.println(String.format(
148       // "HttpServer started with %d threads", server.getThreadPool()
149       // .getThreads()));
150       contextRoot = server.getURI();
151     } catch (Exception e)
152     {
153       System.err.println(
154               "Error trying to start HttpServer: " + e.getMessage());
155       try
156       {
157         server.stop();
158       } catch (Exception e1)
159       {
160         e1.printStackTrace();
161       }
162     }
163     if (server == null)
164     {
165       throw new BindException("HttpServer failed to allocate a port");
166     }
167   }
168
169   /**
170    * Returns the URI on which we are listening
171    * 
172    * @return
173    */
174   public URI getUri()
175   {
176     return server == null ? null : server.getURI();
177   }
178
179   /**
180    * For debug - write HTTP request details to stdout
181    * 
182    * @param request
183    * @param response
184    */
185   protected void dumpRequest(HttpServletRequest request,
186           HttpServletResponse response)
187   {
188     for (String hdr : Collections.list(request.getHeaderNames()))
189     {
190       for (String val : Collections.list(request.getHeaders(hdr)))
191       {
192         System.out.println(hdr + ": " + val);
193       }
194     }
195     for (String param : Collections.list(request.getParameterNames()))
196     {
197       for (String val : request.getParameterValues(param))
198       {
199         System.out.println(param + "=" + val);
200       }
201     }
202   }
203
204   /**
205    * Stop the Http server.
206    */
207   public void stopServer()
208   {
209     if (server != null)
210     {
211       if (server.isStarted())
212       {
213         try
214         {
215           server.stop();
216         } catch (Exception e)
217         {
218           System.err.println("Error stopping Http Server on "
219                   + server.getURI() + ": " + e.getMessage());
220         }
221       }
222     }
223   }
224
225   /**
226    * Register a handler for the given path and set its URI
227    * 
228    * @param handler
229    * @return
230    * @throws IllegalStateException
231    *           if handler path has not been set
232    */
233   public void registerHandler(AbstractRequestHandler handler)
234   {
235     String path = handler.getPath();
236     if (path == null)
237     {
238       throw new IllegalStateException(
239               "Must set handler path before registering handler");
240     }
241
242     // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
243     ContextHandler ch = new ContextHandler();
244     ch.setAllowNullPathInfo(true);
245     ch.setContextPath("/" + JALVIEW_PATH + "/" + path);
246     ch.setResourceBase(".");
247     ch.setClassLoader(Thread.currentThread().getContextClassLoader());
248     ch.setHandler(handler);
249
250     /*
251      * Remember the association so we can remove it later
252      */
253     this.myHandlers.put(handler, ch);
254
255     /*
256      * A handler added to a running server must be started explicitly
257      */
258     contextHandlers.addHandler(ch);
259     try
260     {
261       ch.start();
262     } catch (Exception e)
263     {
264       System.err.println(
265               "Error starting handler for " + path + ": " + e.getMessage());
266     }
267
268     handler.setUri(this.contextRoot + ch.getContextPath().substring(1));
269     System.out.println("Jalview " + handler.getName()
270             + " handler started on " + handler.getUri());
271   }
272
273   /**
274    * Removes the handler from the server; more precisely, remove the
275    * ContextHandler wrapping the specified handler
276    * 
277    * @param handler
278    */
279   public void removeHandler(AbstractRequestHandler handler)
280   {
281     /*
282      * Have to use this cached lookup table since there is no method
283      * ContextHandler.getHandler()
284      */
285     ContextHandler ch = myHandlers.get(handler);
286     if (ch != null)
287     {
288       contextHandlers.removeHandler(ch);
289       myHandlers.remove(handler);
290       System.out.println("Stopped Jalview " + handler.getName()
291               + " handler on " + handler.getUri());
292     }
293   }
294 }