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
77 protected abstract void processRequest(HttpServletRequest request,
78 HttpServletResponse response);
81 * For debug - writes HTTP request details to stdout
85 protected void dumpRequest(HttpServletRequest request)
87 System.out.println(request.getMethod());
88 System.out.println(request.getRequestURL());
89 for (String hdr : Collections.list(request.getHeaderNames()))
91 for (String val : Collections.list(request.getHeaders(hdr)))
93 System.out.println(hdr + ": " + val);
96 for (String param : Collections.list(request.getParameterNames()))
98 for (String val : request.getParameterValues(param))
100 System.out.println(param + "=" + val);
106 * Returns a display name for the handler
110 public abstract String getName();
113 * Deregister this listener and close it down
117 public void shutdown()
121 HttpServer.getInstance().removeHandler(this);
123 } catch (Exception e)
125 System.err.println("Error stopping " + getName() + ": "
131 * Returns the URI on which we are listening
135 public String getUri()
141 * Set the URI to this handler
145 protected void setUri(String u)
151 * Sets the relative path to this handler - do this before registering the
156 protected void setPath(String p)
162 * Returns the relative path to this handler below the context root (without /
167 public String getPath()
173 * Registers the handler with the HttpServer and reports its URI on stdout
175 * @throws BindException
176 * if no port could be allocated
177 * @throws IllegalStateException
178 * if this method is called before {@link #setPath}
180 protected void registerHandler() throws BindException
182 HttpServer.getInstance().registerHandler(this);