package be.ibridge.kettle.www;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.handler.AbstractHandler;

import be.ibridge.kettle.core.LogWriter;

public class GetRootHandler extends AbstractHandler
{
    private static final long serialVersionUID = 3634806745372015720L;
    public static final String CONTEXT_PATH = "/";
    
    private static LogWriter log = LogWriter.getInstance();
    
    public GetRootHandler()
    {
    }

    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    {
        if (!request.getPathInfo().equals(CONTEXT_PATH)) return;
        if (!isStarted()) return;
        Request baseRequest = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();
        baseRequest.setHandled(true);

        if (log.isDebug()) log.logDebug(toString(), "Root requested");

        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        
        PrintWriter out = response.getWriter();
        
        out.println("<HTML>");
        out.println("<HEAD><TITLE>Kettle slave server</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<H2>Slave server menu</H2>");

        out.println("<p>");
        out.println("<a href=\"/kettle/status\">Show status</a><br>");

        out.println("<p>");
        out.println("</BODY>");
        out.println("</HTML>");        
        
        response.flushBuffer();
    }

    public String toString()
    {
        return "Root Handler";
    }
}

@@ -115,25 +115,102 @@
 .Servlet
-Exception
+Config;%0D%0Aimport javax.servlet.ServletException;%0D%0Aimport javax.servlet.http.HttpServlet
 ;%0D%0Aimpor
@@ -381,59 +381,8 @@
 st;%0D
-%0Aimport org.mortbay.jetty.handler.AbstractHandler;%0D
 %0A%0D%0Ai
@@ -464,23 +464,19 @@
 nds 
-AbstractHandler
+HttpServlet
 %0D%0A%7B%0D
@@ -708,32 +708,36 @@
 %0D%0A    %7B%0D%0A    %7D%0D%0A
+    
 %0D%0A    public voi
@@ -742,30 +742,141 @@
 oid 
-handle(String target, 
+init(ServletConfig servletConfig) throws ServletException%0D%0A    %7B%0D%0A        super.init(servletConfig);%0D%0A    %7D%0D%0A%0D%0A    public void doGet(
 Http
@@ -931,22 +931,8 @@
 onse
-, int dispatch
 ) th
@@ -936,18 +936,23 @@
  throws 
-IO
+Servlet
 Exceptio
@@ -954,23 +954,18 @@
 eption, 
-Servlet
+IO
 Exceptio
@@ -986,129 +986,91 @@
     
-if (!request.getPathInfo().equals(CONTEXT_PATH)) return
+doPost(request, response)
 ;%0D%0A    
+%7D%0D%0A
     
-if (!isStarted()) return;%0D%0A        Request base
+%0D%0A    public void doPost(HttpServlet
 Request 
 = (r
@@ -1069,147 +1069,158 @@
 est 
-= (
 request
- instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();
+, HttpServletResponse response) throws ServletException, IOException
 %0D%0A    
+%7B%0D%0A
     
-baseRequest.setHandled(true)
+    if (!request.getServletPath().equals(CONTEXT_PATH)) return
 ;%0D%0A%0D
@@ -1902,24 +1902,204 @@
 hBuffer();%0D%0A
+        %0D%0A        Request baseRequest = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();%0D%0A        baseRequest.setHandled(true);%0D%0A
     %7D%0D%0A%0D%0A   

Convert root handler to a servlet.