JAL-1575 bare bones RestHandler added
[jalview.git] / src / jalview / httpserver / HttpServer.java
index e60db9a..fcfefaa 100644 (file)
@@ -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
  * <ul>
@@ -86,6 +88,11 @@ public class HttpServer
   private HttpServer() throws BindException
   {
     startServer();
+
+    /*
+     * Provides a REST server by default; add more programmatically as required
+     */
+    registerHandler(RestHandler.getInstance());
   }
 
   /**
@@ -200,16 +207,22 @@ public class HttpServer
   }
 
   /**
-   * Register a handler for the given path and returns its URI
+   * Register a handler for the given path and set its URI
    * 
-   * @param path
-   *          a path below the context root (without leading or trailing
-   *          separator)
    * @param handler
    * @return
+   * @throws IllegalStateException
+   *           if handler path has not been set
    */
-  public String registerHandler(String path, AbstractRequestHandler handler)
+  public void registerHandler(AbstractRequestHandler handler)
   {
+    String path = handler.getPath();
+    if (path == null)
+    {
+      throw new IllegalStateException(
+              "Must set handler path before registering handler");
+    }
+
     // http://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime
     ContextHandler ch = new ContextHandler();
     ch.setAllowNullPathInfo(true);
@@ -237,7 +250,9 @@ public class HttpServer
               + e.getMessage());
     }
 
-    return this.contextRoot + ch.getContextPath().substring(1);
+    handler.setUri(this.contextRoot + ch.getContextPath().substring(1));
+    System.out.println("Jalview " + handler.getName()
+            + " handler started on " + handler.getUri());
   }
 
   /**