org.opencms.file
Class CmsRequestContext

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

public final class CmsRequestContext
extends 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

Field Summary
static String ATTRIBUTE_EDITOR
          Request context attribute for indicating that an editor is currently open.
static String ATTRIBUTE_FULLLINKS
          Request context attribute for indicating we want full links generated for HTML fields.
static String ATTRIBUTE_MODEL
          Request context attribute for indicating the model file for a create resource operation.
static String ATTRIBUTE_NEW_RESOURCE_LOCALE
          Request context attribute for indicating content locale for a create resource operation.
 
Constructor Summary
CmsRequestContext(CmsUser user, CmsProject project, String requestedUri, String siteRoot, Locale locale, String encoding, String remoteAddr, long requestTime, CmsResourceTranslator directoryTranslator, CmsResourceTranslator fileTranslator, String ouFqn)
          Constructs a new request context.
 
Method Summary
 String addSiteRoot(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.
 String addSiteRoot(String siteRoot, 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()
          Deprecated. use getCurrentProject() instead
 CmsUser currentUser()
          Deprecated. use getCurrentUser() instead
 String getAdjustedSiteRoot(String resourcename)
          Returns the adjusted site root for a resoure this context current site root.
static String getAdjustedSiteRoot(String siteRoot, String resourcename)
          Returns the adjusted site root for a resource using the provided site root as a base.
 Object getAttribute(String attributeName)
          Gets the value of an attribute from the OpenCms request context attribute list.
 CmsProject getCurrentProject()
          Returns the current project of the current user.
 CmsUser getCurrentUser()
          Returns the current user object.
 CmsResourceTranslator getDirectoryTranslator()
          Returns the directory name translator this context was initialized with.
 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.
 String getFolderUri()
          Gets the name of the parent folder of the requested file.
 Locale getLocale()
          Returns the locale used by this request context.
 String getOuFqn()
          Returns the fully qualified name of the organizational unit.
 String getRemoteAddress()
          Returns the remote ip address.
 long getRequestTime()
          Returns the current request time.
 String getRootUri()
          Returns this request contexts uri extended with the current site root path.
 String getSitePath(CmsResource resource)
          Adjusts the absolute resource root path for the current site.
 String getSiteRoot()
          Returns the current root directory in the virtual file system.
 String getUri()
          Returns the OpenCms VFS URI of the requested resource.
 boolean isUpdateSessionEnabled()
          Check if this request context will update the session.
 Object removeAttribute(String key)
          Removes an attribute from the request context.
 String removeSiteRoot(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 setAttribute(String key, Object value)
          Sets an attribute in the request context.
 CmsProject setCurrentProject(CmsProject project)
          Sets the current project for the user.
 void setEncoding(String encoding)
          Sets the current content encoding to be used in HTTP response.
 void setLocale(Locale locale)
          Sets the locale used by this request context.
 void setOuFqn(String ouFqn)
          Sets the organizational unit fully qualified name.
 void setRequestTime(long time)
          Sets the current request time.
 void setSiteRoot(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(String value)
          Set the requested resource OpenCms VFS URI, that is the value returned by getUri().
protected  void switchUser(CmsUser user, CmsProject project, 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 String ATTRIBUTE_EDITOR
Request context attribute for indicating that an editor is currently open.


ATTRIBUTE_FULLLINKS

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


ATTRIBUTE_MODEL

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


ATTRIBUTE_NEW_RESOURCE_LOCALE

public static final String ATTRIBUTE_NEW_RESOURCE_LOCALE
Request context attribute for indicating content locale for a create resource operation.

Constructor Detail

CmsRequestContext

public CmsRequestContext(CmsUser user,
                         CmsProject project,
                         String requestedUri,
                         String siteRoot,
                         Locale locale,
                         String encoding,
                         String remoteAddr,
                         long requestTime,
                         CmsResourceTranslator directoryTranslator,
                         CmsResourceTranslator fileTranslator,
                         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 String getAdjustedSiteRoot(String siteRoot,
                                         String resourcename)
Returns the adjusted site root for a resource 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 resource

addSiteRoot

public String addSiteRoot(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 String addSiteRoot(String siteRoot,
                          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

@Deprecated
public CmsProject currentProject()
Deprecated. use getCurrentProject() instead

Returns the current project of the current user.

Returns:
the current project of the current user

currentUser

@Deprecated
public CmsUser currentUser()
Deprecated. use getCurrentUser() instead

Returns the current user object.

Returns:
the current user object

getAdjustedSiteRoot

public String getAdjustedSiteRoot(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 Object getAttribute(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

getCurrentProject

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

Returns:
the current project of the current user

getCurrentUser

public CmsUser getCurrentUser()
Returns the current user object.

Returns:
the current user object

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 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 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 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 String getOuFqn()
Returns the fully qualified name of the organizational unit.

Returns:
the fully qualified name of the organizational unit

getRemoteAddress

public 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

getRootUri

public String getRootUri()
Returns this request contexts uri extended with the current site root path.

Returns:
this request contexts uri extended with the current site root path
See Also:
getUri(), addSiteRoot(String)

getSitePath

public 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 String getSiteRoot()
Returns the current root directory in the virtual file system.

Returns:
the current root directory in the virtual file system

getUri

public 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 Object removeAttribute(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 String removeSiteRoot(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)

setAttribute

public void setAttribute(String key,
                         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(String encoding)
Sets the current content encoding to be used in HTTP response.

Parameters:
encoding - the encoding

setLocale

public void setLocale(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(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(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(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,
                          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