Class AbstractConfigurableTemplateResolver

Object
org.thymeleaf.templateresolver.AbstractTemplateResolver
org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver
All Implemented Interfaces:
ITemplateResolver
Direct Known Subclasses:
ClassLoaderTemplateResolver, FileTemplateResolver, UrlTemplateResolver, WebApplicationTemplateResolver

public abstract class AbstractConfigurableTemplateResolver extends AbstractTemplateResolver

Abstract implementation of ITemplateResolver extending AbstractTemplateResolver and providing a large set of methods for configuring resource name (from template name), template mode, cache validity and character encoding.

Unless overriden, this class will always apply the following validity to template resolutions:

Since:
3.0.0
Author:
Daniel Fernández
  • Field Details

    • DEFAULT_TEMPLATE_MODE

      public static final TemplateMode DEFAULT_TEMPLATE_MODE

      Default template mode: TemplateMode.HTML

    • DEFAULT_CACHEABLE

      public static final boolean DEFAULT_CACHEABLE

      Default value for the cacheable flag: true.

      See Also:
    • DEFAULT_CACHE_TTL_MS

      public static final Long DEFAULT_CACHE_TTL_MS

      Default value for the cache TTL: null. This means the parsed template will live in cache until removed by LRU (because of being the oldest entry).

  • Constructor Details

    • AbstractConfigurableTemplateResolver

      public AbstractConfigurableTemplateResolver()
  • Method Details

    • getPrefix

      public final String getPrefix()

      Returns the (optional) prefix to be added to all template names in order to convert template names into resource names.

      Returns:
      the prefix.
    • setPrefix

      public final void setPrefix(String prefix)

      Sets a new (optional) prefix to be added to all template names in order to convert template names into resource names.

      Parameters:
      prefix - the prefix to be set.
    • getSuffix

      public final String getSuffix()

      Returns the (optional) suffix to be added to all template names in order to convert template names into resource names.

      Note that this suffix may not be applied to the template name if the template name already ends in a known file name suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt. If this behaviour needs to be overridden so that suffix is always applied, the setForceSuffix(boolean) will need to be set.

      Returns:
      the suffix.
    • setSuffix

      public final void setSuffix(String suffix)

      Sets a new (optional) suffix to be added to all template names in order to convert template names into resource names.

      Note that this suffix may not be applied to the template name if the template name already ends in a known file name suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt. If this behaviour needs to be overridden so that suffix is always applied, the setForceSuffix(boolean) will need to be set.

      Parameters:
      suffix - the suffix to be set.
    • getForceSuffix

      public final boolean getForceSuffix()

      Returns whether the application of the suffix should be forced on the template name.

      When forced, suffix will be appended to the template name even if the template name ends in a known suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt.

      Default value is false

      .
      Returns:
      whether the suffix will be forced or not.
      Since:
      3.0.6
    • setForceSuffix

      public final void setForceSuffix(boolean forceSuffix)

      Sets whether the application of the suffix should be forced on the template name.

      When forced, suffix will be appended to the template name even if the template name ends in a known suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt.

      Default value is false

      .
      Parameters:
      forceSuffix - whether the suffix should be forced or not.
      Since:
      3.0.6
    • getCharacterEncoding

      public final String getCharacterEncoding()

      Returns the character encoding to be used for reading template resources resolved by this template resolver.

      Returns:
      the character encoding.
    • setCharacterEncoding

      public final void setCharacterEncoding(String characterEncoding)

      Sets a new character encoding for reading template resources.

      Parameters:
      characterEncoding - the character encoding to be used.
    • getTemplateMode

      public final TemplateMode getTemplateMode()

      Returns the template mode to be applied to templates resolved by this template resolver.

      If template mode patterns (see setHtmlTemplateModePatterns(Set), setXmlTemplateModePatterns(Set), etc.) are also set, they have higher priority than the template mode set here (this would act as a default).

      Note that this template mode also may not be applied if the template resource name ends in a known file name suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt. If this behaviour needs to be overridden so that template name is always applied, the setForceTemplateMode(boolean) will need to be set.

      Returns:
      the template mode to be used.
    • setTemplateMode

      public final void setTemplateMode(TemplateMode templateMode)

      Sets the template mode to be applied to templates resolved by this resolver.

      If template mode patterns (see setHtmlTemplateModePatterns(Set), setXmlTemplateModePatterns(Set), etc.) are also set, they have higher priority than the template mode set here (this would act as a default).

      Note that this template mode also may not be applied if the template resource name ends in a known file name suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt. If this behaviour needs to be overridden so that template name is always applied, the setForceTemplateMode(boolean) will need to be set.

      Parameters:
      templateMode - the template mode.
    • setTemplateMode

      public final void setTemplateMode(String templateMode)

      Sets the template mode to be applied to templates resolved by this resolver.

      Allowed templates modes are defined by the TemplateMode class.

      If template mode patterns (see setHtmlTemplateModePatterns(Set), setXmlTemplateModePatterns(Set), etc.) are also set, they have higher priority than the template mode set here (this would act as a default).

      Note that this template mode also may not be applied if the template resource name ends in a known file name suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt. If this behaviour needs to be overridden so that template name is always applied, the setForceTemplateMode(boolean) will need to be set.

      Parameters:
      templateMode - the template mode.
    • getForceTemplateMode

      public final boolean getForceTemplateMode()

      Returns whether the configured template mode should be forced instead of attempting a smart template mode resolution based on template resource name.

      When forced, the configured template mode (setTemplateMode(TemplateMode) will be applied even if the template resource name ends in a known suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt.

      Default value is false

      .
      Returns:
      whether the suffix will be forced or not.
      Since:
      3.0.6
    • setForceTemplateMode

      public final void setForceTemplateMode(boolean forceTemplateMode)

      Sets whether the configured template mode should be forced instead of attempting a smart template mode resolution based on template resource name.

      When forced, the configured template mode (setTemplateMode(TemplateMode) will be applied even if the template resource name ends in a known suffix: .html, .htm, .xhtml, .xml, .js, .json, .css, .rss, .atom, .txt.

      Default value is false

      .
      Parameters:
      forceTemplateMode - whether the configured template mode should be forced or not.
      Since:
      3.0.6
    • isCacheable

      public final boolean isCacheable()

      Returns whether templates resolved by this resolver have to be considered cacheable or not.

      If cacheable patterns (see setCacheablePatterns(Set)) are also set, they have higher priority than the value set here (this would act as a default).

      Returns:
      whether templates resolved are cacheable or not.
    • setCacheable

      public final void setCacheable(boolean cacheable)

      Sets a new value for the cacheable flag.

      If cacheable patterns (see setCacheablePatterns(Set)) are also set, they have higher priority than the value set here (this would act as a default).

      Parameters:
      cacheable - whether resolved patterns should be considered cacheable or not.
    • getCacheTTLMs

      public final Long getCacheTTLMs()

      Returns the TTL (Time To Live) in cache of templates resolved by this resolver.

      If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.

      Returns:
      the cache TTL for resolved templates.
    • setCacheTTLMs

      public final void setCacheTTLMs(Long cacheTTLMs)

      Sets a new value for the cache TTL for resolved templates.

      If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.

      Parameters:
      cacheTTLMs - the new cache TTL, or null for using natural LRU eviction.
    • getTemplateAliases

      public final Map<String,String> getTemplateAliases()

      Returns the currently configured template aliases.

      Template aliases allow the use of several (and probably shorter) names for templates.

      Aliases are applied to template names before prefix/suffix.

      Returns:
      the map of template aliases.
    • setTemplateAliases

      public final void setTemplateAliases(Map<String,String> templateAliases)

      Sets all the new template aliases to be used.

      Template aliases allow the use of several (and probably shorter) names for templates.

      Aliases are applied to template names before prefix/suffix.

      Parameters:
      templateAliases - the new template aliases.
    • addTemplateAlias

      public final void addTemplateAlias(String alias, String templateName)

      Adds a new template alias to the currently configured ones.

      Parameters:
      alias - the new alias name
      templateName - the name of the template the alias will be applied to
    • clearTemplateAliases

      public final void clearTemplateAliases()

      Removes all currently configured template aliases.

    • getXmlTemplateModePatternSpec

      public final PatternSpec getXmlTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.XML template mode to resolved templates.

      Returns:
      the pattern spec
    • getXmlTemplateModePatterns

      public final Set<String> getXmlTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.XML template mode to resolved templates.

      This is a convenience method equivalent to getXmlTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
    • setXmlTemplateModePatterns

      public final void setXmlTemplateModePatterns(Set<String> newXmlTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.XML template mode as Strings.

      This is a convenience method equivalent to getXmlTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newXmlTemplateModePatterns - the new patterns
    • getHtmlTemplateModePatternSpec

      public final PatternSpec getHtmlTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.HTML template mode to resolved templates.

      Returns:
      the pattern spec
      Since:
      3.0.0
    • getHtmlTemplateModePatterns

      public final Set<String> getHtmlTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.HTML template mode to resolved templates.

      This is a convenience method equivalent to getHtmlTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
      Since:
      3.0.0
    • setHtmlTemplateModePatterns

      public final void setHtmlTemplateModePatterns(Set<String> newHtmlTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.HTML template mode as Strings.

      This is a convenience method equivalent to getHtmlTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newHtmlTemplateModePatterns - the new patterns
      Since:
      3.0.0
    • getJavaScriptTemplateModePatternSpec

      public final PatternSpec getJavaScriptTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.JAVASCRIPT template mode to resolved templates.

      Returns:
      the pattern spec
      Since:
      3.0.0
    • getJavaScriptTemplateModePatterns

      public final Set<String> getJavaScriptTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.JAVASCRIPT template mode to resolved templates.

      This is a convenience method equivalent to getJavaScriptTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
      Since:
      3.0.0
    • setJavaScriptTemplateModePatterns

      public final void setJavaScriptTemplateModePatterns(Set<String> newJavaScriptTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.JAVASCRIPT template mode as Strings.

      This is a convenience method equivalent to getJavaScriptTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newJavaScriptTemplateModePatterns - the new patterns
      Since:
      3.0.0
    • getCSSTemplateModePatternSpec

      public final PatternSpec getCSSTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.CSS template mode to resolved templates.

      Returns:
      the pattern spec
      Since:
      3.0.0
    • getCSSTemplateModePatterns

      public final Set<String> getCSSTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.CSS template mode to resolved templates.

      This is a convenience method equivalent to getCSSTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
      Since:
      3.0.0
    • setCSSTemplateModePatterns

      public final void setCSSTemplateModePatterns(Set<String> newCSSTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.CSS template mode as Strings.

      This is a convenience method equivalent to getCSSTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newCSSTemplateModePatterns - the new patterns
      Since:
      3.0.0
    • getRawTemplateModePatternSpec

      public final PatternSpec getRawTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.RAW template mode to resolved templates.

      Returns:
      the pattern spec
      Since:
      3.0.0
    • getRawTemplateModePatterns

      public final Set<String> getRawTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.RAW template mode to resolved templates.

      This is a convenience method equivalent to getRawTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
      Since:
      3.0.0
    • setRawTemplateModePatterns

      public final void setRawTemplateModePatterns(Set<String> newRawTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.RAW template mode as Strings.

      This is a convenience method equivalent to getRawTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newRawTemplateModePatterns - the new patterns
      Since:
      3.0.0
    • getTextTemplateModePatternSpec

      public final PatternSpec getTextTemplateModePatternSpec()

      Returns the pattern spec specified for establishing the TemplateMode.TEXT template mode to resolved templates.

      Returns:
      the pattern spec
      Since:
      3.0.0
    • getTextTemplateModePatterns

      public final Set<String> getTextTemplateModePatterns()

      Returns the patterns specified for establishing the TemplateMode.TEXT template mode to resolved templates.

      This is a convenience method equivalent to getTextTemplateModePatternSpec().getPatterns()

      Returns:
      the pattern spec
      Since:
      3.0.0
    • setTextTemplateModePatterns

      public final void setTextTemplateModePatterns(Set<String> newTextTemplateModePatterns)

      Sets the new patterns to be applied for establishing the TemplateMode.TEXT template mode as Strings.

      This is a convenience method equivalent to getTextTemplateModePatternSpec().setPatterns(Set<String>)

      Parameters:
      newTextTemplateModePatterns - the new patterns
      Since:
      3.0.0
    • getCacheablePatternSpec

      public final PatternSpec getCacheablePatternSpec()

      Returns the pattern spec specified for establishing which templates have to be considered cacheable.

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      Returns:
      the pattern spec
    • getCacheablePatterns

      public final Set<String> getCacheablePatterns()

      Returns the patterns (as String) specified for establishing which templates have to be considered cacheable.

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      This is a convenience method equivalent to getCacheablePatternSpec().getPatterns()

      Returns:
      the patterns
    • setCacheablePatterns

      public final void setCacheablePatterns(Set<String> cacheablePatterns)

      Sets the new patterns to be applied for establishing which templates have to be considered cacheable

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      This is a convenience method equivalent to getCacheablePatternSpec().setPatterns(Set<String>)

      Parameters:
      cacheablePatterns - the new patterns
    • getNonCacheablePatternSpec

      public final PatternSpec getNonCacheablePatternSpec()

      Returns the pattern spec specified for establishing which templates have to be considered non cacheable.

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      Returns:
      the pattern spec
    • getNonCacheablePatterns

      public final Set<String> getNonCacheablePatterns()

      Returns the patterns (as String) specified for establishing which templates have to be considered non cacheable.

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      This is a convenience method equivalent to getNonCacheablePatternSpec().getPatterns()

      Returns:
      the patterns
    • setNonCacheablePatterns

      public final void setNonCacheablePatterns(Set<String> nonCacheablePatterns)

      Sets the new patterns to be applied for establishing which templates have to be considered non cacheable

      These patterns have higher precedence than the cacheable flag (see setCacheable(boolean)). Such flag can be considered a default value after cacheable patterns and non-cacheable patterns have been applied.

      This is a convenience method equivalent to getNonCacheablePatternSpec().setPatterns(Set<String>)

      Parameters:
      nonCacheablePatterns - the new patterns
    • computeResourceName

      protected String computeResourceName(IEngineConfiguration configuration, String ownerTemplate, String template, String prefix, String suffix, boolean forceSuffix, Map<String,String> templateAliases, Map<String,Object> templateResolutionAttributes)

      Computes the resource name that will be used for resolving, from the template name and other parameters configured at this configurable resolver.

      This method can be overridden by subclasses that need to modify the standard way in which the name of the template resource is computed by default before passing it to the real resource resolution mechanism (in method computeTemplateResource(IEngineConfiguration, String, String, String, String, Map)

      By default, the resource name will be created by first applying the template aliases, and then adding prefix and suffix to the specified template (template name).

      Parameters:
      configuration - the engine configuration in use.
      ownerTemplate - the owner template, if the resource being computed is a fragment. Might be null.
      template - the template (normally the template name, except for String templates).
      prefix - the prefix to be applied.
      suffix - the suffix to be applied.
      forceSuffix - whether the suffix should be forced or not.
      templateAliases - the template aliases map.
      templateResolutionAttributes - the template resolution attributes, if any. Might be null.
      Returns:
      the resource name that should be used for resolving
      Since:
      3.0.6
    • computeTemplateMode

      protected TemplateMode computeTemplateMode(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
      Description copied from class: AbstractTemplateResolver

      Computes the template mode that should be applied to a template, according to existing configuration.

      Specified by:
      computeTemplateMode in class AbstractTemplateResolver
      Parameters:
      configuration - the engine configuration.
      ownerTemplate - the owner template, if the resource being computed is a fragment. Might be null.
      template - the template to be resolved (usually its name).
      templateResolutionAttributes - the template resolution attributes, if any. Might be null.
      Returns:
      the template mode proposed by the template resolver for the resolved template.
    • computeValidity

      protected ICacheEntryValidity computeValidity(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
      Description copied from class: AbstractTemplateResolver

      Computes the validity to be applied to the template resolution. This includes determining whether the template can be cached or not, and also in what circumstances (for instance, for how much time) can its cache entry be considered valid.

      Specified by:
      computeValidity in class AbstractTemplateResolver
      Parameters:
      configuration - the engine configuration.
      ownerTemplate - the owner template, if the resource being computed is a fragment. Might be null.
      template - the template to be resolved (usually its name).
      templateResolutionAttributes - the template resolution attributes, if any. Might be null.
      Returns:
      the validity
    • computeTemplateResource

      protected final ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String,Object> templateResolutionAttributes)
      Description copied from class: AbstractTemplateResolver

      Computes the resolved template resource.

      Specified by:
      computeTemplateResource in class AbstractTemplateResolver
      Parameters:
      configuration - the engine configuration.
      ownerTemplate - the owner template, if the resource being computed is a fragment. Might be null.
      template - the template to be resolved (usually its name).
      templateResolutionAttributes - the template resolution attributes, if any. Might be null.
      Returns:
      the template resource, or null if this template cannot be resolved (or the resource does not exist).
    • computeTemplateResource

      protected abstract ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, String resourceName, String characterEncoding, Map<String,Object> templateResolutionAttributes)

      Compute the real resource, once the resource name has been computed using prefix, suffix, and other configured artifacts.

      Parameters:
      configuration - the engine configuration in use.
      ownerTemplate - the owner template, if the resource being computed is a fragment. Might be null.
      template - the template (normally the template name, except for String templates).
      resourceName - the resource name, complete with prefix, suffix, aliases, etc.
      characterEncoding - the character encoding to be used for reading the resource.
      templateResolutionAttributes - the template resolution attributes, if any. Might be null.
      Returns:
      the template resource