KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > CmsRequestContext


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsRequestContext.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.29 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file;
33
34 import org.opencms.main.CmsRuntimeException;
35 import org.opencms.util.CmsResourceTranslator;
36 import org.opencms.workplace.CmsWorkplace;
37
38 import java.util.Hashtable JavaDoc;
39 import java.util.Locale JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * Stores the information about the current users OpenCms context,
44  * for example the requested URI, the current project, the selected site and more.<p>
45  *
46  * @author Alexander Kandzior
47  * @author Michael Emmerich
48  *
49  * @version $Revision: 1.29 $
50  *
51  * @since 6.0.0
52  */

53 public final class CmsRequestContext {
54
55     /** Request context attribute for indicating that an editor is currently open. */
56     public static final String JavaDoc ATTRIBUTE_EDITOR = "org.opencms.file.CmsRequestContext.ATTRIBUTE_EDITOR";
57
58     /** A map for storing (optional) request context attributes. */
59     private Map JavaDoc m_attributeMap;
60
61     /** The current project. */
62     private CmsProject m_currentProject;
63
64     /** Directory name translator. */
65     private CmsResourceTranslator m_directoryTranslator;
66
67     /** Current encoding. */
68     private String JavaDoc m_encoding;
69
70     /** File name translator. */
71     private CmsResourceTranslator m_fileTranslator;
72
73     /** The locale for this request. */
74     private Locale JavaDoc m_locale;
75
76     /** The remote ip address. */
77     private String JavaDoc m_remoteAddr;
78
79     /** The current request time. */
80     private long m_requestTime;
81
82     /** Used to save / restore a site root .*/
83     private String JavaDoc m_savedSiteRoot;
84
85     /** The name of the root, e.g. /site_a/vfs. */
86     private String JavaDoc m_siteRoot;
87
88     /** Flag to indicate that this context should not update the user session. */
89     private boolean m_updateSession;
90
91     /** The URI for getUri() in case it is "overwritten". */
92     private String JavaDoc m_uri;
93
94     /** The current user. */
95     private CmsUser m_user;
96
97     /**
98      * Constructs a new request context.<p>
99      *
100      * @param user the current user
101      * @param project the current project
102      * @param requestedUri the requested OpenCms VFS URI
103      * @param siteRoot the users current site root
104      * @param locale the users current locale
105      * @param encoding the encoding to use for this request
106      * @param remoteAddr the remote IP address of the user
107      * @param directoryTranslator the directory translator
108      * @param fileTranslator the file translator
109      */

110     public CmsRequestContext(
111         CmsUser user,
112         CmsProject project,
113         String JavaDoc requestedUri,
114         String JavaDoc siteRoot,
115         Locale JavaDoc locale,
116         String JavaDoc encoding,
117         String JavaDoc remoteAddr,
118         CmsResourceTranslator directoryTranslator,
119         CmsResourceTranslator fileTranslator) {
120
121         m_updateSession = true;
122         m_user = user;
123         m_currentProject = project;
124         m_uri = requestedUri;
125         setSiteRoot(siteRoot);
126         m_locale = locale;
127         m_encoding = encoding;
128         m_remoteAddr = remoteAddr;
129         m_directoryTranslator = directoryTranslator;
130         m_fileTranslator = fileTranslator;
131         m_requestTime = System.currentTimeMillis();
132     }
133
134     /**
135      * Returns the adjusted site root for a resoure using the provided site root as a base.<p>
136      *
137      * Usually, this would be the site root for the current site.
138      * However, if a resource from the <code>/system/</code> folder is requested,
139      * this will be the empty String.<p>
140      *
141      * @param siteRoot the site root to use
142      * @param resourcename the resource name to get the adjusted site root for
143      * @return the adjusted site root for the resoure
144      */

145     public static String JavaDoc getAdjustedSiteRoot(String JavaDoc siteRoot, String JavaDoc resourcename) {
146
147         if (resourcename.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
148             return "";
149         } else {
150             return siteRoot;
151         }
152     }
153
154     /**
155      * Adds the current site root of this context to the given resource name,
156      * and also translates the resource name with the configured the directory translator.<p>
157      *
158      * @param resourcename the resource name
159      * @return the translated resource name including site root
160      * @see #addSiteRoot(String, String)
161      */

162     public String JavaDoc addSiteRoot(String JavaDoc resourcename) {
163
164         return addSiteRoot(m_siteRoot, resourcename);
165     }
166
167     /**
168      * Adds the given site root of this context to the given resource name,
169      * taking into account special folders like "/system" where no site root must be added,
170      * and also translates the resource name with the configured the directory translator.<p>
171      *
172      * @param siteRoot the site root to add
173      * @param resourcename the resource name
174      * @return the translated resource name including site root
175      */

176     public String JavaDoc addSiteRoot(String JavaDoc siteRoot, String JavaDoc resourcename) {
177
178         if ((resourcename == null) || (siteRoot == null)) {
179             return null;
180         }
181         siteRoot = getAdjustedSiteRoot(siteRoot, resourcename);
182         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
183         result.append(siteRoot);
184         if (((siteRoot.length() == 0) || (siteRoot.charAt(siteRoot.length() - 1) != '/'))
185             && ((resourcename.length() == 0) || (resourcename.charAt(0) != '/'))) {
186             // add slash between site root and resource if required
187
result.append('/');
188         }
189         result.append(resourcename);
190         return m_directoryTranslator.translateResource(result.toString());
191     }
192
193     /**
194      * Returns the current project of the current user.
195      *
196      * @return the current project of the current user
197      */

198     public CmsProject currentProject() {
199
200         return m_currentProject;
201     }
202
203     /**
204      * Returns the current user object.<p>
205      *
206      * @return the current user object
207      */

208     public CmsUser currentUser() {
209
210         return m_user;
211     }
212
213     /**
214      * Returns the adjusted site root for a resoure this context current site root.<p>
215      *
216      * @param resourcename the resource name to get the adjusted site root for
217      * @return the adjusted site root for the resoure
218      * @see #getAdjustedSiteRoot(String, String)
219      */

220     public String JavaDoc getAdjustedSiteRoot(String JavaDoc resourcename) {
221
222         return getAdjustedSiteRoot(m_siteRoot, resourcename);
223     }
224
225     /**
226      * Gets the value of an attribute from the OpenCms request context attribute list.<p>
227      *
228      * @param attributeName the attribute name
229      * @return Object the attribute value, or <code>null</code> if the attribute was not found
230      */

231     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
232
233         if (m_attributeMap == null) {
234             return null;
235         }
236         return m_attributeMap.get(attributeName);
237     }
238
239     /**
240      * Returns the directory name translator this context was initialized with.<p>
241      *
242      * The directory translator is used to translate old VFS path information
243      * to a new location. Example: <code>/bodys/index.html --> /system/bodies/</code>.<p>
244      *
245      * @return the directory name translator this context was initialized with
246      */

247     public CmsResourceTranslator getDirectoryTranslator() {
248
249         return m_directoryTranslator;
250     }
251
252     /**
253      * Returns the current content encoding to be used in HTTP response.<p>
254      *
255      * @return the encoding
256      */

257     public String JavaDoc getEncoding() {
258
259         return m_encoding;
260     }
261
262     /**
263      * Returns the file name translator this context was initialized with.<p>
264      *
265      * The file name translator is used to translate filenames from uploaded files
266      * to valid OpenCms filenames. Example: <code>Wüste Wörter.doc --> Wueste_Woerter.doc</code>.<p>
267      *
268      * @return the file name translator this context was initialized with
269      */

270     public CmsResourceTranslator getFileTranslator() {
271
272         return m_fileTranslator;
273     }
274
275     /**
276      * Gets the name of the parent folder of the requested file.<p>
277      *
278      * @return the name of the parent folder of the requested file
279      */

280     public String JavaDoc getFolderUri() {
281
282         return getUri().substring(0, getUri().lastIndexOf("/") + 1);
283     }
284
285     /**
286      * Returns the name of the requested locale within this context.<p>
287      *
288      * @return the name of the locale
289      */

290     public Locale JavaDoc getLocale() {
291
292         return m_locale;
293     }
294
295     /**
296      * Returns the remote ip address.<p>
297      *
298      * @return the renote ip addresss as string
299      */

300     public String JavaDoc getRemoteAddress() {
301
302         return m_remoteAddr;
303     }
304
305     /**
306      * Returns the current request time.<p>
307      *
308      * @return the current request time
309      */

310     public long getRequestTime() {
311
312         return m_requestTime;
313     }
314
315     /**
316      * Adjusts the absolute resource root path for the current site.<p>
317      *
318      * The full root path of a resource is always available using
319      * <code>{@link CmsResource#getRootPath()}</code>. From this name this method cuts
320      * of the current site root using
321      * <code>{@link CmsRequestContext#removeSiteRoot(String)}</code>.<p>
322      *
323      * If the resource root path does not start with the current site root,
324      * it is left untouched.<p>
325      *
326      * @param resource the resource to get the adjusted site root path for
327      *
328      * @return the absolute resource path adjusted for the current site
329      *
330      * @see #removeSiteRoot(String)
331      * @see CmsResource#getRootPath()
332      * @see CmsObject#getSitePath(CmsResource)
333      */

334     public String JavaDoc getSitePath(CmsResource resource) {
335
336         return removeSiteRoot(resource.getRootPath());
337     }
338
339     /**
340      * Returns the current root directory in the virtual file system.<p>
341      *
342      * @return the current root directory in the virtual file system
343      */

344     public String JavaDoc getSiteRoot() {
345
346         return m_siteRoot;
347     }
348
349     /**
350      * Returns the OpenCms VFS URI of the requested resource.<p>
351      *
352      * @return the OpenCms VFS URI of the requested resource
353      */

354     public String JavaDoc getUri() {
355
356         return m_uri;
357     }
358
359     /**
360      * Check if this request context will update the session.<p>
361      *
362      * This is used mainly for CmsReports that continue to use the
363      * users context, even after the http request is already finished.<p>
364      *
365      * @return true if this request context will update the session, false otherwise
366      */

367     public boolean isUpdateSessionEnabled() {
368
369         return m_updateSession;
370     }
371
372     /**
373      * Removes the current site root prefix from the absolute path in the resource name,
374      * that is adjusts the resource name for the current site root.<p>
375      *
376      * If the resource name does not start with the current site root,
377      * it is left untouched.<p>
378      *
379      * @param resourcename the resource name
380      *
381      * @return the resource name adjusted for the current site root
382      *
383      * @see #getSitePath(CmsResource)
384      */

385     public String JavaDoc removeSiteRoot(String JavaDoc resourcename) {
386
387         String JavaDoc siteRoot = getAdjustedSiteRoot(m_siteRoot, resourcename);
388         if ((siteRoot == m_siteRoot) && resourcename.startsWith(siteRoot)) {
389             resourcename = resourcename.substring(siteRoot.length());
390         }
391         return resourcename;
392     }
393
394     /**
395      * Restores the saved site root.<p>
396      *
397      * @throws RuntimeException in case there is no site root saved
398      */

399     public void restoreSiteRoot() throws RuntimeException JavaDoc {
400
401         if (m_savedSiteRoot == null) {
402             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_EMPTY_SITEROOT_0));
403         }
404         m_siteRoot = m_savedSiteRoot;
405         m_savedSiteRoot = null;
406     }
407
408     /**
409      * Saves the current site root.<p>
410      *
411      * @throws RuntimeException in case there is already a site root saved
412      */

413     public void saveSiteRoot() throws RuntimeException JavaDoc {
414
415         if (m_savedSiteRoot != null) {
416             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NONEMPTY_SITEROOT_1, m_savedSiteRoot));
417         }
418         m_savedSiteRoot = m_siteRoot;
419     }
420
421     /**
422      * Sets an attribute in the request context.<p>
423      *
424      * @param key the attribute name
425      * @param value the attribute value
426      */

427     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
428
429         if (m_attributeMap == null) {
430             // Hashtable is still the most efficient form of a synchronized Map
431
m_attributeMap = new Hashtable JavaDoc();
432         }
433         m_attributeMap.put(key, value);
434     }
435
436     /**
437      * Sets the current project for the user.<p>
438      *
439      * @param project the project to be set as current project
440      * @return the CmsProject instance
441      */

442     public CmsProject setCurrentProject(CmsProject project) {
443
444         if (project != null) {
445             m_currentProject = project;
446         }
447         return m_currentProject;
448     }
449
450     /**
451      * Sets the current content encoding to be used in HTTP response.<p>
452      *
453      * @param encoding the encoding
454      */

455     public void setEncoding(String JavaDoc encoding) {
456
457         m_encoding = encoding;
458     }
459
460     /**
461      * Sets the current request time.<p>
462      *
463      * @param time the request time
464      */

465     public void setRequestTime(long time) {
466
467         m_requestTime = time;
468     }
469
470     /**
471      * Sets the current root directory in the virtual file system.<p>
472      *
473      * @param root the name of the new root directory
474      */

475     public void setSiteRoot(String JavaDoc root) {
476
477         // site roots must never end with a "/"
478
if (root.endsWith("/")) {
479             m_siteRoot = root.substring(0, root.length() - 1);
480         } else {
481             m_siteRoot = root;
482         }
483     }
484
485     /**
486      * Mark this request context to update the session or not.<p>
487      *
488      * @param value true if this request context will update the session, false otherwise
489      */

490     public void setUpdateSessionEnabled(boolean value) {
491
492         m_updateSession = value;
493     }
494
495     /**
496      * Set the requested resource OpenCms VFS URI, that is the value returned by {@link #getUri()}.<p>
497      *
498      * Use this with caution! Many things (caches etc.) depend on this value.
499      * If you change this value, better make sure that you change it only temporarily
500      * and reset it in a <code>try { // do something // } finaly { // reset URI // }</code> statement.<p>
501      *
502      * @param value the value to set the Uri to, must be a complete OpenCms path name like /system/workplace/stlye.css
503      */

504     public void setUri(String JavaDoc value) {
505
506         m_uri = value;
507     }
508
509     /**
510      * Switches the user in the context, required after a login.<p>
511      *
512      * @param user the new user to use
513      * @param project the new users current project
514      */

515     protected void switchUser(CmsUser user, CmsProject project) {
516
517         m_user = user;
518         m_currentProject = project;
519     }
520 }
Popular Tags