From: gmungoc Date: Thu, 7 May 2015 15:58:15 +0000 (+0100) Subject: JAL-1575 bare bones RestHandler added X-Git-Tag: Jalview_2_9~25 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=2150df780eddbdf72013d29ee8eabb0f235c7bb4;p=jalview.git JAL-1575 bare bones RestHandler added --- diff --git a/src/jalview/ext/rbvi/chimera/ChimeraListener.java b/src/jalview/ext/rbvi/chimera/ChimeraListener.java index c9ec136..2882cb5 100644 --- a/src/jalview/ext/rbvi/chimera/ChimeraListener.java +++ b/src/jalview/ext/rbvi/chimera/ChimeraListener.java @@ -6,7 +6,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jalview.httpserver.AbstractRequestHandler; -import jalview.httpserver.HttpServer; import jalview.structure.SelectionSource; /** @@ -31,10 +30,19 @@ import jalview.structure.SelectionSource; public class ChimeraListener extends AbstractRequestHandler implements SelectionSource { + /* + * Chimera notification parameter name + */ private static final String CHIMERA_NOTIFICATION = "chimeraNotification"; + /* + * Chimera model changed notifications start with this + */ private static final String MODEL_CHANGED = "ModelChanged: "; + /* + * Chimera selection changed notification message + */ private static final String SELECTION_CHANGED = "SelectionChanged: selection changed\n"; /* @@ -45,9 +53,9 @@ public class ChimeraListener extends AbstractRequestHandler implements private static int chimeraId = 0; /* - * Path below context root that identifies this handler + * Prefix for path below context root (myChimeraId is appended) */ - private static final String LISTENER_PATH = "chimera"; + private static final String PATH_PREFIX = "chimera"; /* * Value of chimeraId (0, 1, 2...) for this instance @@ -59,13 +67,8 @@ public class ChimeraListener extends AbstractRequestHandler implements */ private JalviewChimeraBinding chimeraBinding; - /* - * The URI of this listener - */ - private String uri; - /** - * Constructor that also registers this as an Http request handler on path + * Constructor that registers this as an Http request handler on path * /chimeraN, where N is incremented for each instance. Call getUri to get the * resulting URI for this handler. * @@ -78,18 +81,8 @@ public class ChimeraListener extends AbstractRequestHandler implements { myChimeraId = chimeraId++; this.chimeraBinding = binding; - final String path = LISTENER_PATH + myChimeraId; - this.uri = HttpServer.getInstance().registerHandler(path, this); - } - - /** - * Returns the URI on which we are listening - * - * @return - */ - public String getUri() - { - return this.uri; + setPath(PATH_PREFIX + myChimeraId); + registerHandler(); } /** @@ -126,20 +119,11 @@ public class ChimeraListener extends AbstractRequestHandler implements } /** - * Deregister this listener and close it down - * - * @throws Exception + * Returns a display name for this service */ - public void shutdown() + @Override + public String getName() { - try - { - HttpServer.getInstance().removeHandler(this); - stop(); - } catch (Exception e) - { - System.err.println("Error stopping chimera listener: " - + e.getMessage()); - } + return "ChimeraListener"; } } diff --git a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java index 3c751e7..543cad4 100644 --- a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java +++ b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java @@ -40,6 +40,7 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; +import jalview.httpserver.AbstractRequestHandler; import jalview.schemes.ColourSchemeI; import jalview.schemes.ResidueProperties; import jalview.structure.AtomSpec; @@ -69,7 +70,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel /* * Object which listens to Chimera notifications */ - private ChimeraListener chimeraListener; + private AbstractRequestHandler chimeraListener; /* * set if chimera state is being restored from some source - instructs binding diff --git a/src/jalview/httpserver/AbstractRequestHandler.java b/src/jalview/httpserver/AbstractRequestHandler.java index 45064e7..9642508 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. */ @@ -88,4 +101,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); + } } diff --git a/src/jalview/httpserver/HttpServer.java b/src/jalview/httpserver/HttpServer.java index e60db9a..fcfefaa 100644 --- a/src/jalview/httpserver/HttpServer.java +++ b/src/jalview/httpserver/HttpServer.java @@ -16,6 +16,8 @@ import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.util.thread.QueuedThreadPool; +import jalview.rest.RestHandler; + /** * An HttpServer built on Jetty. To use it *