KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > security > LogoutFilter


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.security;
21
22 import org.apache.log4j.Logger;
23 import java.io.*;
24 import java.io.IOException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28
29
30 /**
31  * @author Uddhab Pant
32  *
33  * Filter to handle logout event.
34  *
35  */

36 public class LogoutFilter implements Filter {
37     private static Logger logger = Logger.getLogger(LogoutFilter.class);
38
39     /**
40      * Called by the web container to indicate to a filter that it is being
41      * placed into service.
42      *
43      * @param filterConfig FilterConfig
44      * @throws ServletException
45      */

46     public void init(FilterConfig filterConfig) throws ServletException {
47     }
48
49     /**
50      * The <code>doFilter</code> method of the Filter is called by the container
51      * each time a request/response pair is passed through the chain due to a
52      * client request for a resource at the end of the chain.
53      *
54      * @param request ServletRequest
55      * @param response ServletResponse
56      * @param chain FilterChain
57      * @throws IOException
58      * @throws ServletException
59      */

60     public void doFilter(ServletRequest request, ServletResponse response,
61         FilterChain chain) throws IOException JavaDoc, ServletException {
62         HttpServletResponse res = (HttpServletResponse) response;
63         HttpServletRequest req = (HttpServletRequest) request;
64         HttpSession session = req.getSession();
65
66         String JavaDoc requestURI = req.getRequestURI();
67
68         logger.debug("handling requestURI:" + requestURI);
69
70         /*
71            When logout is clicked, 'killsession' request is sent to Filter.
72            Terminate session and redirect to login page.
73          */

74         if (session != null) {
75             // Session.invalidate() does not remove the actual session; It just marks it as invalid.
76
logger.debug("removing session attributes");
77
78             for (Enumeration JavaDoc list = session.getAttributeNames();
79                     list.hasMoreElements();)
80                 session.removeAttribute((String JavaDoc) list.nextElement());
81
82             logger.debug("terminating session");
83             session.invalidate();
84         }
85
86         logger.debug("redirecting to root");
87         res.sendRedirect(req.getContextPath() + "/");
88     }
89
90     /**
91      * Called by the web container to indicate to a filter that it is being taken
92      * out of service.
93      *
94      */

95     public void destroy() {
96     }
97 }
98
Popular Tags