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