KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > Resource


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.cms.util;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.beans.runtime.File;
17 import info.magnolia.cms.beans.runtime.MultipartForm;
18 import info.magnolia.cms.core.Aggregator;
19 import info.magnolia.cms.core.Content;
20 import info.magnolia.cms.core.HierarchyManager;
21 import info.magnolia.context.MgnlContext;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25
26 import org.apache.commons.lang.BooleanUtils;
27 import org.apache.commons.lang.StringUtils;
28
29
30 /**
31  * @author Sameer Charles
32  * @version 1.1
33  */

34 public final class Resource {
35
36     /**
37      * Attribute used for enabling the preview mode.
38      */

39     public static final String JavaDoc MGNL_PREVIEW_ATTRIBUTE = "mgnlPreview"; //$NON-NLS-1$
40

41     private static final String JavaDoc GLOBAL_CONTENT_NODE = "contentObjGlobal"; //$NON-NLS-1$
42

43     private static final String JavaDoc LOCAL_CONTENT_NODE = "contentObj"; //$NON-NLS-1$
44

45     private static final String JavaDoc LOCAL_CONTENT_NODE_COLLECTION_NAME = "localContentNodeCollectionName"; //$NON-NLS-1$
46

47     /**
48      * Utility class, don't instantiate.
49      */

50     private Resource() {
51         // unused
52
}
53
54     /**
55      * <p>
56      * get Content object as requested from the URI
57      * </p>
58      * @param req HttpServletRequest as received in JSP or servlet
59      * @return currently active page, as requested from the URI
60      */

61     public static Content getActivePage(HttpServletRequest JavaDoc req) {
62         return (Content) req.getAttribute(Aggregator.ACTPAGE);
63     }
64
65     /**
66      * <p>
67      * get selector as requested from the URI. The selector is the part between the handle and the extension.
68      * selector("http://server/a.x.1.f.4.html") = "x.1.f.4"
69      * </p>
70      * @param req HttpServletRequest as received in JSP or servlet
71      * @return selector String as requested from the URI
72      */

73     public static String JavaDoc getSelector(HttpServletRequest JavaDoc req) {
74         return (String JavaDoc) req.getAttribute(Aggregator.SELECTOR);
75     }
76
77     /**
78      * <p>
79      * get file object associated with the requested atom
80      * </p>
81      * @param req HttpServletRequest as received in JSP or servlet
82      * @return currently atom
83      */

84     public static File getFile(HttpServletRequest JavaDoc req) {
85         return (File) req.getAttribute(Aggregator.FILE);
86     }
87
88     /**
89      * <p>
90      * get Content object as requested from the URI
91      * </p>
92      * @param req HttpServletRequest as received in JSP or servlet
93      * @return currently active page, as requested from the URI
94      */

95     public static Content getCurrentActivePage(HttpServletRequest JavaDoc req) {
96         Content currentActpage;
97         currentActpage = (Content) req.getAttribute(Aggregator.CURRENT_ACTPAGE);
98         if (currentActpage == null) {
99             currentActpage = (Content) req.getAttribute(Aggregator.ACTPAGE);
100         }
101         return currentActpage;
102     }
103
104     /**
105      * Get HierarchyManager object from the request OR from the user session this hierarchy manager points to website
106      * repository, in order to swith between user and website repositories, use method (changeContext) on this object.
107      * @param req HttpServletRequest as received in JSP or servlet
108      * @return hierarchy manager, for the website repository
109      * @deprecated as on magnolia 2.0, use SessionAccessControl instead
110      * @see info.magnolia.cms.security.SessionAccessControl#getHierarchyManager(javax.servlet.http.HttpServletRequest)
111      */

112     public static HierarchyManager getHierarchyManager(HttpServletRequest JavaDoc req) {
113         return MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
114     }
115
116     /**
117      * <p>
118      * this only works for forms which uses enctype=multipart/form-data
119      * </p>
120      * @param req HttpServletRequest as received in JSP or servlet
121      * @return initialised multipart form object with the posted data
122      */

123     public static MultipartForm getPostedForm(HttpServletRequest JavaDoc req) {
124         return (MultipartForm) req.getAttribute("multipartform"); //$NON-NLS-1$
125
}
126
127     /**
128      * <p>
129      * get ContentNode object as passed to the include tag
130      * </p>
131      * @param req HttpServletRequest as received in JSP or servlet
132      * @return ContentNode , local container specific to the current JSP/Servlet paragraph
133      */

134     public static Content getLocalContentNode(HttpServletRequest JavaDoc req) {
135         try {
136             return (Content) req.getAttribute(Resource.LOCAL_CONTENT_NODE);
137         }
138         catch (Exception JavaDoc e) {
139             return null;
140         }
141     }
142
143     /**
144      * <p>
145      * set ContentNode object in resources , scope:TAG
146      * </p>
147      * @param req HttpServletRequest as received in JSP or servlet
148      * @param node to be set
149      */

150     public static void setLocalContentNode(HttpServletRequest JavaDoc req, Content node) {
151         req.setAttribute(Resource.LOCAL_CONTENT_NODE, node);
152     }
153
154     /**
155      * <p>
156      * removes ContentNode object in resources , scope:TAG
157      * </p>
158      * @param req HttpServletRequest as received in JSP or servlet
159      */

160     public static void removeLocalContentNode(HttpServletRequest JavaDoc req) {
161         req.removeAttribute(Resource.LOCAL_CONTENT_NODE);
162     }
163
164     /**
165      * <p>
166      * get ContentNode object as set by the "set" tag
167      * </p>
168      * @param req HttpServletRequest as received in JSP or servlet
169      * @return ContentNode , global container specific to the current JSP/Servlet page
170      */

171     public static Content getGlobalContentNode(HttpServletRequest JavaDoc req) {
172         try {
173             return (Content) req.getAttribute(Resource.GLOBAL_CONTENT_NODE);
174         }
175         catch (Exception JavaDoc e) {
176             return null;
177         }
178     }
179
180     /**
181      * <p>
182      * set ContentNode object in resources, scope:page
183      * </p>
184      * @param req HttpServletRequest as received in JSP or servlet
185      * @param node to be set
186      */

187     public static void setGlobalContentNode(HttpServletRequest JavaDoc req, Content node) {
188         req.setAttribute(Resource.GLOBAL_CONTENT_NODE, node);
189     }
190
191     /**
192      * <p>
193      * removes ContentNode object in resources , scope:page
194      * </p>
195      * @param req HttpServletRequest as received in JSP or servlet
196      */

197     public static void removeGlobalContentNode(HttpServletRequest JavaDoc req) {
198         req.removeAttribute(Resource.GLOBAL_CONTENT_NODE);
199     }
200
201     /**
202      *
203      */

204     public static void setLocalContentNodeCollectionName(HttpServletRequest JavaDoc req, String JavaDoc name) {
205         req.setAttribute(Resource.LOCAL_CONTENT_NODE_COLLECTION_NAME, name);
206     }
207
208     /**
209      *
210      */

211     public static String JavaDoc getLocalContentNodeCollectionName(HttpServletRequest JavaDoc req) {
212         try {
213             return (String JavaDoc) req.getAttribute(Resource.LOCAL_CONTENT_NODE_COLLECTION_NAME);
214         }
215         catch (Exception JavaDoc e) {
216             return StringUtils.EMPTY;
217         }
218     }
219
220     /**
221      *
222      */

223     public static void removeLocalContentNodeCollectionName(HttpServletRequest JavaDoc req) {
224         req.removeAttribute(Resource.LOCAL_CONTENT_NODE_COLLECTION_NAME);
225     }
226
227     /**
228      * Check for preview mode.
229      * @param req HttpServletRequest as received in JSP or servlet
230      * @return boolean , true if preview is enabled
231      */

232     public static boolean showPreview(HttpServletRequest JavaDoc req) {
233         // first check if its set in request scope
234
if (req.getParameter(MGNL_PREVIEW_ATTRIBUTE) != null) {
235             return BooleanUtils.toBoolean(req.getParameter(MGNL_PREVIEW_ATTRIBUTE));
236         }
237
238         HttpSession JavaDoc httpsession = req.getSession(false);
239         if (httpsession != null) {
240             return BooleanUtils.toBoolean((Boolean JavaDoc) httpsession.getAttribute(MGNL_PREVIEW_ATTRIBUTE));
241         }
242
243         return false;
244     }
245
246     /**
247      * Set the request's <code>actpage</code> attribute to <code>page</code>
248      */

249     public static void setCurrentActivePage(HttpServletRequest JavaDoc request, Content page) {
250         request.setAttribute(Aggregator.CURRENT_ACTPAGE, page);
251     }
252
253     /**
254      * Restores the request's original <code>actpage</code> attribute (i.e. the one specified by the request URI).
255      */

256     public static void restoreCurrentActivePage(HttpServletRequest JavaDoc request) {
257         setCurrentActivePage(request, getActivePage(request));
258     }
259
260 }
261
Popular Tags