Class CmsStaticExportManager

java.lang.Object
org.opencms.staticexport.CmsStaticExportManager
All Implemented Interfaces:
I_CmsEventListener

public class CmsStaticExportManager extends Object implements I_CmsEventListener
Provides the functionality to export resources from the OpenCms VFS to the file system.

Since:
6.0.0
  • Field Details

  • Constructor Details

  • Method Details

    • getRfsPath

      public static String getRfsPath(String filename, String extension, String parameters)
      Creates unique, valid RFS name for the given filename that contains a coded version of the given parameters, with the given file extension appended.

      Adapted from CmsFileUtil.getRfsPath().

      Parameters:
      filename - the base file name
      extension - the extension to use
      parameters - the parameters to code in the result file name
      Returns:
      a unique, valid RFS name for the given parameters
      See Also:
    • addDefaultFileNameToFolder

      public String addDefaultFileNameToFolder(String rfsName, boolean isFolder)
      Returns the real file system name plus the default file name.

      Parameters:
      rfsName - the real file system name to append the default file name to
      isFolder - signals whether the according virtual file system resource is an folder or not
      Returns:
      the real file system name plus the default file name
    • addExportRule

      public void addExportRule(String name, String description)
      Adds a new export rule to the configuration.

      Parameters:
      name - the name of the rule
      description - the description for the rule
    • addExportRuleRegex

      public void addExportRuleRegex(String regex)
      Adds a regex to the latest export rule.

      Parameters:
      regex - the regex to add
    • addExportRuleUri

      public void addExportRuleUri(String exportUri)
      Adds a export uri to the latest export rule.

      Parameters:
      exportUri - the export uri to add
    • addProtectedExportPoint

      public void addProtectedExportPoint(String uri, String destination)
      Adds an protected export point.

      Parameters:
      uri - the source URI
      destination - the export destination
    • addRfsRule

      public void addRfsRule(String name, String description, String source, String rfsPrefix, String exportPath, String exportWorkPath, String exportBackups, String useRelativeLinks)
      Adds a new rfs rule to the configuration.

      Parameters:
      name - the name of the rule
      description - the description for the rule
      source - the source regex
      rfsPrefix - the url prefix
      exportPath - the rfs export path
      exportWorkPath - the rfs export work path
      exportBackups - the number of backups
      useRelativeLinks - the relative links value
    • addRfsRuleSystemRes

      public void addRfsRuleSystemRes(String regex)
      Adds a regex of related system resources to the latest rfs-rule.

      Parameters:
      regex - the regex to add
    • cacheOnlineLink

      public void cacheOnlineLink(String linkName, String vfsName)
      Caches a calculated online link.

      Parameters:
      linkName - the link
      vfsName - the name of the VFS resource
    • cmsEvent

      public void cmsEvent(CmsEvent event)
      Implements the CmsEvent interface, the static export properties uses the events to clear the list of cached keys in case a project is published.

      Specified by:
      cmsEvent in interface I_CmsEventListener
      Parameters:
      event - CmsEvent that has occurred
    • export

      public int export(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, CmsObject cms, CmsStaticExportData data) throws CmsException, IOException, javax.servlet.ServletException, CmsStaticExportException
      Exports the requested uri and at the same time writes the uri to the response output stream if required.

      Parameters:
      req - the current request
      res - the current response
      cms - an initialised cms context (should be initialised with the "Guest" user only)
      data - the static export data set
      Returns:
      status code of the export operation, status codes are the same as http status codes (200,303,304)
      Throws:
      CmsException - in case of errors accessing the VFS
      javax.servlet.ServletException - in case of errors accessing the servlet
      IOException - in case of errors writing to the export output stream
      CmsStaticExportException - if static export is disabled
    • exportFullStaticRender

      public void exportFullStaticRender(boolean purgeFirst, I_CmsReport report) throws CmsException, IOException, javax.servlet.ServletException
      Starts a complete static export of all resources.

      Parameters:
      purgeFirst - flag to delete all resources in the export folder of the rfs
      report - an I_CmsReport instance to print output message, or null to write messages to the log file
      Throws:
      CmsException - in case of errors accessing the VFS
      IOException - in case of errors writing to the export output stream
      javax.servlet.ServletException - in case of errors accessing the servlet
    • getAcceptCharsetHeader

      Returns the accept-charset header used for internal requests.

      Returns:
      the accept-charset header
    • getAcceptLanguageHeader

      Returns the accept-language header used for internal requests.

      Returns:
      the accept-language header
    • getCachedOnlineLink

      public String getCachedOnlineLink(String vfsName)
      Returns a cached link for the given vfs name.

      Parameters:
      vfsName - the name of the vfs resource to get the cached link for
      Returns:
      a cached link for the given vfs name, or null
    • getCacheKey

      public String getCacheKey(String siteRoot, String uri)
      Returns the key for the online, export and secure cache.

      Parameters:
      siteRoot - the site root of the resource
      uri - the URI of the resource
      Returns:
      a key for the cache
    • getDefault

      public String getDefault()
      Gets the default property value as a string representation.

      Returns:
      "true" or "false"
    • getDefaultAcceptCharsetHeader

      Returns the current default charset header.

      Returns:
      the current default charset header
    • getDefaultAcceptLanguageHeader

      Returns the current default locale header.

      Returns:
      the current default locale header
    • getDefaultRfsPrefix

      Returns the default prefix for exported links in the "real" file system.

      Returns:
      the default prefix for exported links in the "real" file system
    • getExportBackups

      Returns the number of stored backups.

      Returns:
      the number of stored backups
    • getExportData

      public CmsStaticExportData getExportData(javax.servlet.http.HttpServletRequest request, CmsObject cms)
      Returns the export data for the request, if null is returned no export is required.

      Parameters:
      request - the request to check for export data
      cms - an initialized cms context (should be initialized with the "Guest" user only
      Returns:
      the export data for the request, if null is returned no export is required
    • getExportEnabled

      Gets the export enabled value as a string representation.

      Returns:
      "true" or "false"
    • getExportFolderMatcher

      Returns the current folder matcher.

      Returns:
      the current folder matcher
    • getExportFolderPatterns

      Returns list of resources patterns which are part of the export.

      Returns:
      the of resources patterns which are part of the export.
    • getExportHeaders

      Returns specific http headers for the static export.

      If the header Cache-Control is set, OpenCms will not use its default headers.

      Returns:
      the list of http export headers
    • getExportnames

      Returns a map of all export names with export name as key and the vfs folder path as value.

      Returns:
      a map of export names
    • getExportPath

      public String getExportPath(String vfsName)
      Returns the export path for the static export, that is the folder where the static exported resources will be written to.

      The returned value will be a directory like prefix. The value is configured in the opencms-importexport.xml configuration file. An optimization of the configured value will be performed, where all relative path information is resolved (for example /export/../static will be resolved to /export. Moreover, if the configured path ends with a /, this will be cut off (for example /export/ becomes /export.

      This is resource name based, and based on the rfs-rules defined in the opencms-importexport.xml configuration file.

      Parameters:
      vfsName - the name of the resource to export
      Returns:
      the export path for the static export, that is the folder where the
      See Also:
    • getExportPathForConfiguration

      Returns the original configured export path for the static export without the complete rfs path, to be used when re-writing the configuration.

      This is required only to serialize the configuration again exactly as it was configured. This method should not be used otherwise. Use getExportPath(String) to obtain the export path to use when exporting.

      Returns:
      the original configured export path for the static export without the complete rfs path
    • getExportPoints

      Returns the protected export points.

      Returns:
      the protected export points
    • getExportPropertyDefault

      public boolean getExportPropertyDefault()
      Returns true if the default value for the resource property "export" is true.

      Returns:
      true if the default value for the resource property "export" is true
    • getExportRules

      Returns the export Rules.

      Returns:
      the export Rules
    • getExportSuffixes

      Gets the list of resource suffixes which will be exported by default.

      Returns:
      list of resource suffixes
    • getExportUrl

      public String getExportUrl()
      Returns the export URL used for internal requests for exporting resources that require a request / response (like JSP).

      Returns:
      the export URL used for internal requests for exporting resources like JSP
    • getExportUrlForConfiguration

      Returns the export URL used for internal requests with unsubstituted context values, to be used when re-writing the configuration.

      This is required only to serialize the configuration again exactly as it was configured. This method should not be used otherwise. Use getExportUrl() to obtain the export path to use when exporting.

      Returns:
      the export URL used for internal requests with unsubstituted context values
    • getExportUrlPrefix

      Returns the export URL used for internal requests for exporting resources that require a request / response (like JSP) without http://servername.

      Returns:
      the export URL used for internal requests for exporting resources like JSP without http://servername
    • getExportWorkPath

      Returns the export work path for the static export, that is the folder where the static exported resources will be written to during the export process.

      Returns:
      the export work path for the static export
    • getExportWorkPathForConfiguration

      Returns the original configured export work path for the static export without the complete rfs path, to be used when re-writing the configuration.

      Returns:
      the original configured export work path for the static export without the complete rfs path
    • getHandler

      Returns the configured static export handler class.

      If not set, a new CmsAfterPublishStaticExportHandler is created and returned.

      Returns:
      the configured static export handler class
    • getLinkSubstitutionHandler

      Returns the configured link substitution handler class.

      If not set, a new CmsDefaultLinkSubstitutionHandler is created and returned.

      Returns:
      the configured link substitution handler class
    • getPlainExportOptimization

      Gets the plain export optimization value as a string representation.

      Returns:
      "true" or "false"
    • getProtectedExportName

      Returns the protected export name for the given root path.

      Parameters:
      rootPath - the root path
      Returns:
      the protected export name
    • getProtectedExportPath

      Returns the protected export path.

      Returns:
      the protected export path
    • getProtectedExportPoints

      Returns the protected export points.

      Returns:
      the protected export points
    • getQuickPlainExport

      public boolean getQuickPlainExport()
      Returns true if the quick plain export is enabled.

      Returns:
      true if the quick plain export is enabled
    • getRelativeLinks

      Gets the relative links value as a string representation.

      Returns:
      "true" or "false"
    • getRemoteAddr

      Returns the remote address used for internal requests.

      Returns:
      the remote address
    • getRemoteAddress

      Returns the remote address.

      Returns:
      the remote address
    • getRfsName

      public String getRfsName(CmsObject cms, String vfsName)
      Returns the static export rfs name for a given vfs resource.

      Parameters:
      cms - an initialized cms context
      vfsName - the name of the vfs resource
      Returns:
      the static export rfs name for a give vfs resource
      See Also:
    • getRfsName

      public String getRfsName(CmsObject cms, String vfsName, String parameters, String targetDetailPage)
      Returns the static export rfs name for a given vfs resource where the link to the resource includes request parameters.

      Parameters:
      cms - an initialized cms context
      vfsName - the name of the vfs resource
      parameters - the parameters of the link pointing to the resource
      targetDetailPage - the target detail page to use
      Returns:
      the static export rfs name for a give vfs resource
    • getRfsPrefix

      public String getRfsPrefix(String vfsName)
      Returns the prefix for exported links in the "real" file system.

      The returned value will be a directory like prefix. The value is configured in the opencms-importexport.xml configuration file. An optimization of the configured value will be performed, where all relative path information is resolved (for example /export/../static will be resolved to /export. Moreover, if the configured path ends with a /, this will be cut off (for example /export/ becomes /export.

      This is resource name based, and based on the rfs-rules defined in the opencms-importexport.xml configuration file.

      Parameters:
      vfsName - the name of the resource to export
      Returns:
      the prefix for exported links in the "real" file system
      See Also:
    • getRfsPrefixForConfiguration

      Returns the original configured prefix for exported links in the "real" file, to be used when re-writing the configuration.

      This is required only to serialize the configuration again exactly as it was configured. This method should not be used otherwise. Use getRfsPrefix(String) to obtain the rfs prefix to use for the exported links.

      Returns:
      the original configured prefix for exported links in the "real" file
    • getRfsRules

      Returns the rfs Rules.

      Returns:
      the rfs Rules
    • getTestResource

      Returns the vfs name of the test resource.

      Returns:
      the vfs name of the test resource.
    • getVfsExportData

      Returns the export data for a requested resource, if null is returned no export is required.

      Parameters:
      cms - an initialized cms context (should be initialized with the "Guest" user only
      vfsName - the VFS name of the resource requested
      Returns:
      the export data for the request, if null is returned no export is required
    • getVfsName

      public String getVfsName(CmsObject cms, String rfsName)
      Returns the VFS name for the given RFS name, being the exact reverse of getRfsName(CmsObject, String).

      Returns null if no matching VFS resource can be found for the given RFS name.

      Parameters:
      cms - the current users OpenCms context
      rfsName - the RFS name to get the VFS name for
      Returns:
      the VFS name for the given RFS name, or null if the RFS name does not match to the VFS
      See Also:
    • getVfsNameInternal

      Returns the VFS name from a given RFS name.

      The RFS name must not contain the RFS prefix.

      Parameters:
      cms - an initialized OpenCms user context
      rfsName - the name of the RFS resource
      Returns:
      the name of the VFS resource
      Throws:
      CmsVfsResourceNotFoundException - if something goes wrong
    • getVfsPrefix

      public String getVfsPrefix()
      Returns the prefix for the internal in the VFS.

      The returned value will be a directory like prefix. The value is configured in the opencms-importexport.xml configuration file. An optimization of the configured value will be performed, where all relative path information is resolved (for example /opencms/../mycms will be resolved to /mycms. Moreover, if the configured path ends with a /, this will be cut off (for example /opencms/ becomes /opencms.

      Returns:
      the prefix for the internal in the VFS
      See Also:
    • getVfsPrefixForConfiguration

      Returns the original configured prefix for internal links in the VFS, to be used when re-writing the configuration.

      This is required only to serialize the configuration again exactly as it was configured. This method should not be used otherwise. Use getVfsPrefix() to obtain the VFS prefix to use for the internal links.

      Returns:
      the original configured prefix for internal links in the VFS
    • initialize

      public void initialize(CmsObject cms)
      Initializes the static export manager with the OpenCms system configuration.

      Parameters:
      cms - an OpenCms context object
    • isExportLink

      public boolean isExportLink(CmsObject cms, String vfsName)
      Checks if the static export is required for the given VFS resource.

      Please note that the given OpenCms user context is NOT used to read the resource. The check for export is always done with the permissions of the "Export" user. The provided user context is just used to get the current site root.

      Since the "Export" user always operates in the "Online" project, the resource is also read from the "Online" project, not from the current project of the given OpenCms context.

      Parameters:
      cms - the current users OpenCms context
      vfsName - the VFS resource name to check
      Returns:
      true if static export is required for the given VFS resource
    • isFullStaticExport

      public boolean isFullStaticExport()
      Returns true if the export process is a full static export.

      Returns:
      true if the export process is a full static export
    • isSecureLink

      public boolean isSecureLink(CmsObject cms, String vfsName)
      Returns true if the given VFS resource should be transported through a secure channel.

      The secure mode is only checked in the "Online" project. If the given OpenCms context is currently not in the "Online" project, false is returned.

      The given resource is read from the site root of the provided OpenCms context.

      Parameters:
      cms - the current users OpenCms context
      vfsName - the VFS resource name to check
      Returns:
      true if the given VFS resource should be transported through a secure channel
      See Also:
    • isSecureLink

      public boolean isSecureLink(CmsObject cms, String vfsName, boolean fromSecure)
      Returns true if the given VFS resource should be transported through a secure channel.

      The secure mode is only checked in the "Online" project. If the given OpenCms context is currently not in the "Online" project, false is returned.

      The given resource is read from the site root of the provided OpenCms context.

      Parameters:
      cms - the current users OpenCms context
      vfsName - the VFS resource name to check
      fromSecure - true if the link source is delivered secure
      Returns:
      true if the given VFS resource should be transported through a secure channel
      See Also:
    • isSecureLink

      public boolean isSecureLink(CmsObject cms, String vfsName, String siteRoot)
      Returns true if the given VFS resource that is located under the given site root should be transported through a secure channel.

      Parameters:
      cms - the current users OpenCms context
      vfsName - the VFS resource name to check
      siteRoot - the site root where the the VFS resource should be read
      Returns:
      true if the given VFS resource should be transported through a secure channel
      See Also:
    • isSecureLink

      public boolean isSecureLink(CmsObject cms, String vfsName, String siteRoot, boolean fromSecure)
      Returns true if the given VFS resource should be transported through a secure channel.

      The secure mode is only checked in the "Online" project. If the given OpenCms context is currently not in the "Online" project, false is returned.

      The given resource is read from the site root of the provided OpenCms context.

      Parameters:
      cms - the current users OpenCms context
      vfsName - the VFS resource name to check
      siteRoot - the site root where the the VFS resource should be read
      fromSecure - true if the link source is delivered secure
      Returns:
      true if the given VFS resource should be transported through a secure channel
      See Also:
    • isStaticExportEnabled

      public boolean isStaticExportEnabled()
      Returns true if the static export is enabled.

      Returns:
      true if the static export is enabled
    • isSuffixExportable

      public boolean isSuffixExportable(String resourceName)
      Returns true if the given resource name is exportable because of it's suffix.

      Parameters:
      resourceName - the name to check
      Returns:
      true if the given resource name is exportable because of it's suffix
    • isUseTempDir

      public boolean isUseTempDir()
      Checks if we have to use temporary directories during export.

      Returns:
      true if using temporary directories
    • relativeLinksInExport

      public boolean relativeLinksInExport(String vfsName)
      Returns true if the links in the static export should be relative.

      Parameters:
      vfsName - the name of the resource to export
      Returns:
      true if the links in the static export should be relative
    • setAcceptCharsetHeader

      public void setAcceptCharsetHeader(String value)
      Sets the accept-charset header value.

      Parameters:
      value - accept-language header value
    • setAcceptLanguageHeader

      public void setAcceptLanguageHeader(String value)
      Sets the accept-language header value.

      Parameters:
      value - accept-language header value
    • setDefault

      public void setDefault(String value)
      Sets the default property value.

      Parameters:
      value - must be true or false
    • setExportBackups

      public void setExportBackups(String backup)
      Sets the number of backups for the static export.

      Parameters:
      backup - number of backups
    • setExportEnabled

      public void setExportEnabled(String value)
      Sets the export enabled value.

      Parameters:
      value - must be true or false
    • setExportFolderPattern

      public void setExportFolderPattern(String folder)
      Adds a resource pattern to the list of resources which are part of the export.

      Parameters:
      folder - the folder pattern to add to the list.
    • setExportHeader

      public void setExportHeader(String exportHeader)
      Sets specific http header for the static export.

      The format of the headers must be "header:value".

      Parameters:
      exportHeader - a specific http header
    • setExportPath

      public void setExportPath(String path)
      Sets the path where the static export is written.

      Parameters:
      path - the path where the static export is written
    • setExportSuffix

      public void setExportSuffix(String suffix)
      Adds a suffix to the list of resource suffixes which will be exported by default.

      Parameters:
      suffix - the suffix to add to the list.
    • setExportUrl

      public void setExportUrl(String url)
      Sets the export url.

      Parameters:
      url - the export url
    • setExportWorkPath

      public void setExportWorkPath(String path)
      Sets the path where the static export is temporarily written.

      Parameters:
      path - the path where the static export is temporarily written
    • setHandler

      public void setHandler(String handlerClassName)
      Sets the link substitution handler class.

      Parameters:
      handlerClassName - the link substitution handler class name
    • setLinkSubstitutionHandler

      public void setLinkSubstitutionHandler(String handlerClassName)
      Sets the static export handler class.

      Parameters:
      handlerClassName - the static export handler class name
    • setPlainExportOptimization

      public void setPlainExportOptimization(String value)
      Sets the plain export optimization value.

      Parameters:
      value - must be true or false
    • setProtectedExportPath

      public void setProtectedExportPath(String exportPath)
      Sets the protected export path.

      Parameters:
      exportPath - the export path to set
    • setRelativeLinks

      public void setRelativeLinks(String value)
      Sets the relative links value.

      Parameters:
      value - must be true or false
    • setRemoteAddr

      public void setRemoteAddr(String addr)
      Sets the remote address which will be used for internal requests during the static export.

      Parameters:
      addr - the remote address to be used
    • setRfsPrefix

      public void setRfsPrefix(String rfsPrefix)
      Sets the prefix for exported links in the "real" file system.

      Parameters:
      rfsPrefix - the prefix for exported links in the "real" file system
    • setTestResource

      public void setTestResource(String testResource)
      Sets the test resource.

      Parameters:
      testResource - the vfs name of the test resource
    • setVfsPrefix

      public void setVfsPrefix(String vfsPrefix)
      Sets the prefix for internal links in the vfs.

      Parameters:
      vfsPrefix - the prefix for internal links in the vfs
    • shutDown

      public void shutDown()
      Shuts down all this static export manager.

      This is required since there may still be a thread running when the system is being shut down.

    • clearCaches

      protected void clearCaches(CmsEvent event)
      Clears the caches in the export manager.

      Parameters:
      event - the event that requested to clear the caches
    • createExportBackupFolders

      protected void createExportBackupFolders(File staticExport, String exportPath, int exportBackups, String ruleBackupExtension)
      Creates the backup folders for the given export folder and deletes the oldest if the maximum number is reached.

      Parameters:
      staticExport - folder for which a new backup folder has to be created
      exportPath - export path to create backup path out of it
      exportBackups - number of maximum
      ruleBackupExtension - extension for rule based backups
    • createExportFolder

      protected void createExportFolder(String exportPath, String rfsName) throws CmsException
      Creates the parent folder for a exported resource in the RFS.

      Parameters:
      exportPath - the path to export the file
      rfsName - the rfs name of the resource
      Throws:
      CmsException - if the folder could not be created
    • getCacheExportLinks

      Returns the cacheExportLinks.

      Returns:
      the cacheExportLinks
    • getCacheSecureLinks

      Returns the cacheSecureLinks.

      Returns:
      the cacheSecureLinks
    • getRfsExportData

      Returns the export data for a requested resource, if null is returned no export is required.

      Parameters:
      cms - an initialized cms context (should be initialized with the "Export" user only)
      uri - the uri, ie RFS name of the requested resource, with or without the 'export' prefix
      Returns:
      the export data for the request, if null is returned no export is required
    • getRfsNameWithExportName

      protected String getRfsNameWithExportName(CmsObject cms, String vfsName)
      Returns the rfs name for a given vfs name with consideration of the export name.

      Parameters:
      cms - the cms obejct
      vfsName - the the name of the vfs resource
      Returns:
      the rfs name for a given vfs name with consideration of the export name
    • getRfsPrefixForRfsName

      protected String getRfsPrefixForRfsName(String rfsName)
      Returns the longest rfs prefix matching a given already translated rfs name.

      Parameters:
      rfsName - the rfs name
      Returns:
      its rfs prefix
      See Also:
    • insertContextStrings

      Substitutes the ${CONTEXT_NAME} and ${SERVLET_NAME} in a path with the real values.

      Parameters:
      path - the path to substitute
      Returns:
      path with real context values
    • isValidRfsName

      protected boolean isValidRfsName(String rfsName)
      Returns true if the rfs Name match against any of the defined export urls.

      Parameters:
      rfsName - the rfs Name to validate
      Returns:
      true if the rfs Name match against any of the defined export urls
    • isValidURL

      protected boolean isValidURL(String inputString)
      Checks if a String is a valid URL.

      Parameters:
      inputString - The String to check can be null
      Returns:
      true if the String is not null and a valid URL
    • normalizeExportPath

      protected String normalizeExportPath(String exportPath)
      Returns a normalized export path.

      Replacing macros, normalizing the path and taking care of relative paths.

      Parameters:
      exportPath - the export path to normalize
      Returns:
      the normalized export path
    • normalizeRfsPrefix

      protected String normalizeRfsPrefix(String rfsPrefix)
      Returns a normalized rfs prefix.

      Replacing macros and normalizing the path.

      Parameters:
      rfsPrefix - the prefix to normalize
      Returns:
      the normalized rfs prefix
    • readResource

      Reads the resource with the given URI.

      Parameters:
      cms - the current CMS context
      uri - the URI to check
      Returns:
      the resource export data
      Throws:
      CmsException - if soemthing goes wrong
    • scrubExportFolders

      protected void scrubExportFolders(I_CmsReport report)
      Scrubs all the "export" folders.

      Parameters:
      report - an I_CmsReport instance to print output message, or null to write messages to the log file
    • writeResource

      protected void writeResource(javax.servlet.http.HttpServletRequest req, String exportPath, String rfsName, CmsResource resource, byte[] content) throws CmsException
      Writes a resource to the given export path with the given rfs name and the given content.

      Parameters:
      req - the current request
      exportPath - the path to export the resource
      rfsName - the rfs name
      resource - the resource
      content - the content
      Throws:
      CmsException - if something goes wrong