Merge branch 'features/JAL-1541_BioJsMSA' into develop
[jalview.git] / src / jalview / httpserver / AbstractRequestHandler.java
index 45064e7..8030c1d 100644 (file)
@@ -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);
+  }
 }