X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fhttpserver%2FAbstractRequestHandler.java;h=8030c1d62a8e76d3677c7bc6868de0f480489746;hb=a4780f236ded1ae07824254bca59b2aa0ea539ba;hp=45064e7edaf215b9ef366c9a48358d35ab4e1a12;hpb=ab97cf4af61bd67f20676c686601522d0ba0875d;p=jalview.git diff --git a/src/jalview/httpserver/AbstractRequestHandler.java b/src/jalview/httpserver/AbstractRequestHandler.java index 45064e7..8030c1d 100644 --- a/src/jalview/httpserver/AbstractRequestHandler.java +++ b/src/jalview/httpserver/AbstractRequestHandler.java @@ -1,6 +1,7 @@ package jalview.httpserver; import java.io.IOException; +import java.net.BindException; import java.util.Collections; import javax.servlet.ServletException; @@ -17,6 +18,18 @@ import org.eclipse.jetty.server.handler.AbstractHandler; */ public abstract class AbstractRequestHandler extends AbstractHandler { + + /* + * The relative path (below context root) of this handler (without / + * separators) + */ + private String path; + + /* + * The full URI on which this handler listens + */ + private String uri; + /** * Handle an incoming Http request. */ @@ -60,9 +73,10 @@ public abstract class AbstractRequestHandler extends AbstractHandler * * @param request * @param response + * @throws IOException */ protected abstract void processRequest(HttpServletRequest request, - HttpServletResponse response); + HttpServletResponse response) throws IOException; /** * For debug - writes HTTP request details to stdout @@ -88,4 +102,84 @@ public abstract class AbstractRequestHandler extends AbstractHandler } } } + + /** + * Returns a display name for the handler + * + * @return + */ + public abstract String getName(); + + /** + * Deregister this listener and close it down + * + * @throws Exception + */ + public void shutdown() + { + try + { + HttpServer.getInstance().removeHandler(this); + stop(); + } catch (Exception e) + { + System.err.println("Error stopping " + getName() + ": " + + e.getMessage()); + } + } + + /** + * Returns the URI on which we are listening + * + * @return + */ + public String getUri() + { + return this.uri; + } + + /** + * Set the URI to this handler + * + * @param u + */ + protected void setUri(String u) + { + this.uri = u; + } + + /** + * Sets the relative path to this handler - do this before registering the + * handler. + * + * @param p + */ + protected void setPath(String p) + { + this.path = p; + } + + /** + * Returns the relative path to this handler below the context root (without / + * separators) + * + * @return + */ + public String getPath() + { + return this.path; + } + + /** + * Registers the handler with the HttpServer and reports its URI on stdout + * + * @throws BindException + * if no port could be allocated + * @throws IllegalStateException + * if this method is called before {@link #setPath} + */ + protected void registerHandler() throws BindException + { + HttpServer.getInstance().registerHandler(this); + } }