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