JAL-3253 jalview.bin.Instance streamlining
[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.Instance;
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   /**
57    * Returns the singleton instance of this class.
58    * 
59    * @return
60    * @throws BindException
61    */
62   public static HttpServer getInstance() throws BindException
63   {
64     synchronized (HttpServer.class)
65     {
66       Instance j = Instance.getInstance();
67       return (j.httpServer == null ? j.httpServer = new HttpServer()
68               : j.httpServer);
69     }
70   }
71
72   /**
73    * Private constructor to enforce use of singleton; use getInstance().
74    * 
75    * @throws BindException
76    *           if no free port can be assigned
77    */
78   private HttpServer() throws BindException
79   {
80     // use getInstance()
81
82     startServer();
83
84     /*
85      * Provides a REST server by default; add more programmatically as required
86      */
87     registerHandler(RestHandler.getInstance());
88   }
89
90   /*
91    * 'context root' - actually just prefixed to the path for each handler for
92    * now - see registerHandler
93    */
94   private static final String JALVIEW_PATH = "jalview";
95
96   /*
97    * The Http server
98    */
99   private Server server;
100
101   /*
102    * Registered handlers for context paths
103    */
104   private HandlerCollection contextHandlers;
105
106   /*
107    * Lookup of ContextHandler by its wrapped handler
108    */
109   Map<Handler, ContextHandler> myHandlers = new HashMap<>();
110
111   /*
112    * The context root for the server
113    */
114   private URI contextRoot;
115
116   /**
117    * Start the http server
118    * 
119    * @throws BindException
120    */
121   private void startServer() throws BindException
122   {
123     try
124     {
125       /*
126        * Create a server with a small number of threads; jetty will allocate a
127        * free port
128        */
129       QueuedThreadPool tp = new QueuedThreadPool(4, 1); // max, min
130       server = new Server(tp);
131       // 2 selector threads to handle incoming connections
132       ServerConnector connector = new ServerConnector(server, 0, 2);
133       // restrict to localhost
134       connector.setHost("localhost");
135       server.addConnector(connector);
136
137       /*
138        * HttpServer shuts down with Jalview process
139        */
140       server.setStopAtShutdown(true);
141
142       /*
143        * Create a mutable set of handlers (can add handlers while the server is
144        * running). Using vanilla handlers here rather than servlets
145        */
146       // TODO how to properly configure context root "/jalview"
147       contextHandlers = new HandlerCollection(true);
148       server.setHandler(contextHandlers);
149       server.start();
150       // System.out.println(String.format(
151       // "HttpServer started with %d threads", server.getThreadPool()
152       // .getThreads()));
153       contextRoot = server.getURI();
154     } catch (Exception e)
155     {
156       System.err.println(
157               "Error trying to start HttpServer: " + e.getMessage());
158       try
159       {
160         server.stop();
161       } catch (Exception e1)
162       {
163         e1.printStackTrace();
164       }
165     }
166     if (server == null)
167     {
168       throw new BindException("HttpServer failed to allocate a port");
169     }
170   }
171
172   /**
173    * Returns the URI on which we are listening
174    * 
175    * @return
176    */
177   public URI getUri()
178   {
179     return server == null ? null : server.getURI();
180   }
181
182   /**
183    * For debug - write HTTP request details to stdout
184    * 
185    * @param request
186    * @param response
187    */
188   protected void dumpRequest(HttpServletRequest request,
189           HttpServletResponse response)
190   {
191     for (String hdr : Collections.list(request.getHeaderNames()))
192     {
193       for (String val : Collections.list(request.getHeaders(hdr)))
194       {
195         System.out.println(hdr + ": " + val);
196       }
197     }
198     for (String param : Collections.list(request.getParameterNames()))
199     {
200       for (String val : request.getParameterValues(param))
201       {
202         System.out.println(param + "=" + val);
203       }
204     }
205   }
206
207   /**
208    * Stop the Http server.
209    */
210   public void stopServer()
211   {
212     if (server != null)
213     {
214       if (server.isStarted())
215       {
216         try
217         {
218           server.stop();
219         } catch (Exception e)
220         {
221           System.err.println("Error stopping Http Server on "
222                   + server.getURI() + ": " + e.getMessage());
223         }
224       }
225     }
226   }
227
228   /**
229    * Register a handler for the given path and set its URI
230    * 
231    * @param handler
232    * @return
233    * @throws IllegalStateException
234    *           if handler path has not been set
235    */
236   public void registerHandler(AbstractRequestHandler handler)
237   {
238     String path = handler.getPath();
239     if (path == null)
240     {
241       throw new IllegalStateException(
242               "Must set handler path before registering handler");
243     }
244
245     // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
246     ContextHandler ch = new ContextHandler();
247     ch.setAllowNullPathInfo(true);
248     ch.setContextPath("/" + JALVIEW_PATH + "/" + path);
249     ch.setResourceBase(".");
250     ch.setClassLoader(Thread.currentThread().getContextClassLoader());
251     ch.setHandler(handler);
252
253     /*
254      * Remember the association so we can remove it later
255      */
256     this.myHandlers.put(handler, ch);
257
258     /*
259      * A handler added to a running server must be started explicitly
260      */
261     contextHandlers.addHandler(ch);
262     try
263     {
264       ch.start();
265     } catch (Exception e)
266     {
267       System.err.println(
268               "Error starting handler for " + path + ": " + e.getMessage());
269     }
270
271     handler.setUri(this.contextRoot + ch.getContextPath().substring(1));
272     System.out.println("Jalview " + handler.getName()
273             + " handler started on " + handler.getUri());
274   }
275
276   /**
277    * Removes the handler from the server; more precisely, remove the
278    * ContextHandler wrapping the specified handler
279    * 
280    * @param handler
281    */
282   public void removeHandler(AbstractRequestHandler handler)
283   {
284     /*
285      * Have to use this cached lookup table since there is no method
286      * ContextHandler.getHandler()
287      */
288     ContextHandler ch = myHandlers.get(handler);
289     if (ch != null)
290     {
291       contextHandlers.removeHandler(ch);
292       myHandlers.remove(handler);
293       System.out.println("Stopped Jalview " + handler.getName()
294               + " handler on " + handler.getUri());
295     }
296   }
297 }