package org.lemurproject.galago.tupleflow.web;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.lemurproject.galago.utility.Parameters;
import org.lemurproject.galago.tupleflow.Utility;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.UnknownHostException;

/**
 * Consolidating some common Jetty stuff.
 * @author jfoley.
 */
public class WebServer {

  private final Server server;

  public WebServer(Server server) {
    this.server = server;
  }

  public void stop() throws WebServerException {
    try {
      this.server.stop();
    } catch (Exception ex) {
      throw new WebServerException(ex);
    }
  }

  public void join() throws WebServerException {
    try {
      this.server.join();
    } catch (InterruptedException e) {
      throw new WebServerException(e);
    }
  }

  public int getPort() {
    return server.getConnectors()[0].getPort();
  }

  public static String getHostName() {
    try {
      return java.net.InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      e.printStackTrace();
      return "localhost";
    }
  }

  public String getURL() {
    return String.format("http://%s:%d", getHostName(), getPort());
  }

  public static WebServer start(int port, WebHandler handler) throws WebServerException {
    Server server = new Server(port);
    server.setHandler(new JettyHandler(handler));
    try {
      server.start();
    } catch (Exception ex) {
      throw new WebServerException(ex);
    }

    return new WebServer(server);
  }

  public static WebServer start(Parameters p, WebHandler handler) throws WebServerException {
    int port = (int) p.get("port", 0);
    if (port == 0) {
      try {
        port = Utility.getFreePort();
      } catch (IOException e) {
        throw new WebServerException(e);
      }
    }
    return start(port, handler);
  }

  public static final class JettyHandler extends AbstractHandler {
    public final WebHandler handler;

    public JettyHandler(WebHandler handler) {
      this.handler = handler;
    }

    @Override
    public void handle(String s, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
      jettyRequest.setHandled(true);
      try {
        handler.handle(request, response);
      } catch (Exception e) {
        response.sendError(501, e.getMessage());
      }
    }
  }
}

@@ -175,24 +175,74 @@
 actHandler;%0A
+import org.lemurproject.galago.tupleflow.Utility;%0A
 import org.l
@@ -311,33 +311,37 @@
 .galago.
-tupleflow.
+utility.json.JSON
 Util
-ity
 ;%0A%0Aimpor
@@ -532,16 +532,97 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.Enumeration;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1982,14 +1982,8 @@
 rt =
- (int)
  p.g
@@ -2718,28 +2718,28 @@
 age());%0A      %7D%0A
-
     %7D%0A  %7D%0A%7D%0A
@@ -2735,11 +2735,506 @@
    %7D%0A  %7D
+%0A%0A  public static Parameters parseGetParameters(HttpServletRequest req) %7B%0A    Parameters request = Parameters.create();%0A%0A    Enumeration args = req.getParameterNames();%0A    while(args.hasMoreElements()) %7B%0A      String arg = (String) args.nextElement();%0A      List%3CObject%3E vals = new ArrayList%3C%3E();%0A      for (String x : req.getParameterValues(arg)) %7B%0A        vals.add(JSONUtil.parseString(x));%0A      %7D%0A      request.put(arg, vals.size() == 1 ? vals.get(0) : vals);%0A    %7D%0A%0A    return request;%0A  %7D
 %0A%7D%0A

Add utility method for parsing GET parameters to WebServer class