1 package jalview.httpserver;
3 import java.net.BindException;
5 import java.util.Collections;
6 import java.util.HashMap;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import org.eclipse.jetty.server.Handler;
13 import org.eclipse.jetty.server.Server;
14 import org.eclipse.jetty.server.ServerConnector;
15 import org.eclipse.jetty.server.handler.ContextHandler;
16 import org.eclipse.jetty.server.handler.HandlerCollection;
17 import org.eclipse.jetty.util.thread.QueuedThreadPool;
20 * An HttpServer built on Jetty. To use it
22 * <li>call getInstance() to create and start the server</li>
23 * <li>call registerHandler to add a handler for a path (below /jalview)</li>
24 * <li>when finished, call removedHandler</li>
28 * @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html
30 public class HttpServer
33 * 'context root' - actually just prefixed to the path for each handler for
34 * now - see registerHandler
36 private static final String JALVIEW_PATH = "jalview";
39 * Singleton instance of this server
41 private static HttpServer instance;
46 private Server server;
49 * Registered handlers for context paths
51 private HandlerCollection contextHandlers;
54 * Lookup of ContextHandler by its wrapped handler
56 Map<Handler, ContextHandler> myHandlers = new HashMap<Handler, ContextHandler>();
59 * The context root for the server
61 private URI contextRoot;
64 * Returns the singleton instance of this class.
67 * @throws BindException
69 public static HttpServer getInstance() throws BindException
71 synchronized (HttpServer.class)
73 if (instance == null) {
74 instance = new HttpServer();
81 * Private constructor to enforce use of singleton
83 * @throws BindException
84 * if no free port can be assigned
86 private HttpServer() throws BindException
92 * Start the http server
94 * @throws BindException
96 private void startServer() throws BindException
101 * Create a server with a small number of threads; jetty will allocate a
104 QueuedThreadPool tp = new QueuedThreadPool(4, 1); // max, min
105 server = new Server(tp);
106 // 2 selector threads to handle incoming connections
107 ServerConnector connector = new ServerConnector(server, 0, 2);
108 server.addConnector(connector);
111 * HttpServer shuts down with Jalview process
113 server.setStopAtShutdown(true);
116 * Create a mutable set of handlers (can add handlers while the server is
117 * running). Using vanilla handlers here rather than servlets
119 // TODO how to properly configure context root "/jalview"
120 contextHandlers = new HandlerCollection(true);
121 server.setHandler(contextHandlers);
123 // System.out.println(String.format(
124 // "HttpServer started with %d threads", server.getThreadPool()
126 contextRoot = server.getURI();
127 System.out.println("Jalview endpoint " + contextRoot);
128 } catch (Exception e)
130 System.err.println("Error trying to start HttpServer: "
135 } catch (Exception e1)
137 e1.printStackTrace();
142 throw new BindException("HttpServer failed to allocate a port");
147 * Returns the URI on which we are listening
153 return server == null ? null : server.getURI();
157 * For debug - write HTTP request details to stdout
162 protected void dumpRequest(HttpServletRequest request,
163 HttpServletResponse response)
165 for (String hdr : Collections.list(request.getHeaderNames()))
167 for (String val : Collections.list(request.getHeaders(hdr)))
169 System.out.println(hdr + ": " + val);
172 for (String param : Collections.list(request.getParameterNames()))
174 for (String val : request.getParameterValues(param))
176 System.out.println(param + "=" + val);
182 * Stop the Http server.
184 public void stopServer()
188 if (server.isStarted())
193 } catch (Exception e)
195 System.err.println("Error stopping Http Server on "
196 + server.getURI() + ": " + e.getMessage());
203 * Register a handler for the given path and returns its URI
206 * a path below the context root (without leading or trailing
211 public String registerHandler(String path, AbstractRequestHandler handler)
213 // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
214 ContextHandler ch = new ContextHandler();
215 ch.setAllowNullPathInfo(true);
216 ch.setContextPath("/" + JALVIEW_PATH + "/" + path);
217 ch.setResourceBase(".");
218 ch.setClassLoader(Thread.currentThread()
219 .getContextClassLoader());
220 ch.setHandler(handler);
223 * Remember the association so we can remove it later
225 this.myHandlers.put(handler, ch);
228 * A handler added to a running server must be started explicitly
230 contextHandlers.addHandler(ch);
234 } catch (Exception e)
236 System.err.println("Error starting handler for " + path + ": "
240 return this.contextRoot + ch.getContextPath().substring(1);
244 * Removes the handler from the server; more precisely, remove the
245 * ContextHandler wrapping the specified handler
249 public void removeHandler(Handler handler)
252 * Have to use this cached lookup table since there is no method
253 * ContextHandler.getHandler()
255 ContextHandler ch = myHandlers.get(handler);
258 contextHandlers.removeHandler(ch);
259 myHandlers.remove(handler);