/*
 * $Id: LogoutAction.java,v 1.17 2006/04/29 18:39:46 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: LogoutAction.java,v $
 * Revision 1.17  2006/04/29 18:39:46  marbobo
 * + Refactoriengs und eine Ausgaben vereinheitlicht
 *
 * Revision 1.16  2004/12/16 13:17:51  jonyp
 * + saveMostRecentLogout
 *
 * Revision 1.15  2004/12/01 16:44:14  jonyp
 * added support for ldapusers
 *
 * Revision 1.14  2004/10/28 19:44:01  marbobo
 * + Anpassung wegen Loginseite
 *
 * Revision 1.13  2004/10/22 15:28:44  jonyp
 * added support for ldap user
 *
 * Revision 1.12  2004/05/10 16:03:26  jonyp
 * checks for default login page
 *
 * Revision 1.11  2004/03/08 18:23:52  bigmac
 * Anpassungen an VKCLanguageSerializer und VKCLanguagePackSerializer
 *
 * Revision 1.10  2004/02/09 10:17:30  bigmac
 * NPE bei abgelaufenen Sessions wird verhindert
 *
 * Revision 1.9  2004/02/04 16:08:09  jonyp
 * track logout time of user
 *
 * Revision 1.8  2003/12/18 18:49:54  bigmac
 * Überflüssige Imports gelöscht
 *
 * Revision 1.7  2003/11/21 14:19:46  bigmac
 * Usertracking erweitert
 *
 * Revision 1.6  2003/11/12 16:31:36  bigmac
 * Usertracking überarbeitet
 *
 * Revision 1.5  2003/11/01 00:35:57  marbobo
 * + Ausgeloggten Benutzer aus der Liste löschen (modifiziert)
 *
 * Revision 1.4  2003/10/31 17:51:27  marbobo
 * + Ausgeloggten Benutzer aus der Liste löschen
 *
 * Revision 1.3  2003/10/20 18:49:25  marbobo
 * sysLink bugfix
 *
 * Revision 1.2  2003/09/09 00:33:06  marbobo
 * ungenutzte Imports entfernt und Bugfixes
 *
 * Revision 1.1  2003/07/08 11:44:32  koppy
 * Initiale Version
 *
 */
package vkc.view.action;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.util.ServletUtils;
import vkc.view.WebShare;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCUserSerializer;
import vkc.misc.VKCLogManager;
import vkc.ldap.VKCLdapUser;

/**
 * Diese Klasse implementiert eine Action
 * zum Ausloggen des aktuellen Benutzers
 * 
 * @author koppy
 */
public class LogoutAction extends Action {
	/**
	 * Process the specified HTTP request, and create the corresponding HTTP
	 * response (or forward to another web component that will create it).
	 * Return an <code>ActionForward</code> instance describing where and how
	 * control should be forwarded, or <code>null</code> if the response has
	 * already been completed.
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The optional ActionForm bean for this request (if any)
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 *
	 * @exception Exception if business logic throws an exception
	 */
	public ActionForward execute(ActionMapping mapping, 
	        					 ActionForm form,
	        					 HttpServletRequest request,
	        					 HttpServletResponse response)
	throws Exception {
		VKCUser user = (VKCUser) request.getSession().getAttribute(WebShare.USER_KEY);

		if (user != null) {    
		    // release ldapcontext, if user is an LdapUser
			if (user instanceof VKCLdapUser) {
				if (((VKCLdapUser) user).getLdapContext() != null) {
			        ((VKCLdapUser) user).getLdapContext().close();
				}
			} 
	
			String log = user.getUserDescription() + 
						 " logged out of session " + request.getSession().getId() +
						 " with locale: " + user.getLanguage().getLocale();
	
			VKCLogManager.infoLog(this, log);
			
			VKCUserSerializer.saveMostRecentLogout(user.getUserId());
		}	
		
		ServletUtils.removeSessionAttributes(request.getSession());
		request.getSession().invalidate();	
				
		return new ActionForward(WebShare.getLoginPage(), true);
	}
}