org.opencms.file
Class CmsRequestContext

java.lang.Object
  extended by org.opencms.file.CmsRequestContext

public final class CmsRequestContext
extends java.lang.Object

Stores the information about the current users OpenCms context, for example the requested URI, the current project, the selected site and more.

Since:
6.0.0
Version:
$Revision: 1.41 $
Author:
Alexander Kandzior, Michael Emmerich

Field Summary
static java.lang.String ATTRIBUTE_EDITOR
          Request context attribute for indicating that an editor is currently open.
static java.lang.String ATTRIBUTE_FULLLINKS
          Request context attribute for indicating we want full links generated for HTML fields.
static java.lang.String ATTRIBUTE_MODEL
          Request context attribute for indicating the model file for a create resource operation.
 
Constructor Summary
CmsRequestContext(CmsUser user, CmsProject project, java.lang.String requestedUri, java.lang.String siteRoot, java.util.Locale locale, java.lang.String encoding, java.lang.String remoteAddr, long requestTime, CmsResourceTranslator directoryTranslator, CmsResourceTranslator fileTranslator, java.lang.String ouFqn)
          Constructs a new request context.
 
Method Summary
 java.lang.String addSiteRoot(java.lang.String resourcename)
          Adds the current site root of this context to the given resource name, and also translates the resource name with the configured the directory translator.
 java.lang.String addSiteRoot(java.lang.String siteRoot, java.lang.String resourcename)
          Adds the given site root of this context to the given resource name, taking into account special folders like "/system" where no site root must be added, and also translates the resource name with the configured the directory translator.
 CmsProject currentProject()
          Returns the current project of the current user.
 CmsUser currentUser()
          Returns the current user object.
 java.lang.String getAdjustedSiteRoot(java.lang.String resourcename)
          Returns the adjusted site root for a resoure this context current site root.
static java.lang.String getAdjustedSiteRoot(java.lang.String siteRoot, java.lang.String resourcename)
          Returns the adjusted site root for a resoure using the provided site root as a base.
 java.lang.Object getAttribute(java.lang.String attributeName)
          Gets the value of an attribute from the OpenCms request context attribute list.
 CmsResourceTranslator getDirectoryTranslator()
          Returns the directory name translator this context was initialized with.
 java.lang.String getEncoding()
          Returns the current content encoding to be used in HTTP response.
 CmsResourceTranslator getFileTranslator()
          Returns the file name translator this context was initialized with.
 java.lang.String getFolderUri()
          Gets the name of the parent folder of the requested file.
 java.util.Locale getLocale()
          Returns the locale used by this request context.
 java.lang.String getOuFqn()
          Returns the fully qualified name of the organizational unit.
 java.lang.String getRemoteAddress()
          Returns the remote ip address.
 long getRequestTime()
          Returns the current request time.
 java.lang.String getSitePath(CmsResource resource)
          Adjusts the absolute resource root path for the current site.
 java.lang.String getSiteRoot()
          Returns the current root directory in the virtual file system.
 java.lang.String getUri()
          Returns the OpenCms VFS URI of the requested resource.
 boolean isUpdateSessionEnabled()
          Check if this request context will update the session.
 java.lang.Object removeAttribute(java.lang.String key)
          Removes an attribute from the request context.
 java.lang.String removeSiteRoot(java.lang.String resourcename)
          Removes the current site root prefix from the absolute path in the resource name, that is adjusts the resource name for the current site root.
 void restoreSiteRoot()
          Deprecated. store the current site root locally before switching to another site, and set it back again at the end, like:
             String storedSite = context.getSiteRoot();
             try {
                  context.setSiteRoot("");
                  // do something with the context
             } finally {
                  context.setSiteRoot(storedSite);
             }
 
 void saveSiteRoot()
          Deprecated. store the current site root locally before switching to another site, and set it back again at the end, like:
             String storedSite = context.getSiteRoot();
             try {
                  context.setSiteRoot("");
                  // do something with the context
             } finally {
                  context.setSiteRoot(storedSite);
             }
 
 void setAttribute(java.lang.String key, java.lang.Object value)
          Sets an attribute in the request context.
 CmsProject setCurrentProject(CmsProject project)
          Sets the current project for the user.
 void setEncoding(java.lang.String encoding)
          Sets the current content encoding to be used in HTTP response.
 void setLocale(java.util.Locale locale)
          Sets the locale used by this request context.
 void setOuFqn(java.lang.String ouFqn)
          Sets the organizational unit fully qualified name.
 void setRequestTime(long time)
          Sets the current request time.
 void setSiteRoot(java.lang.String root)
          Sets the current root directory in the virtual file system.
 void setUpdateSessionEnabled(boolean value)
          Mark this request context to update the session or not.
 void setUri(java.lang.String value)
          Set the requested resource OpenCms VFS URI, that is the value returned by getUri().
protected  void switchUser(CmsUser user, CmsProject project, java.lang.String ouFqn)
          Switches the user in the context, required after a login.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTRIBUTE_EDITOR

public static final java.lang.String ATTRIBUTE_EDITOR
Request context attribute for indicating that an editor is currently open.


ATTRIBUTE_FULLLINKS

public static final java.lang.String ATTRIBUTE_FULLLINKS
Request context attribute for indicating we want full links generated for HTML fields.


ATTRIBUTE_MODEL

public static final java.lang.String ATTRIBUTE_MODEL
Request context attribute for indicating the model file for a create resource operation.

Constructor Detail

CmsRequestContext

public CmsRequestContext(CmsUser user,
                         CmsProject project,
                         java.lang.String requestedUri,
                         java.lang.String siteRoot,
                         java.util.Locale locale,
                         java.lang.String encoding,
                         java.lang.String remoteAddr,
                         long requestTime,
                         CmsResourceTranslator directoryTranslator,
                         CmsResourceTranslator fileTranslator,
                         java.lang.String ouFqn)
Constructs a new request context.

Parameters:
user - the current user
project - the current project
requestedUri - the requested OpenCms VFS URI
siteRoot - the users current site root
locale - the users current locale
encoding - the encoding to use for this request
remoteAddr - the remote IP address of the user
requestTime - the time of the request (used for resource publication / expiration date)
directoryTranslator - the directory translator
fileTranslator - the file translator
ouFqn - the fully qualified name of the organizational unit
Method Detail

getAdjustedSiteRoot

public static java.lang.String getAdjustedSiteRoot(java.lang.String siteRoot,
                                                   java.lang.String resourcename)
Returns the adjusted site root for a resoure using the provided site root as a base.

Usually, this would be the site root for the current site. However, if a resource from the /system/ folder is requested, this will be the empty String.

Parameters:
siteRoot - the site root of the current site
resourcename - the resource name to get the adjusted site root for
Returns:
the adjusted site root for the resoure

addSiteRoot

public java.lang.String addSiteRoot(java.lang.String resourcename)
Adds the current site root of this context to the given resource name, and also translates the resource name with the configured the directory translator.

Parameters:
resourcename - the resource name
Returns:
the translated resource name including site root
See Also:
addSiteRoot(String, String)

addSiteRoot

public java.lang.String addSiteRoot(java.lang.String siteRoot,
                                    java.lang.String resourcename)
Adds the given site root of this context to the given resource name, taking into account special folders like "/system" where no site root must be added, and also translates the resource name with the configured the directory translator.

Parameters:
siteRoot - the site root to add
resourcename - the resource name
Returns:
the translated resource name including site root

currentProject

public CmsProject currentProject()
Returns the current project of the current user.

Returns:
the current project of the current user

currentUser

public CmsUser currentUser()
Returns the current user object.

Returns:
the current user object

getAdjustedSiteRoot

public java.lang.String getAdjustedSiteRoot(java.lang.String resourcename)
Returns the adjusted site root for a resoure this context current site root.

Parameters:
resourcename - the resource name to get the adjusted site root for
Returns:
the adjusted site root for the resoure
See Also:
getAdjustedSiteRoot(String, String)

getAttribute

public java.lang.Object getAttribute(java.lang.String attributeName)
Gets the value of an attribute from the OpenCms request context attribute list.

Parameters:
attributeName - the attribute name
Returns:
Object the attribute value, or null if the attribute was not found

getDirectoryTranslator

public CmsResourceTranslator getDirectoryTranslator()
Returns the directory name translator this context was initialized with.

The directory translator is used to translate old VFS path information to a new location. Example: /bodys/index.html --> /system/bodies/.

Returns:
the directory name translator this context was initialized with

getEncoding

public java.lang.String getEncoding()
Returns the current content encoding to be used in HTTP response.

Returns:
the encoding

getFileTranslator

public CmsResourceTranslator getFileTranslator()
Returns the file name translator this context was initialized with.

The file name translator is used to translate filenames from uploaded files to valid OpenCms filenames. Example: Wüste Wörter.doc --> Wueste_Woerter.doc.

Returns:
the file name translator this context was initialized with

getFolderUri

public java.lang.String getFolderUri()
Gets the name of the parent folder of the requested file.

Returns:
the name of the parent folder of the requested file

getLocale

public java.util.Locale getLocale()
Returns the locale used by this request context.

In normal operation, the request context locale is initialized using I_CmsLocaleHandler.getI18nInfo(javax.servlet.http.HttpServletRequest, CmsUser, CmsProject, String) depending on the requested resource URI.

Returns:
the locale used by this request context
See Also:
I_CmsLocaleHandler.getI18nInfo(javax.servlet.http.HttpServletRequest, CmsUser, CmsProject, String), CmsLocaleManager.getDefaultLocale(CmsObject, String)

getOuFqn

public java.lang.String getOuFqn()
Returns the fully qualified name of the organizational unit.

Returns:
the fully qualified name of the organizational unit

getRemoteAddress

public java.lang.String getRemoteAddress()
Returns the remote ip address.

Returns:
the remote ip address as string

getRequestTime

public long getRequestTime()
Returns the current request time.

Returns:
the current request time

getSitePath

public java.lang.String getSitePath(CmsResource resource)
Adjusts the absolute resource root path for the current site.

The full root path of a resource is always available using CmsResource.getRootPath(). From this name this method cuts of the current site root using removeSiteRoot(String).

If the resource root path does not start with the current site root, it is left untouched.

Parameters:
resource - the resource to get the adjusted site root path for
Returns:
the absolute resource path adjusted for the current site
See Also:
removeSiteRoot(String), CmsResource.getRootPath(), CmsObject.getSitePath(CmsResource)

getSiteRoot

public java.lang.String getSiteRoot()
Returns the current root directory in the virtual file system.

Returns:
the current root directory in the virtual file system

getUri

public java.lang.String getUri()
Returns the OpenCms VFS URI of the requested resource.

Returns:
the OpenCms VFS URI of the requested resource

isUpdateSessionEnabled

public boolean isUpdateSessionEnabled()
Check if this request context will update the session.

This is used mainly for CmsReports that continue to use the users context, even after the http request is already finished.

Returns:
true if this request context will update the session, false otherwise

removeAttribute

public java.lang.Object removeAttribute(java.lang.String key)
Removes an attribute from the request context.

Parameters:
key - the name of the attribute to remove
Returns:
the removed attribute, or null if no attribute was set with this name

removeSiteRoot

public java.lang.String removeSiteRoot(java.lang.String resourcename)
Removes the current site root prefix from the absolute path in the resource name, that is adjusts the resource name for the current site root.

If the resource name does not start with the current site root, it is left untouched.

Parameters:
resourcename - the resource name
Returns:
the resource name adjusted for the current site root
See Also:
getSitePath(CmsResource)

restoreSiteRoot

public void restoreSiteRoot()
                     throws java.lang.RuntimeException
Deprecated. store the current site root locally before switching to another site, and set it back again at the end, like:
             String storedSite = context.getSiteRoot();
             try {
                  context.setSiteRoot("");
                  // do something with the context
             } finally {
                  context.setSiteRoot(storedSite);
             }
 

Restores the saved site root.

Throws:
java.lang.RuntimeException - in case there is no site root saved

saveSiteRoot

public void saveSiteRoot()
                  throws java.lang.RuntimeException
Deprecated. store the current site root locally before switching to another site, and set it back again at the end, like:
             String storedSite = context.getSiteRoot();
             try {
                  context.setSiteRoot("");
                  // do something with the context
             } finally {
                  context.setSiteRoot(storedSite);
             }
 

Saves the current site root.

Throws:
java.lang.RuntimeException - in case there is already a site root saved

setAttribute

public void setAttribute(java.lang.String key,
                         java.lang.Object value)
Sets an attribute in the request context.

Parameters:
key - the attribute name
value - the attribute value

setCurrentProject

public CmsProject setCurrentProject(CmsProject project)
Sets the current project for the user.

Parameters:
project - the project to be set as current project
Returns:
the CmsProject instance

setEncoding

public void setEncoding(java.lang.String encoding)
Sets the current content encoding to be used in HTTP response.

Parameters:
encoding - the encoding

setLocale

public void setLocale(java.util.Locale locale)
Sets the locale used by this request context.

Parameters:
locale - the locale to set
See Also:
for more information about how the locale is set in normal operation

setOuFqn

public void setOuFqn(java.lang.String ouFqn)
Sets the organizational unit fully qualified name.

Parameters:
ouFqn - the organizational unit fully qualified name

setRequestTime

public void setRequestTime(long time)
Sets the current request time.

Parameters:
time - the request time

setSiteRoot

public void setSiteRoot(java.lang.String root)
Sets the current root directory in the virtual file system.

Parameters:
root - the name of the new root directory

setUpdateSessionEnabled

public void setUpdateSessionEnabled(boolean value)
Mark this request context to update the session or not.

Parameters:
value - true if this request context will update the session, false otherwise

setUri

public void setUri(java.lang.String value)
Set the requested resource OpenCms VFS URI, that is the value returned by getUri().

Use this with caution! Many things (caches etc.) depend on this value. If you change this value, better make sure that you change it only temporarily and reset it in a try { // do something // } finally { // reset URI // } statement.

Parameters:
value - the value to set the Uri to, must be a complete OpenCms path name like /system/workplace/style.css

switchUser

protected void switchUser(CmsUser user,
                          CmsProject project,
                          java.lang.String ouFqn)
Switches the user in the context, required after a login.

Parameters:
user - the new user to use
project - the new users current project
ouFqn - the organizational unit