/*
 * Copyright 2012 the hegemon authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cueup.hegemon.testing.server;

import com.cueup.hegemon.LoadPath;
import com.cueup.hegemon.testing.HegemonRunner;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.runner.notification.RunNotifier;

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

/**
 * Test server for Hegemon.
 */
public class HegemonTestServer extends AbstractHandler {

  private final LoadPath loadPath;


  /**
   * Creates a test server loading sources with 'loadPath'.
   */
  public HegemonTestServer(LoadPath loadPath) {
    this.loadPath = loadPath;
  }


  /**
   *  The request handler translates an HTTP request to a test class and method to run.
   */
  @Override
  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    if ("/favicon.ico".equals(target)) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      baseRequest.setHandled(true);
      return;
    }

    if (target.startsWith("/_file/")) {
      String[] parts = target.substring(7).split(":");
      String filename = parts[0];
      int lineNumber = Integer.parseInt(parts[1]);
      Runtime.getRuntime().exec(new String[]{
        "/Applications/IntelliJ IDEA 10.app/Contents/MacOS/idea",
        "--line",
        "" + lineNumber,
        "/var/greplin/src/java/hydra/src/main/webapp/" + filename,
      });
      Runtime.getRuntime().exec("open -b com.jetbrains.intellij");

      response.setContentType("text/html;charset=utf-8");
      response.getWriter().print("ok");
      response.setStatus(HttpServletResponse.SC_OK);
      baseRequest.setHandled(true);
      return;
    }

    try {
      Class c = Class.forName(target.substring(1), true, getClass().getClassLoader());
      HegemonRunner runner =
          new HegemonRunner(c, baseRequest.getQueryString(), this.loadPath);
      RunNotifier notifier = new RunNotifier();
      notifier.addListener(new ResponseListener(response));

      long start = System.currentTimeMillis();

      response.setContentType("text/html;charset=utf-8");
      response.getWriter().print(
          "<style>.ok {color:green} .fail {color:red} .ignore {color:orange} pre {margin-left: 24px}</style>");
      runner.run(notifier);
      response.setStatus(HttpServletResponse.SC_OK);

      response.getWriter().println("<br>Finished in " + ((System.currentTimeMillis() - start) / 1000.0) + " seconds.");

      baseRequest.setHandled(true);

    } catch (Throwable t) { // lint: disable=IllegalCatchCheck
      response.setContentType("text/plain;charset=utf-8");
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      baseRequest.setHandled(true);
      t.printStackTrace(response.getWriter());
    }

  }


  /**
   * Starts an http server on a port that responds to a passed handler.
   */
  public static void run(AbstractHandler handler, int port) throws Exception {
    Server server = new Server(port);
    server.setHandler(handler);

    server.start();
    server.join();
  }

}

@@ -1796,696 +1796,8 @@
  %7D%0A%0A
-    if (target.startsWith(%22/_file/%22)) %7B%0A      String%5B%5D parts = target.substring(7).split(%22:%22);%0A      String filename = parts%5B0%5D;%0A      int lineNumber = Integer.parseInt(parts%5B1%5D);%0A      Runtime.getRuntime().exec(new String%5B%5D%7B%0A        %22/Applications/IntelliJ IDEA 10.app/Contents/MacOS/idea%22,%0A        %22--line%22,%0A        %22%22 + lineNumber,%0A        %22/var/greplin/src/java/hydra/src/main/webapp/%22 + filename,%0A      %7D);%0A      Runtime.getRuntime().exec(%22open -b com.jetbrains.intellij%22);%0A%0A      response.setContentType(%22text/html;charset=utf-8%22);%0A      response.getWriter().print(%22ok%22);%0A      response.setStatus(HttpServletResponse.SC_OK);%0A      baseRequest.setHandled(true);%0A      return;%0A    %7D%0A%0A
     

Remove IntelliJ linking for now