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.handler.ContextHandler;
15 import org.eclipse.jetty.server.handler.HandlerCollection;
16 import org.eclipse.jetty.util.thread.QueuedThreadPool;
19 * An HttpServer built on Jetty
22 * @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html
24 public class HttpServer
26 private static final String JALVIEW_PATH = "/jalview";
29 * Singleton instance of this server
31 private static HttpServer instance;
36 private Server server;
39 * Registered handlers for context paths
41 private HandlerCollection contextHandlers;
44 * Lookup of ContextHandler by its wrapped handler
46 Map<Handler, ContextHandler> myHandlers = new HashMap<Handler, ContextHandler>();
49 * The context root for the server
51 private URI contextRoot;
54 * Returns the singleton instance of this class.
57 * @throws BindException
59 public static HttpServer getInstance() throws BindException
61 synchronized (HttpServer.class)
63 if (instance == null) {
64 instance = new HttpServer();
71 * Private constructor to enforce use of singleton
73 * @throws BindException
74 * if no free port can be assigned
76 private HttpServer() throws BindException
82 * Start the http server with a default thread pool size of 1
84 * @throws BindException
86 private void startServer() throws BindException
90 // problem: how to both limit thread pool size and
91 // pick a random free port - two alternative constructors
92 QueuedThreadPool tp = new QueuedThreadPool(1, 1);
93 // server = new Server(tp); // ? fails with URI null
94 server = new Server(0); // pick a free port number
97 * HttpServer shuts down with Jalview process
99 server.setStopAtShutdown(true);
102 * Create a mutable set of handlers (can add handlers while the server is
105 // TODO how to combine this with context root "/jalview"
106 contextHandlers = new HandlerCollection(true);
107 server.setHandler(contextHandlers);
110 contextRoot = server.getURI();
111 System.out.println("Jalview endpoint " + contextRoot);
112 } catch (Exception e)
114 System.err.println("Error trying to start HttpServer: "
119 } catch (Exception e1)
121 e1.printStackTrace();
126 throw new BindException("HttpServer failed to allocate a port");
131 * Returns the URI on which we are listening
137 return server == null ? null : server.getURI();
141 * For debug - write HTTP request details to stdout
146 protected void dumpRequest(HttpServletRequest request,
147 HttpServletResponse response)
149 for (String hdr : Collections.list(request.getHeaderNames()))
151 for (String val : Collections.list(request.getHeaders(hdr)))
153 System.out.println(hdr + ": " + val);
156 for (String param : Collections.list(request.getParameterNames()))
158 for (String val : request.getParameterValues(param))
160 System.out.println(param + "=" + val);
166 * Stop the Http server.
168 public void stopServer()
172 if (server.isStarted())
177 } catch (Exception e)
179 System.err.println("Error stopping Http Server on "
180 + server.getURI() + ": " + e.getMessage());
187 * Register a handler for the given path and returns its URI
190 * a path below the context root (without leading or trailing
195 public String registerHandler(String path, AbstractRequestHandler handler)
197 // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
198 ContextHandler ch = new ContextHandler();
199 ch.setAllowNullPathInfo(true);
200 ch.setContextPath("/" + path);
201 ch.setResourceBase(".");
202 ch.setClassLoader(Thread.currentThread()
203 .getContextClassLoader());
204 ch.setHandler(handler);
207 * Remember the association so we can remove it later
209 this.myHandlers.put(handler, ch);
212 * A handler added to a running server must be started explicitly
214 contextHandlers.addHandler(ch);
218 } catch (Exception e)
220 System.err.println("Error starting handler for " + path + ": "
224 return this.contextRoot + ch.getContextPath().substring(1);
228 * Removes the handler from the server; more precisely, remove the
229 * ContextHandler wrapping the specified handler
233 public void removeHandler(Handler handler)
236 * Have to use this cached lookup table since there is no method
237 * ContextHandler.getHandler()
239 ContextHandler ch = myHandlers.get(handler);
242 contextHandlers.removeHandler(ch);
243 myHandlers.remove(handler);