KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > context > WebContextImpl


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.context;
14
15 import info.magnolia.cms.beans.runtime.File;
16 import info.magnolia.cms.beans.runtime.MultipartForm;
17 import info.magnolia.cms.core.Aggregator;
18 import info.magnolia.cms.core.Content;
19 import info.magnolia.cms.core.HierarchyManager;
20 import info.magnolia.cms.core.search.QueryManager;
21 import info.magnolia.cms.security.AccessManager;
22 import info.magnolia.cms.security.Authenticator;
23 import info.magnolia.cms.security.Security;
24 import info.magnolia.cms.security.User;
25 import info.magnolia.cms.security.UserManager;
26 import info.magnolia.cms.util.DumperUtil;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.jcr.RepositoryException;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import javax.servlet.jsp.jstl.core.Config;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 /**
43  * @author Sameer Charles
44  * @version $Revision $ ($Author $)
45  */

46 public class WebContextImpl extends AbstractContext implements WebContext {
47
48     /**
49      * Stable serialVersionUID.
50      */

51     private static final long serialVersionUID = 222L;
52
53     /**
54      * Logger
55      */

56     private static Logger log = LoggerFactory.getLogger(WebContextImpl.class);
57
58     /**
59      * http request
60      */

61     private HttpServletRequest JavaDoc request;
62
63     /**
64      * Use init to initialize the object
65      */

66     public WebContextImpl() {
67     }
68
69     /**
70      * @see info.magnolia.context.WebContext#init(javax.servlet.http.HttpServletRequest)
71      */

72     public void init(HttpServletRequest JavaDoc request) {
73         this.request = request;
74     }
75
76     /**
77      * Create the subject on demand
78      * @see info.magnolia.context.AbstractContext#getUser()
79      */

80     public User getUser() {
81         if (this.user == null) {
82             if (Authenticator.getSubject(request) == null) {
83                 log.debug("JAAS Subject is null, returning Anonymous user");
84                 this.user = Security.getUserManager().getUser(UserManager.ANONYMOUS_USER);
85             }
86             else {
87                 this.user = Security.getUserManager().getUser(Authenticator.getSubject(request));
88             }
89         }
90         return this.user;
91     }
92
93     /**
94      * Make the language available for JSTL
95      */

96     public void setLocale(Locale JavaDoc locale) {
97         super.setLocale(locale);
98         this.setAttribute(Config.FMT_LOCALE + ".session", locale.getLanguage(), Context.SESSION_SCOPE); //$NON-NLS-1$
99
}
100
101     /**
102      * Get hierarchy manager initialized for this user
103      * @param repositoryId
104      * @param workspaceId
105      * @return hierarchy manager
106      */

107     public HierarchyManager getHierarchyManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
108         HierarchyManager hm = SessionStore.getHierarchyManager(this.request, repositoryId, workspaceId);
109
110         //TODO remove this after we found the session refreshing issues
111
// check only once per session
112
if(this.request.getAttribute("jcr.session. " + repositoryId + ".checked")==null){
113             this.request.setAttribute("jcr.session. " + repositoryId + ".checked", "true");
114             try {
115                 if(hm.getWorkspace().getSession().hasPendingChanges()){
116                     log.error("the current jcr session has pending changes but shouldn't please set to debug level to see the dumped details");
117                     if(log.isDebugEnabled()){
118                         DumperUtil.dumpChanges(hm);
119                     }
120                     log.warn("will refresh (cleanup) the session");
121                     hm.getWorkspace().getSession().refresh(false);
122                 }
123             }
124             catch (RepositoryException e) {
125                 log.error("wasn't able to check pending changes on the session", e);
126             }
127         }
128         return hm;
129     }
130
131     /**
132      * Get access manager for the specified repository on the specified workspace
133      * @param repositoryId
134      * @param workspaceId
135      * @return access manager
136      */

137     public AccessManager getAccessManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
138         return SessionStore.getAccessManager(this.request, repositoryId, workspaceId);
139     }
140
141     /**
142      * Get QueryManager created for this user on the specified repository and workspace
143      * @param repositoryId
144      * @param workspaceId
145      * @return query manager
146      */

147     public QueryManager getQueryManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
148         try {
149             return SessionStore.getQueryManager(this.request, repositoryId, workspaceId);
150         }
151         catch (RepositoryException re) {
152             log.error(re.getMessage());
153             log.debug("Exception caught", re);
154             return null;
155         }
156     }
157
158     /**
159      * Get currently active page
160      * @return content object
161      */

162     public Content getActivePage() {
163         return (Content) this.request.getAttribute(Aggregator.ACTPAGE);
164     }
165
166     /**
167      * Get aggregated file, its used from image templates to manipulate
168      * @return file object
169      */

170     public File getFile() {
171         return (File) this.request.getAttribute(Aggregator.FILE);
172     }
173
174     /**
175      * Get form object assembled by <code>MultipartRequestFilter</code>
176      * @return multipart form object
177      */

178     public MultipartForm getPostedForm() {
179         return (MultipartForm) this.request.getAttribute("multipartform"); //$NON-NLS-1$
180
}
181
182     /**
183      * Get parameter value as string
184      * @param name
185      * @return parameter value
186      */

187     public String JavaDoc getParameter(String JavaDoc name) {
188         return this.request.getParameter(name);
189     }
190
191     /**
192      * Get parameter values as strings
193      * @return parameter values
194      */

195     public Map JavaDoc getParameters() {
196         // getParameterMap() is returning multiple values!
197
Map JavaDoc map = new HashMap JavaDoc();
198         Enumeration JavaDoc paramEnum = this.request.getParameterNames();
199         while (paramEnum.hasMoreElements()) {
200             String JavaDoc name = (String JavaDoc) paramEnum.nextElement();
201             map.put(name, this.request.getParameter(name));
202         }
203         return map;
204     }
205
206     /**
207      * Set attribute value, scope of the attribute is defined
208      * @param name is used as a key
209      * @param value
210      * @param scope , highest level of scope from which this attribute is visible
211      */

212     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
213         switch (scope) {
214             case Context.LOCAL_SCOPE:
215                 this.request.setAttribute(name, value);
216                 break;
217             case Context.SESSION_SCOPE:
218
219                 HttpSession JavaDoc httpsession = request.getSession(false);
220                 if (httpsession == null) {
221                     log
222                         .warn(
223                             "Session initialized in oder to setting attribute '{}' to '{}'. You should avoid using session when possible!",
224                             name,
225                             value);
226                     httpsession = request.getSession(true);
227                 }
228
229                 httpsession.setAttribute(name, value);
230                 break;
231             case Context.APPLICATION_SCOPE:
232                 MgnlContext.getSystemContext().setAttribute(name, value, Context.APPLICATION_SCOPE);
233                 break;
234             default:
235                 this.request.setAttribute(name, value);
236                 if (log.isDebugEnabled()) {
237                     log.debug("Undefined scope, setting attribute [{}] in request scope", name);
238                 }
239         }
240     }
241
242     /**
243      * Get attribute value
244      * @param name to which value is associated to
245      * @return attribute value
246      */

247     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
248         switch (scope) {
249             case Context.LOCAL_SCOPE:
250                 Object JavaDoc obj = this.request.getAttribute(name);
251                 if (obj == null) {
252                     obj = this.getParameter(name);
253                 }
254                 return obj;
255             case Context.SESSION_SCOPE:
256                 HttpSession JavaDoc httpsession = request.getSession(false);
257                 if (httpsession == null) {
258                     return null;
259                 }
260                 return httpsession.getAttribute(name);
261             case Context.APPLICATION_SCOPE:
262                 return MgnlContext.getSystemContext().getAttribute(name, Context.APPLICATION_SCOPE);
263             default:
264                 log.error("no illegal scope passed");
265                 return null;
266         }
267     }
268
269     /**
270      * Remove an attribute.
271      */

272     public void removeAttribute(String JavaDoc name, int scope) {
273         switch (scope) {
274             case Context.LOCAL_SCOPE:
275                 this.request.removeAttribute(name);
276                 break;
277             case Context.SESSION_SCOPE:
278                 HttpSession JavaDoc httpsession = request.getSession(false);
279                 if (httpsession != null) {
280                     httpsession.removeAttribute(name);
281                 }
282                 break;
283             case Context.APPLICATION_SCOPE:
284                 MgnlContext.getSystemContext().removeAttribute(name, Context.APPLICATION_SCOPE);
285                 break;
286             default:
287                 log.error("no illegal scope passed");
288         }
289     }
290
291     /**
292      * Get a map represenation of the scope
293      */

294     public final Map JavaDoc getAttributes(int scope) {
295         Map JavaDoc map = new HashMap JavaDoc();
296         Enumeration JavaDoc keysEnum;
297         switch (scope) {
298             case Context.LOCAL_SCOPE:
299                 // add parameters
300
map.putAll(this.getParameters());
301                 // attributes have higher priority
302
keysEnum = this.request.getAttributeNames();
303                 while (keysEnum.hasMoreElements()) {
304                     String JavaDoc key = (String JavaDoc) keysEnum.nextElement();
305                     Object JavaDoc value = getAttribute(key, scope);
306                     map.put(key, value);
307                 }
308                 return map;
309             case Context.SESSION_SCOPE:
310                 HttpSession JavaDoc httpsession = request.getSession(false);
311                 if (httpsession == null) {
312                     return null;
313                 }
314                 keysEnum = httpsession.getAttributeNames();
315                 while (keysEnum.hasMoreElements()) {
316                     String JavaDoc key = (String JavaDoc) keysEnum.nextElement();
317                     Object JavaDoc value = getAttribute(key, scope);
318                     map.put(key, value);
319                 }
320                 return map;
321             case Context.APPLICATION_SCOPE:
322                 return MgnlContext.getSystemContext().getAttributes(Context.APPLICATION_SCOPE);
323             default:
324                 log.error("no illegal scope passed");
325                 return null;
326         }
327
328     }
329
330     /**
331      * Avoid the call to this method where ever possible.
332      * @return Returns the request.
333      */

334     public HttpServletRequest JavaDoc getRequest() {
335         return this.request;
336     }
337
338     public String JavaDoc getContextPath() {
339         return this.request.getContextPath();
340     }
341 }
342
Popular Tags