2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.httpserver;
23 import java.io.IOException;
24 import java.net.BindException;
25 import java.util.Collections;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.eclipse.jetty.server.Request;
32 import org.eclipse.jetty.server.handler.AbstractHandler;
34 import jalview.bin.Cache;
41 public abstract class AbstractRequestHandler extends AbstractHandler
45 * The relative path (below context root) of this handler (without /
51 * The full URI on which this handler listens
56 * Handle an incoming Http request.
59 public void handle(String target, Request baseRequest,
60 HttpServletRequest request, HttpServletResponse response)
61 throws IOException, ServletException
65 // dumpRequest(request); // debug
66 processRequest(request, response);
70 * Set server error status on response
72 System.err.println("Exception handling request "
73 + request.getRequestURI() + " : " + t.getMessage());
75 if (response.isCommitted())
78 * Can't write an HTTP header once any response content has been written
81 "Unable to return HTTP 500 as response already committed");
85 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
89 response.getWriter().flush();
90 baseRequest.setHandled(true);
95 * Subclasses should override this method to perform request processing
101 protected abstract void processRequest(HttpServletRequest request,
102 HttpServletResponse response) throws IOException;
105 * For debug - writes HTTP request details to stdout
109 protected void dumpRequest(HttpServletRequest request)
111 System.out.println(request.getMethod());
112 System.out.println(request.getRequestURL());
113 for (String hdr : Collections.list(request.getHeaderNames()))
115 for (String val : Collections.list(request.getHeaders(hdr)))
117 System.out.println(hdr + ": " + val);
120 for (String param : Collections.list(request.getParameterNames()))
122 for (String val : request.getParameterValues(param))
124 System.out.println(param + "=" + val);
130 * Returns a display name for the handler
134 public abstract String getName();
137 * Deregister this listener and close it down
141 public void shutdown()
145 HttpServer.getInstance().removeHandler(this);
147 } catch (Exception e)
150 "Error stopping " + getName() + ": " + e.getMessage());
155 * Returns the URI on which we are listening
159 public String getUri()
165 * Set the URI to this handler
169 protected void setUri(String u)
175 * Sets the relative path to this handler - do this before registering the
180 protected void setPath(String p)
186 * Returns the relative path to this handler below the context root (without /
191 public String getPath()
197 * Registers the handler with the HttpServer and reports its URI on stdout
199 * @throws BindException
200 * if no port could be allocated
201 * @throws IllegalStateException
202 * if this method is called before {@link #setPath}
204 protected void registerHandler() throws BindException
206 HttpServer.getInstance().registerHandler(this);