View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.context;
35  
36  import info.magnolia.cms.beans.runtime.MultipartForm;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.core.HierarchyManager;
39  import info.magnolia.cms.core.search.QueryManager;
40  import info.magnolia.cms.i18n.Messages;
41  import info.magnolia.cms.security.AccessManager;
42  import info.magnolia.cms.security.User;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  import javax.servlet.ServletContext;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import java.util.Locale;
50  import java.util.Map;
51  
52  
53  /**
54   * This class enables to get the current Request without passing the request around the world. A ThreadLocal is used to
55   * manage this.
56   * <p>
57   * In a later version this class should not depend on servlets. The core should use the context to get and set
58   * attributes instead of using the request or session object directly. Magnolia could run then in a neutral and
59   * configurable context.
60   * @author Philipp Bracher
61   * @version $Revision: 34573 $ ($Author: had $)
62   */
63  
64  public class MgnlContext {
65      private static final Logger log = LoggerFactory.getLogger(MgnlContext.class);
66  
67      /**
68       * The thread local variable holding the current context
69       */
70      private static ThreadLocal<Context> localContext = new ThreadLocal<Context>();
71  
72      /**
73       * Do not instantiate this class. The constructor must be public to use discovery
74       */
75      public MgnlContext() {
76      }
77  
78      /**
79       * A short cut for the current user.
80       * @return the current user
81       */
82      public static User getUser() {
83          return getInstance().getUser();
84      }
85  
86      /**
87       * Set the locale for the current context.
88       */
89      public static void setLocale(Locale locale) {
90          getInstance().setLocale(locale);
91      }
92  
93      /**
94       * Get the contexts locale object
95       * @return the current locale
96       */
97      public static Locale getLocale() {
98          return getInstance().getLocale();
99      }
100 
101     public static Messages getMessages() {
102         return getInstance().getMessages();
103     }
104 
105     public static Messages getMessages(String basename) {
106         return getInstance().getMessages(basename);
107     }
108 
109     public static void login(User user) {
110         ((UserContext)getInstance()).login(user);
111     }
112 
113     /**
114      * Get hierarchy manager initialized for this user.
115      */
116     public static HierarchyManager getHierarchyManager(String repositoryId) {
117         return getInstance().getHierarchyManager(repositoryId);
118     }
119 
120     /**
121      * Get hierarchy manager initialized for this user.
122      */
123     public static HierarchyManager getHierarchyManager(String repositoryId, String workspaceId) {
124         return getInstance().getHierarchyManager(repositoryId, workspaceId);
125     }
126 
127     /**
128      * Get access manager for the specified repository on default workspace.
129      */
130     public static AccessManager getAccessManager(String repositoryId) {
131         return getInstance().getAccessManager(repositoryId);
132     }
133 
134     /**
135      * Get access manager for the specified repository on the specified workspace.
136      */
137     public static AccessManager getAccessManager(String repositoryId, String workspaceId) {
138         return getInstance().getAccessManager(repositoryId, workspaceId);
139     }
140 
141     /**
142      * Get QueryManager created for this user on the specified repository.
143      */
144     public static QueryManager getQueryManager(String repositoryId) {
145         return getInstance().getQueryManager(repositoryId);
146     }
147 
148     /**
149      * Get QueryManager created for this user on the specified repository and workspace.
150      */
151     public static QueryManager getQueryManager(String repositoryId, String workspaceId) {
152         return getInstance().getQueryManager(repositoryId, workspaceId);
153     }
154 
155     /**
156      * Get form object assembled by <code>MultipartRequestFilter</code>
157      * @return multipart form object
158      * TODO - move to getAggregationState() ?
159      */
160     public static MultipartForm getPostedForm() {
161         WebContext ctx = getWebContextOrNull();
162         if (ctx != null) {
163             return ctx.getPostedForm();
164         }
165         return null;
166     }
167 
168     /**
169      * Get parameter value as string.
170      */
171     public static String getParameter(String name) {
172         WebContext ctx = getWebContextOrNull();
173         if (ctx != null) {
174             return ctx.getParameter(name);
175         }
176         return null;
177 
178     }
179 
180     public static String[] getParameterValues(String name) {
181         WebContext ctx = getWebContextOrNull();
182         if (ctx != null) {
183             return ctx.getParameterValues(name);
184         }
185         return null;
186 
187     }
188 
189     /**
190      * Get parameter value as a Map&lt;String, String&gt;.
191      */
192     public static Map<String, String> getParameters() {
193         WebContext ctx = getWebContextOrNull();
194         if (ctx != null) {
195             return ctx.getParameters();
196         }
197         return null;
198     }
199 
200     /**
201      * @return the context path.
202      */
203     public static String getContextPath() {
204         WebContext ctx = getWebContextOrNull();
205         if (ctx != null) {
206             return ctx.getContextPath();
207         } else {
208             throw new IllegalStateException("Can only get the context path within a WebContext.");
209         }
210     }
211 
212     /**
213      * Returns the AggregationState if we're in a WebContext, throws an
214      * IllegalStateException otherwise.
215      */
216     public static AggregationState getAggregationState() {
217         final WebContext ctx = getWebContextOrNull();
218         if (ctx != null) {
219             return ctx.getAggregationState();
220         } else {
221             throw new IllegalStateException("Can only get the aggregation state within a WebContext.");
222         }
223     }
224 
225     /**
226      * Resets the current aggregation state if we're in a WebContext, throws an IllegalStateException otherwise.
227      */
228     public static void resetAggregationState() {
229         final WebContext ctx = getWebContextOrNull();
230         if (ctx != null) {
231             ctx.resetAggregationState();
232         }
233         else {
234             throw new IllegalStateException("Can only reset the aggregation state within a WebContext.");
235         }
236     }
237 
238     /**
239      * Set attribute value, scope of the attribute is defined.
240      */
241     public static void setAttribute(String name, Object value) {
242         getInstance().setAttribute(name, value, Context.LOCAL_SCOPE);
243     }
244 
245     /**
246      * Set attribute value, scope of the attribute is defined.
247      * @param scope , highest level of scope from which this attribute is visible.
248      */
249     public static void setAttribute(String name, Object value, int scope) {
250         getInstance().setAttribute(name, value, scope);
251     }
252 
253     /**
254      * Get attribute value.
255      */
256     public static Object getAttribute(String name) {
257         return getInstance().getAttribute(name);
258     }
259 
260     /**
261      * Get the attribute from the specified scope.
262      */
263     public static Object getAttribute(String name, int scope) {
264         return getInstance().getAttribute(name, scope);
265     }
266 
267     /**
268      * Check if this attribute exists in the local scope.
269      */
270     public static boolean hasAttribute(String name){
271         return getInstance().getAttribute(name, Context.LOCAL_SCOPE) != null;
272     }
273 
274     /**
275      * Remove an attribute in the local scope.
276      */
277     public static void removeAttribute(String name){
278         getInstance().removeAttribute(name, Context.LOCAL_SCOPE);
279     }
280 
281     /**
282      * Set context implementation instance.
283      */
284     public static void setInstance(Context context) {
285         localContext.set(context);
286     }
287 
288     /**
289      * Get the current context of this thread.
290      */
291     public static Context getInstance() {
292         Context context = localContext.get();
293         // It should never fall back, We need to fix all false callers instead
294         if (context == null) {
295             IllegalStateException ise = new IllegalStateException("MgnlContext is not set for this thread");
296             log.error("MgnlContext is not initialized. This could happen if the request does not go through the Magnolia default filters.", ise);
297             throw ise;
298         }
299         return context;
300     }
301 
302     /**
303      * Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext.
304      * @see #getWebContext(String)
305      */
306     public static WebContext getWebContext() {
307         return getWebContext(null);
308     }
309 
310     /**
311      * Throws an IllegalStateException if the current context is not set, or if it is not an instance of WebContext.
312      * Yes, you can specify the exception message if you want. This is useful if you're calling this from a component
313      * which only supports WebContext and still care enough to actually throw an exception with a meaningful message.
314      * @see #getWebContext()
315      */
316     public static WebContext getWebContext(String exceptionMessage) {
317         final WebContext wc = getWebContextIfExisting(getInstance());
318         if (wc == null) {
319             throw new IllegalStateException(exceptionMessage == null ? "The current context is not an instance of WebContext (" + localContext.get() + ")" : exceptionMessage);
320         }
321         return wc;
322     }
323 
324     /**
325      * Returns the current context if it is set and is an instance of WebContext, returns null otherwise.
326      * @return
327      */
328     public static WebContext getWebContextOrNull() {
329         return hasInstance() ? getWebContextIfExisting(getInstance()) : null;
330     }
331 
332     /**
333      * Used to check if an instance is already set since getInstance() will always return a context.
334      * @return true if an instance was set.
335      */
336     public static boolean hasInstance() {
337         return localContext.get() != null;
338     }
339 
340     public static boolean isSystemInstance() {
341         return localContext.get() instanceof SystemContext;
342     }
343 
344     /**
345      * Returns true if the current context is set and is an instance of WebContext. (it might be wrapped in a ContextDecorator)
346      */
347     public static boolean isWebContext() {
348         return hasInstance() && getWebContextIfExisting(getInstance()) != null;
349     }
350 
351     /**
352      * Get Magnolia system context. This context has full permissions over all repositories/ workspaces.
353      */
354     public static SystemContext getSystemContext() {
355         return ContextFactory.getInstance().getSystemContext();
356     }
357 
358     /**
359      * @deprecated since 4.2 - use the Op interface, which can return values, or extend VoidOp.
360      */
361     public static void doInSystemContext(final SystemContextOperation op) {
362         doInSystemContext(op, false);
363     }
364 
365     /**
366      * Executes the given operation in the system context and sets it back to the original once done
367      * (also if an exception is thrown). Also works if there was no context upon calling. (sets it back
368      * to null in this case)
369      */
370     public static <T, E extends Throwable> T doInSystemContext(final Op<T, E> op) throws E {
371         return doInSystemContext(op, false);
372     }
373 
374     /**
375      * @deprecated since 4.2 - use the Op interface, which can return values, or extend VoidOp.
376      */
377     public static void doInSystemContext(final SystemContextOperation op, boolean releaseAfterExecution) {
378         doInSystemContext(new VoidOp() {
379             public void doExec() {
380                 op.exec();
381             }
382         }, releaseAfterExecution);
383     }
384 
385     /**
386      * Executes the given operation in the system context and sets it back to the original once done
387      * (also if an exception is thrown). Also works if there was no context upon calling (sets it back
388      * to null in this case)
389      * @param releaseAfterExecution TODO document this parameter
390      */
391     public static <T, E extends Throwable> T doInSystemContext(final Op<T, E> op, boolean releaseAfterExecution) throws E {
392         final Context originalCtx = MgnlContext.hasInstance() ? MgnlContext.getInstance() : null;
393         T result;
394         try {
395             MgnlContext.setInstance(MgnlContext.getSystemContext());
396             result = op.exec();
397             if (releaseAfterExecution) {
398                 MgnlContext.release();
399             }
400         } finally {
401             MgnlContext.setInstance(originalCtx);
402         }
403         return result;
404     }
405 
406     /**
407      * @deprecated since 4.2 - use the Op interface, which can return values, or extend VoidOp.
408      * @see info.magnolia.context.MgnlContext.Op
409      * @see info.magnolia.context.MgnlContext.VoidOp
410      */
411     public static interface SystemContextOperation {
412         void exec();
413     }
414 
415     /**
416      * A simple execution interface to be used with the doInSystemContext method.
417      * If no return value is necessary, return null (for semantic's sake, declare T as <Void>)
418      * If no checked exception need to be thrown, declare E as <RuntimeException>)
419      *
420      * @see MgnlContext#doInSystemContext(Op op)
421      * @param <T> the return type of this operation
422      * @param <E> an exception this operation can throw
423      */
424     public static interface Op<T, E extends Throwable> {
425         T exec() throws E;
426     }
427 
428     /**
429      * An Op that does not return values and can only throw RuntimeExceptions.
430      */
431     public abstract static class VoidOp implements Op<Void, RuntimeException> {
432         public Void exec() {
433             doExec();
434             return null;
435         }
436 
437         abstract public void doExec();
438     }
439 
440     /**
441      * Sets this context as a web context.
442      * @param request HttpServletRequest
443      * @param response HttpServletResponse
444      * @param servletContext ServletContext instance
445      */
446     public static void initAsWebContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
447         WebContext ctx = ContextFactory.getInstance().createWebContext(request, response, servletContext);
448         setInstance(ctx);
449     }
450 
451     /**
452      * Returns the web context, also if eventually wrapped in a ContextDecorator.
453      * @return WebContext instance or null if no web context is set
454      */
455     private static WebContext getWebContextIfExisting(Context ctx) {
456         if (ctx instanceof WebContext) {
457             return (WebContext) ctx;
458         }
459         else if (ctx instanceof ContextDecorator) {
460             return getWebContextIfExisting(((ContextDecorator) ctx).getWrappedContext());
461         }
462         return null;
463     }
464 
465     /**
466      * Releases the current thread (if not a system context) and calls the releaseThread() method of the system context
467      *
468      */
469     public static void release() {
470         if(hasInstance() && !(getInstance() instanceof SystemContext)){
471             getInstance().release();
472         }
473         SystemContext systemContext = getSystemContext();
474         if(systemContext instanceof ThreadDependentSystemContext){
475             ((ThreadDependentSystemContext)systemContext).releaseThread();
476         }
477     }
478 
479     public static void push(HttpServletRequest request, HttpServletResponse response) {
480         if (isWebContext()) {
481             WebContext wc = getWebContext();
482             wc.push(request,response);
483         }
484     }
485 
486     public static void pop() {
487         if (isWebContext()) {
488             WebContext wc = getWebContext();
489             wc.pop();
490         }
491     }
492 }