1 package jalview.httpserver;
3 import jalview.rest.RestHandler;
5 import java.net.BindException;
7 import java.util.Collections;
8 import java.util.HashMap;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
14 import org.eclipse.jetty.server.Handler;
15 import org.eclipse.jetty.server.Server;
16 import org.eclipse.jetty.server.ServerConnector;
17 import org.eclipse.jetty.server.handler.ContextHandler;
18 import org.eclipse.jetty.server.handler.HandlerCollection;
19 import org.eclipse.jetty.util.thread.QueuedThreadPool;
22 * An HttpServer built on Jetty. To use it
24 * <li>call getInstance() to create and start the server</li>
25 * <li>call registerHandler to add a handler for a path (below /jalview)</li>
26 * <li>when finished, call removedHandler</li>
30 * @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html
32 public class HttpServer
35 * 'context root' - actually just prefixed to the path for each handler for
36 * now - see registerHandler
38 private static final String JALVIEW_PATH = "jalview";
41 * Singleton instance of this server
43 private static HttpServer instance;
48 private Server server;
51 * Registered handlers for context paths
53 private HandlerCollection contextHandlers;
56 * Lookup of ContextHandler by its wrapped handler
58 Map<Handler, ContextHandler> myHandlers = new HashMap<Handler, ContextHandler>();
61 * The context root for the server
63 private URI contextRoot;
66 * Returns the singleton instance of this class.
69 * @throws BindException
71 public static HttpServer getInstance() throws BindException
73 synchronized (HttpServer.class)
75 if (instance == null) {
76 instance = new HttpServer();
83 * Private constructor to enforce use of singleton
85 * @throws BindException
86 * if no free port can be assigned
88 private HttpServer() throws BindException
93 * Provides a REST server by default; add more programmatically as required
95 registerHandler(RestHandler.getInstance());
99 * Start the http server
101 * @throws BindException
103 private void startServer() throws BindException
108 * Create a server with a small number of threads; jetty will allocate a
111 QueuedThreadPool tp = new QueuedThreadPool(4, 1); // max, min
112 server = new Server(tp);
113 // 2 selector threads to handle incoming connections
114 ServerConnector connector = new ServerConnector(server, 0, 2);
115 // restrict to localhost
116 connector.setHost("localhost");
117 server.addConnector(connector);
120 * HttpServer shuts down with Jalview process
122 server.setStopAtShutdown(true);
125 * Create a mutable set of handlers (can add handlers while the server is
126 * running). Using vanilla handlers here rather than servlets
128 // TODO how to properly configure context root "/jalview"
129 contextHandlers = new HandlerCollection(true);
130 server.setHandler(contextHandlers);
132 // System.out.println(String.format(
133 // "HttpServer started with %d threads", server.getThreadPool()
135 contextRoot = server.getURI();
136 } catch (Exception e)
138 System.err.println("Error trying to start HttpServer: "
143 } catch (Exception e1)
145 e1.printStackTrace();
150 throw new BindException("HttpServer failed to allocate a port");
155 * Returns the URI on which we are listening
161 return server == null ? null : server.getURI();
165 * For debug - write HTTP request details to stdout
170 protected void dumpRequest(HttpServletRequest request,
171 HttpServletResponse response)
173 for (String hdr : Collections.list(request.getHeaderNames()))
175 for (String val : Collections.list(request.getHeaders(hdr)))
177 System.out.println(hdr + ": " + val);
180 for (String param : Collections.list(request.getParameterNames()))
182 for (String val : request.getParameterValues(param))
184 System.out.println(param + "=" + val);
190 * Stop the Http server.
192 public void stopServer()
196 if (server.isStarted())
201 } catch (Exception e)
203 System.err.println("Error stopping Http Server on "
204 + server.getURI() + ": " + e.getMessage());
211 * Register a handler for the given path and set its URI
215 * @throws IllegalStateException
216 * if handler path has not been set
218 public void registerHandler(AbstractRequestHandler handler)
220 String path = handler.getPath();
223 throw new IllegalStateException(
224 "Must set handler path before registering handler");
227 // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
228 ContextHandler ch = new ContextHandler();
229 ch.setAllowNullPathInfo(true);
230 ch.setContextPath("/" + JALVIEW_PATH + "/" + path);
231 ch.setResourceBase(".");
232 ch.setClassLoader(Thread.currentThread()
233 .getContextClassLoader());
234 ch.setHandler(handler);
237 * Remember the association so we can remove it later
239 this.myHandlers.put(handler, ch);
242 * A handler added to a running server must be started explicitly
244 contextHandlers.addHandler(ch);
248 } catch (Exception e)
250 System.err.println("Error starting handler for " + path + ": "
254 handler.setUri(this.contextRoot + ch.getContextPath().substring(1));
255 System.out.println("Jalview " + handler.getName()
256 + " handler started on " + handler.getUri());
260 * Removes the handler from the server; more precisely, remove the
261 * ContextHandler wrapping the specified handler
265 public void removeHandler(AbstractRequestHandler handler)
268 * Have to use this cached lookup table since there is no method
269 * ContextHandler.getHandler()
271 ContextHandler ch = myHandlers.get(handler);
274 contextHandlers.removeHandler(ch);
275 myHandlers.remove(handler);
276 System.out.println("Stopped Jalview " + handler.getName()
277 + " handler on " + handler.getUri());