package Juno2;
import java.io.*;
import java.util.HashMap;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class CmdLogoutHandler extends org.apache.struts.action.Action implements RequestHandler {

    public CmdEventFactory factory = new CmdEventFactory();

    //public ActionForward perform(ActionMapping mapping, ActionForm form,
	public ActionForward execute(ActionMapping mapping, ActionForm form,    
                    javax.servlet.http.HttpServletRequest request,
                    javax.servlet.http.HttpServletResponse response) 
    {
        ////System.out.println("CmdLogoutHandler perform() invoked");   										
        try {
	   		processRequest(request);
        } catch (Exception e) {
	        System.out.println("CmdLogoutHandler processRequest threw exception: " + e.getMessage());
        }
        
        ////System.out.println("CmdLogoutHandler about to ActionForward()");   		
   		ActionForward result = mapping.findForward(CmdConstants.SUCCESS_KEY);
   		return result;
	}   		 	

	public void processRequest(HttpServletRequest request) throws Exception, CmdException
	{
        HttpSession session = null;
        try {
			session=request.getSession();
        }catch (Exception e) {
           System.out.println("request.getSession() exception");
           throw new CmdException("Fatal error getSession() Logout ");
    	}

		//// get the existing userid and password attributes
		String sessionUserId = (String)session.getAttribute("userId");
		String sessionUserPassword = (String)session.getAttribute("userPassword");
		// System.out.println("About to invalidate UserId / Password session |" + sessionUserId +
			// "|" + sessionUserPassword + "|  Logout");

		session.invalidate();
        request.setAttribute("CmdMessage", "Logged out succeeded.");
 	}
}
