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