Class FreemarkerDispatcher

java.lang.Object
org.apache.juneau.rest.server.view.freemarker.FreemarkerDispatcher
All Implemented Interfaces:
RawTemplateDispatcher

public class FreemarkerDispatcher extends Object implements RawTemplateDispatcher
Flavor-neutral worker bean carrying the FreeMarker raw-template-dispatch logic shared by the FreemarkerMixin mixin, the FreemarkerServlet servlet, and the FreemarkerResource child flavors.

This is the flavor-neutral worker for the FreeMarker capability: it is not one of the three @Rest flavor classes — it is a plain bean that implements RawTemplateDispatcher and holds the capability state (basePath / templateSuffix / cacheTemplates / lazy default configuration) plus the raw-serve and configuration-resolution logic. Each flavor independently holds an instance of this worker and delegates to it, so no flavor is another flavor's worker.

See Also:
Since:
10.0.0
  • Field Details

  • Constructor Details

  • Method Details

    • create

      Creates a new builder.
      Returns:
      A new builder.
    • getBasePath

      public String getBasePath()
      Returns the base path under which template resources are resolved.
      Returns:
      The base path. Never null.
    • getTemplateSuffix

      Returns the configured template-name suffix.
      Returns:
      The template suffix. Never null.
    • isCacheTemplates

      public boolean isCacheTemplates()
      Returns the template-cache flag.
      Returns:
      The cache flag.
    • isExposeFields

      public boolean isExposeFields()
      Returns the field-exposure flag applied to the bridge-default object wrapper.
      Returns:
      The field-exposure flag. Ignored once getObjectWrapper() returns non-null.
    • getObjectWrapper

      public freemarker.template.ObjectWrapper getObjectWrapper()
      Returns the user-supplied ObjectWrapper override, if any.
      Returns:
      The object wrapper override, or null if the bridge builds its own default (a DefaultObjectWrapper configured per isExposeFields()).
    • applyTemplateSuffix

      Appends getTemplateSuffix() to name if not already present (idempotent).
      Parameters:
      name - The template name. Must not be null.
      Returns:
      The template name with the configured suffix appended (if applicable).
    • resolveConfiguration

      public freemarker.template.Configuration resolveConfiguration(RestRequest req)
      Resolves the active Configuration.

      Lookup order:

      1. req.getContext().getBeanStore().getBean(Configuration.class) — any user-supplied bean (Spring @Bean including Spring Boot autoconfig, microservice BasicBeanStore.put, etc.).
      2. Lazy bridge default — constructed on first call when no configuration bean is registered. Anchored on a classpath resource root derived from basePath, with IncompatibleImprovements pinned to the bridge-tested minor version.
      Parameters:
      req - The current REST request.
      Returns:
      The active FreeMarker configuration. Never null.
    • buildDefaultConfiguration

      protected freemarker.template.Configuration buildDefaultConfiguration()
      Constructs the bridge-default Configuration.

      Anchored on a classpath resource root derived from basePath (leading + trailing slashes trimmed; a "/" base yields a root-of-classpath resolver). The default pins IncompatibleImprovements to Configuration.VERSION_2_3_34 so behavior is stable across consumer upgrades of org.freemarker:freemarker; sets DefaultEncoding to UTF-8 and OutputFormat to HTMLOutputFormat.INSTANCE so HTML escaping is the natural target; applies TemplateUpdateDelayMilliseconds per the configured cacheTemplates flag; and sets an ObjectWrapper — either the user-supplied objectWrapper override, or (by default) a DefaultObjectWrapper built with exposeFields set to isExposeFields(). Field exposure defaults to true (unlike FreeMarker's own version-default wrapper, which is getter-only): Juneau's own marshalling is comfortable with public-field beans elsewhere, so a view-model DTO written with public fields and no getters renders its field values here too, instead of silently resolving to null/missing.

      Template exceptions are not logged by FreeMarker (LogTemplateExceptions is false): every caller rethrows them as an InternalServerError carrying the cause, so logging them as well would only duplicate the stack trace.

      Subclasses may override to plug in custom loaders / encodings / output formats without registering a separate @Bean Configuration.

      Returns:
      A new Configuration instance.
    • render

      public void render(String path, RestRequest req, RestResponse res) throws IOException, BasicHttpException
      Renders a raw template through the FreeMarker engine onto the response.
      Specified by:
      render in interface RawTemplateDispatcher
      Parameters:
      path - The trailing path segment after the mount prefix (the template name relative to the configured base path; configured suffix appended if missing).
      req - The current REST request.
      res - The current REST response.
      Throws:
      IOException - If the underlying response writer fails.
      BasicHttpException - On boundary violation (403), missing engine (500), or render failure (500).