Class CmsRequestContext

java.lang.Object
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 Details

    • ATTRIBUTE_ADE_CONTEXT_PATH

      public static final String ATTRIBUTE_ADE_CONTEXT_PATH
      Request context attribute for the ADE context path (should be a root path).
    • ATTRIBUTE_EDITOR

      public static final String ATTRIBUTE_EDITOR
      Request context attribute for indicating that an editor is currently open.
    • 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

      Request context attribute for indicating content locale for a create resource operation.
  • Constructor Details

    • CmsRequestContext

      public CmsRequestContext(CmsUser user, CmsProject project, String requestedUri, CmsSiteMatcher requestMatcher, String siteRoot, boolean isSecureRequest, Locale locale, String encoding, String remoteAddr, long requestTime, CmsResourceTranslator directoryTranslator, CmsResourceTranslator fileTranslator, 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 Details

    • 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

      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.
      Returns the current project of the current user.
      Returns:
      the current project of the current user
    • 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 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:
    • 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

      Returns the current project of the current user.
      Returns:
      the current project of the current user
    • getCurrentUser

      Returns the current user object.

      Returns:
      the current user object
    • getDetailContentId

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

      Returns:
      the detail content id
    • getDetailResource

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

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

      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:
    • getOuFqn

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

      Returns:
      the fully qualified name of the organizational unit
    • getRemoteAddress

      Returns the remote ip address.

      Returns:
      the remote ip address as string
    • 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 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:
    • 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:
    • 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
    • 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
    • isOnlineOrEditDisabled

      public boolean isOnlineOrEditDisabled()
      Checks if we are currently either in the Online project or the 'direct edit disabled' mode.
      Returns:
      true if we are online or in 'direct edit enabled' mode
    • 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

      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:
    • 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

      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(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
    • setLocale

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

      Parameters:
      locale - the locale to set
      See Also:
    • 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
    • setSecureRequest

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

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