1 package jalview.httpserver;
3 import java.io.IOException;
4 import java.net.BindException;
5 import java.util.Collections;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import org.eclipse.jetty.server.Request;
12 import org.eclipse.jetty.server.handler.AbstractHandler;
19 public abstract class AbstractRequestHandler extends AbstractHandler
23 * The relative path (below context root) of this handler (without /
29 * The full URI on which this handler listens
34 * Handle an incoming Http request.
37 public void handle(String target, Request baseRequest,
38 HttpServletRequest request, HttpServletResponse response)
39 throws IOException, ServletException
43 // dumpRequest(request); // debug
44 processRequest(request, response);
48 * Set server error status on response
50 System.err.println("Exception handling request "
51 + request.getRequestURI() + " : " + t.getMessage());
52 if (response.isCommitted())
55 * Can't write an HTTP header once any response content has been written
58 .println("Unable to return HTTP 500 as response already committed");
62 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
66 response.getWriter().flush();
67 baseRequest.setHandled(true);
72 * Subclasses should override this method to perform request processing
78 protected abstract void processRequest(HttpServletRequest request,
79 HttpServletResponse response) throws IOException;
82 * For debug - writes HTTP request details to stdout
86 protected void dumpRequest(HttpServletRequest request)
88 System.out.println(request.getMethod());
89 System.out.println(request.getRequestURL());
90 for (String hdr : Collections.list(request.getHeaderNames()))
92 for (String val : Collections.list(request.getHeaders(hdr)))
94 System.out.println(hdr + ": " + val);
97 for (String param : Collections.list(request.getParameterNames()))
99 for (String val : request.getParameterValues(param))
101 System.out.println(param + "=" + val);
107 * Returns a display name for the handler
111 public abstract String getName();
114 * Deregister this listener and close it down
118 public void shutdown()
122 HttpServer.getInstance().removeHandler(this);
124 } catch (Exception e)
126 System.err.println("Error stopping " + getName() + ": "
132 * Returns the URI on which we are listening
136 public String getUri()
142 * Set the URI to this handler
146 protected void setUri(String u)
152 * Sets the relative path to this handler - do this before registering the
157 protected void setPath(String p)
163 * Returns the relative path to this handler below the context root (without /
168 public String getPath()
174 * Registers the handler with the HttpServer and reports its URI on stdout
176 * @throws BindException
177 * if no port could be allocated
178 * @throws IllegalStateException
179 * if this method is called before {@link #setPath}
181 protected void registerHandler() throws BindException
183 HttpServer.getInstance().registerHandler(this);