KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > setup > actions > LogoffSessionAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.setup.actions;
21
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35
36 import com.sslexplorer.agent.DefaultAgentManager;
37 import com.sslexplorer.core.RedirectWithMessages;
38 import com.sslexplorer.core.actions.AuthenticatedAction;
39 import com.sslexplorer.policyframework.Permission;
40 import com.sslexplorer.policyframework.PolicyConstants;
41 import com.sslexplorer.security.LogonControllerFactory;
42 import com.sslexplorer.security.SessionInfo;
43
44 public class LogoffSessionAction extends AuthenticatedAction {
45
46     static Log log = LogFactory.getLog(LogoffSessionAction.class);
47
48     public LogoffSessionAction() {
49         super(PolicyConstants.STATUS_TYPE_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_VIEW });
50     }
51
52     public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53                     throws Exception JavaDoc {
54         String JavaDoc ticket = request.getParameter("ticket");
55         if (ticket == null || ticket.equals("")) {
56             throw new Exception JavaDoc("No ticket parameter provided.");
57         } else {
58             
59             try {
60                 if (ticket.equals(this.getSessionInfo(request).getLogonTicket())) {
61                     log.error("You cannot log yourself off.");
62                     ActionMessages mesgs = new ActionMessages();
63                     mesgs.add(Globals.ERROR_KEY, new ActionMessage("status.sessions.cannotLogoffYourself"));
64                     saveErrors(request, mesgs);
65                     return new RedirectWithMessages(mapping.findForward("success"), request);
66                 }
67                 Map JavaDoc map = LogonControllerFactory.getInstance().getActiveSessions();
68                 synchronized (map) {
69                     SessionInfo info = (SessionInfo) map.get(ticket);
70                     if (info == null) {
71                         throw new Exception JavaDoc("No session with ticket " + ticket);
72                     } else {
73                         if(info.getType() == SessionInfo.UI || info.getType() == SessionInfo.DAV_CLIENT) {
74                             info.invalidate();
75                         }
76                         else if(info.getType() == SessionInfo.AGENT) {
77                             DefaultAgentManager.getInstance().unregisterAgent(info);
78                         }
79                     }
80                 }
81             } catch(IllegalStateException JavaDoc ex) {
82
83                 // Something strange happened, force a logoff of this ticket
84
try {
85                     LogonControllerFactory.getInstance().logoff(ticket);
86                 } catch(Throwable JavaDoc t) {}
87             }
88         }
89         return mapping.findForward("success");
90     }
91
92     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
93         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT;
94     }
95 }
Popular Tags