KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > Session


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common;
25
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpSession JavaDoc;
30
31 import org.infoglue.cms.entities.management.SystemUser;
32 import org.infoglue.cms.security.InfoGlueAuthenticationFilter;
33 import org.infoglue.cms.security.InfoGluePrincipal;
34
35 import webwork.action.ActionContext;
36 import webwork.action.factory.SessionMap;
37
38
39 /**
40  * Session wrapper to ease getting things out of the Session. Abstract it
41  * so that I can work well with a Map (for testing, or ActionContext.getSession())
42  * or an HttpSession, but not rely on an HttpSession.
43  *
44  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
45  * @author Frank Febbraro (frank@phase2technology.com)
46  */

47 public class Session
48 {
49     // Session attribute names
50
public static final String JavaDoc LOCALE = "locale";
51     private static final String JavaDoc USER = "user";
52     private static final String JavaDoc IG_PRINCIPAL = InfoGlueAuthenticationFilter.INFOGLUE_FILTER_USER;
53     public static final String JavaDoc TOOL_ID = "toolId";
54
55
56     private Map JavaDoc sessionDelegate;
57
58     public Session()
59     {
60         this(ActionContext.getSession());
61     }
62
63     public Session(Map JavaDoc session)
64     {
65         this.sessionDelegate = session;
66     }
67
68     public Session(HttpSession JavaDoc httpSession)
69     {
70         this.sessionDelegate = new SessionMap(httpSession);
71     }
72
73     /**
74     * Returns the locale used for the session.
75     */

76     public final Locale JavaDoc getLocale()
77     {
78         //If empty set it to english
79
if(sessionDelegate.get(LOCALE) == null)
80         {
81             setLocale(java.util.Locale.ENGLISH);
82         }
83
84         return (Locale JavaDoc) sessionDelegate.get(LOCALE);
85     }
86
87     /**
88     * Returns the locale used for the session.
89     */

90     public final Integer JavaDoc getToolId()
91     {
92         //If empty set it to english
93
if(sessionDelegate.get(TOOL_ID) == null)
94         {
95             setToolId(new Integer JavaDoc(0));
96         }
97
98         return (Integer JavaDoc) sessionDelegate.get(TOOL_ID);
99     }
100
101     /**
102      * Sets the locale used for the session.
103      *
104      * @param locale the locale to use for the session.
105      */

106     public final void setLocale(Locale JavaDoc locale)
107     {
108         sessionDelegate.put(LOCALE, locale);
109     }
110
111     /**
112      * Sets the locale used for the session.
113      *
114      * @param locale the locale to use for the session.
115      */

116     public final void setToolId(Integer JavaDoc toolId)
117     {
118         sessionDelegate.put(TOOL_ID, toolId);
119     }
120
121     /**
122      * Returns the user of the session.
123      *
124      * @return the user of the session.
125      */

126     public final SystemUser getUser()
127     {
128         return (SystemUser) sessionDelegate.get(USER);
129     }
130
131     /**
132      * Sets the user of the session.
133      *
134      * @param systemUser the user of the session.
135      */

136     public final void setSystemUser(SystemUser systemUser)
137     {
138         // <todo>throw error if this happens more than once</todo>
139
sessionDelegate.put(USER, systemUser);
140     }
141
142     /**
143      * Returns the InfoGlue principal associated with the current user and session.
144      * TODO: Update InfoGlueAuthenticationFilter to use this Session Object
145      * @return the InfoGlue principal associated with the current user and session
146      */

147     public InfoGluePrincipal getInfoGluePrincipal()
148     {
149         return (InfoGluePrincipal)sessionDelegate.get(IG_PRINCIPAL);
150     }
151
152     /**
153      * Sets the InfoGlue principal associated with the current user and session.
154      * TODO: Update InfoGlueAuthenticationFilter to use this Session Object
155      */

156     public void setInfoGluePrincipal(InfoGluePrincipal p)
157     {
158         sessionDelegate.put(IG_PRINCIPAL, p);
159     }
160
161     public String JavaDoc toString()
162     {
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<Session>\n");
164         sb.append(" locale=[" + getLocale() + "]\n");
165         sb.append(" user=[" + getUser() + "]\n");
166         return sb.toString();
167     }
168     
169 }
Popular Tags