KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > LogoffAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.apache.struts.action.*;
28 import org.apache.struts.util.*;
29
30 import cowsultants.itracker.ejb.client.models.*;
31 import cowsultants.itracker.ejb.client.util.*;
32 import cowsultants.itracker.web.util.*;
33
34
35 public class LogoffAction extends ITrackerAction {
36     public LogoffAction() {
37     }
38
39     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40         try {
41             HttpSession session = request.getSession(true);
42             UserModel user = (UserModel) session.getAttribute("user");
43             String JavaDoc login = (user != null ? user.getLogin() : "UNKNOWN");
44
45             if(clearSession(login, request, response)) {
46                 Logger.logInfo("User " + login + " logged out successfully.");
47             }
48         } catch(Exception JavaDoc e) {
49             Logger.logError("Error logging out user. " + e.getMessage());
50         }
51         return mapping.findForward("login");
52     }
53
54     public boolean clearSession(String JavaDoc login, HttpServletRequest request, HttpServletResponse response) {
55         try {
56             HttpSession session = request.getSession(true);
57             session.invalidate();
58
59             if(login != null) {
60                 SessionManager.invalidateSession(login);
61             }
62         } catch(Exception JavaDoc e) {
63             Logger.logError("Unable to clear session for user " + (login == null ? "UNKNOWN" : login));
64             return false;
65         }
66         return true;
67     }
68 }
69   
Popular Tags