package ca.nrc.cadc.vosi;

import ca.nrc.cadc.auth.AuthenticationUtil;
import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import javax.security.auth.Subject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * Servlet implementation class CapabilityServlet
 */
public class AvailabilityServlet extends HttpServlet
{
	private static Logger log = Logger.getLogger(AvailabilityServlet.class);
    private static final long serialVersionUID = 201003131300L;
       
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
	{
        boolean started = false;
	    try
        {
	        String wsClassName = getInitParameter("ca.nrc.cadc.vosi.WebService");
	        Class wsClass = Class.forName(wsClassName);
	        WebService ws = (WebService) wsClass.newInstance();
	        AvailabilityStatus status = ws.getStatus();
	        Availability availability = new Availability(status);

	        Document document = availability.toXmlDocument();
	        XMLOutputter xop = new XMLOutputter(Format.getPrettyFormat());
            started = true;
            response.setContentType("text/xml");
	        xop.output(document, response.getOutputStream());
	    } 
        catch (Throwable t)
        {
            log.error("BUG", t);
            if (!started)
                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, t.getMessage());
        }
        finally
        {

        }
	}

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
	{
        boolean started = false;
	    try
        {
            Subject subject = AuthenticationUtil.getSubject(request);

	        String wsClassName = getInitParameter("ca.nrc.cadc.vosi.WebService");
	        Class wsClass = Class.forName(wsClassName);
	        WebService ws = (WebService) wsClass.newInstance();

            Subject.doAs(subject, new ChangeServiceState(ws, request));

            response.sendRedirect(request.getRequestURL().toString());
	    }
        catch (Throwable t)
        {
            log.error("BUG", t);
            if (!started)
                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, t.getMessage());
        }
        finally
        {

        }
	}

    private class ChangeServiceState implements PrivilegedExceptionAction
    {
        private WebService ws;
        private HttpServletRequest req;
        ChangeServiceState(WebService ws, HttpServletRequest req)
        {
            this.ws = ws;
            this.req = req;
        }

        public Object run() throws Exception
        {
            String state = req.getParameter("state");
            ws.setState(state);
            return null;
        }


    }
}

@@ -117,69 +117,266 @@
 ity.
-PrivilegedExceptionAction;%0Aimport javax.security.auth.Subject
+AccessControlContext;%0Aimport java.security.AccessController;%0Aimport java.security.Principal;%0Aimport java.security.PrivilegedExceptionAction;%0Aimport javax.security.auth.Subject;%0Aimport javax.security.auth.x500.X500Principal;%0Aimport javax.servlet.ServletConfig
 ;%0A%0Ai
@@ -924,23 +924,297 @@
 31300L;%0A
-       
+%0A    private String wsClassName;%0A    %0A    @Override%0A    public void init(ServletConfig config)%0A        throws ServletException%0A    %7B%0A        this.wsClassName = config.getInitParameter(%22ca.nrc.cadc.vosi.WebService%22);%0A        log.info(%22WebService class name: %22 + wsClassName);%0A    %7D%0A
 %0A    @Ov
@@ -1407,87 +1407,8 @@
   %7B%0A
-%09        String wsClassName = getInitParameter(%22ca.nrc.cadc.vosi.WebService%22);%0A
 %09   
@@ -1513,24 +1513,25 @@
 Instance();%0A
+%0A
 %09        Ava
@@ -2150,24 +2150,38 @@
       %7D%0A%09%7D%0A%0A
+    @Override%0A
     protecte
@@ -2427,87 +2427,8 @@
 );%0A%0A
-%09        String wsClassName = getInitParameter(%22ca.nrc.cadc.vosi.WebService%22);%0A
 %09   
@@ -3080,24 +3080,25 @@
 equest req;%0A
+%0A
         Chan

read wsClassname from config once only