View Javadoc

1   /*
2    * $Id: SecuritySessionManager.java 124 2004-11-01 18:09:40Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.security;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpSession;
27  
28  import org.talika.tarsis.Globals;
29  
30  /**
31   * Utility class to manage common tasks with security session.
32   *
33   * @author  Jose M. Palomar
34   * @version $Revision: 124 $
35   */
36  public final class SecuritySessionManager {
37  
38      // Fields
39      /**
40       * Singleton instance.
41       */
42      private static final SecuritySessionManager INSTANCE =
43                                                      new SecuritySessionManager();
44  
45      // Cosntructors
46      /**
47       * Creates a new <code>SecuritySessionManager</code>.
48       */
49      protected SecuritySessionManager() {
50      }
51  
52      // Method
53      /**
54       * Returns a <code>SecuritySessionManager</code> instance.
55       *
56       * @return SecuritySessionManager a <code>SecuritySessionManager</code> instance.
57       */
58      public static SecuritySessionManager getInstance() {
59          return INSTANCE;
60      }
61  
62      /**
63       * Returns <code>true</code> if <code>SecuritySession</code> from client's
64       * session is valid.
65       *
66       * @param request HttpServletRequest client's request.
67       * @return boolean <code>true</code> if <code>SecuritySession</code> from client's
68       * session is valid.
69       */
70      public boolean isSecuritySessionValid(HttpServletRequest request) {
71  
72          // Retrieve the saved security session from our session
73          HttpSession session = request.getSession(false);
74          if (session == null) {
75              return (false);
76          }
77  
78          SecuritySession securitySession =
79                  (SecuritySession) session.getAttribute(Globals.SECURITY_SESSION_ATTR);
80          if (securitySession == null) {
81              return (false);
82          }
83  
84          return (securitySession.isValid());
85  
86      }
87  
88      /**
89       * Resets <code>SecuritySession</code> from client's session.
90       *
91       * @param request HttpServletRequest client's session.
92       */
93      public void resetSecuritySession(HttpServletRequest request) {
94  
95          HttpSession session = request.getSession(false);
96          if (session == null) {
97              return;
98          }
99          session.removeAttribute(Globals.SECURITY_SESSION_ATTR);
100 
101     }
102 
103     /**
104      * Saves given <code>SecuritySession</code> in client's session.
105      *
106      * @param request HttpServletRequest client's request.
107      * @param securitySession SecuritySession security session.
108      */
109     public void saveSecuritySession(HttpServletRequest request, SecuritySession securitySession) {
110 
111         HttpSession session = request.getSession();
112         if (securitySession != null) {
113             session.setAttribute(Globals.SECURITY_SESSION_ATTR, securitySession);
114         }
115 
116     }
117 
118     /**
119      * Returns <code>SecuritySession</code> from client's session.
120      *
121      * @param request HttpServletRequest client's request.
122      * @return SecuritySession <code>SecuritySession</code> from client's session or
123      * <code>null</code> if there is no security session.
124      */
125     public SecuritySession getSecuritySession(HttpServletRequest request) {
126 
127         HttpSession session = request.getSession();
128         if (session != null) {
129             return (SecuritySession) session.getAttribute(Globals.SECURITY_SESSION_ATTR);
130         }
131         else {
132             return null;
133         }
134 
135     }
136 
137 }