Class 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ATTRIBUTE_ADE_CONTEXT_PATH
      Request context attribute for the ADE context path.
      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.
      static java.lang.String ATTRIBUTE_NEW_RESOURCE_LOCALE
      Request context attribute for indicating content locale for a create resource operation.
    • Constructor Summary

      Constructors 
      Constructor Description
      CmsRequestContext​(CmsUser user, CmsProject project, java.lang.String requestedUri, CmsSiteMatcher requestMatcher, java.lang.String siteRoot, boolean isSecureRequest, java.util.Locale locale, java.lang.String encoding, java.lang.String remoteAddr, long requestTime, CmsResourceTranslator directoryTranslator, CmsResourceTranslator fileTranslator, java.lang.String ouFqn, boolean forceAbsoluteLinks)
      Constructs a new request context.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      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()
      Deprecated.
      CmsUser currentUser()
      Deprecated.
      use getCurrentUser() instead
      java.lang.String getAdjustedSiteRoot​(java.lang.String resourcename)
      Returns the adjusted site root for a resource 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 resource 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.
      CmsProject getCurrentProject()
      Returns the current project of the current user.
      CmsUser getCurrentUser()
      Returns the current user object.
      CmsUUID getDetailContentId()
      Gets the detail content structure id (or null if no detail content has been loaded).
      CmsResource getDetailResource()
      Gets the detail content resource (or null if no detail content has been loaded).
      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.
      CmsSiteMatcher getRequestMatcher()
      Returns the matcher for the current request, that is the host part of the URI from the original http request.
      long getRequestTime()
      Returns the current request time.
      java.lang.String getRootUri()
      Returns this request contexts uri extended with the current site root path.
      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 isForceAbsoluteLinks()
      Returns true if links to the current site should be generated as absolute links, i.e.
      boolean isSecureRequest()
      Returns true if this is a secure request.
      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 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 setDetailResource​(CmsResource detailResource)
      Sets the detail content resource.
      void setEncoding​(java.lang.String encoding)
      Sets the current content encoding to be used in HTTP response.
      void setForceAbsoluteLinks​(boolean forceAbsoluteLinks)
      Enables/disables link generation with full server prefix for the current site.
      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 setSecureRequest​(boolean secureRequest)
      Sets the 'secure request' status.
      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.
      • ATTRIBUTE_ADE_CONTEXT_PATH

        public static final java.lang.String ATTRIBUTE_ADE_CONTEXT_PATH
        Request context attribute for the ADE context path.
      • ATTRIBUTE_NEW_RESOURCE_LOCALE

        public static final java.lang.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,
                                 java.lang.String requestedUri,
                                 CmsSiteMatcher requestMatcher,
                                 java.lang.String siteRoot,
                                 boolean isSecureRequest,
                                 java.util.Locale locale,
                                 java.lang.String encoding,
                                 java.lang.String remoteAddr,
                                 long requestTime,
                                 CmsResourceTranslator directoryTranslator,
                                 CmsResourceTranslator fileTranslator,
                                 java.lang.String ouFqn,
                                 boolean forceAbsoluteLinks)
        Constructs a new request context.

        Parameters:
        user - the current user
        project - the current project
        requestedUri - the requested OpenCms VFS URI
        requestMatcher - the matcher for the current request, that is the host part of the URI from the original http request
        siteRoot - the users current site root
        isSecureRequest - true if this is a secure request
        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
        forceAbsoluteLinks - if true, links should be generated with a server prefix even if we're linking to the current site
    • Method Detail

      • getAdjustedSiteRoot

        public static java.lang.String getAdjustedSiteRoot​(java.lang.String siteRoot,
                                                           java.lang.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 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
      • getAdjustedSiteRoot

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

        Parameters:
        resourcename - the resource name to get the adjusted site root for
        Returns:
        the adjusted site root for the resource
        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
      • 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
      • getDetailContentId

        public CmsUUID getDetailContentId()
        Gets the detail content structure id (or null if no detail content has been loaded).

        Returns:
        the detail content id
      • getDetailResource

        public CmsResource getDetailResource()
        Gets the detail content resource (or null if no detail content has been loaded).

        Returns:
        the detail content resource
      • 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
      • 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
      • getRequestMatcher

        public CmsSiteMatcher getRequestMatcher()
        Returns the matcher for the current request, that is the host part of the URI from the original http request.

        Returns:
        the matcher for the current request, that is the host part of the URI from the original http request
      • getRequestTime

        public long getRequestTime()
        Returns the current request time.

        Returns:
        the current request time
      • getRootUri

        public java.lang.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)
      • 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
      • isForceAbsoluteLinks

        public boolean isForceAbsoluteLinks()
        Returns true if links to the current site should be generated as absolute links, i.e. with a server prefix.
        Returns:
        true if links to the current site should be absolute
      • isSecureRequest

        public boolean isSecureRequest()
        Returns true if this is a secure request.

        Returns:
        true if this is secure
      • 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)
      • 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
      • setDetailResource

        public void setDetailResource​(CmsResource detailResource)
        Sets the detail content resource.

        Parameters:
        detailResource - the detail content resource
      • setEncoding

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

        Parameters:
        encoding - the encoding
      • setForceAbsoluteLinks

        public void setForceAbsoluteLinks​(boolean forceAbsoluteLinks)
        Enables/disables link generation with full server prefix for the current site.
        Parameters:
        forceAbsoluteLinks - true if links to the current site should be generated with server prefix
      • 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
      • setSecureRequest

        public void setSecureRequest​(boolean secureRequest)
        Sets the 'secure request' status.

        Parameters:
        secureRequest - the new value
      • 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