Class MustacheDispatcher

java.lang.Object
org.apache.juneau.rest.server.view.mustache.MustacheDispatcher
All Implemented Interfaces:
RawTemplateDispatcher

public class MustacheDispatcher extends Object implements RawTemplateDispatcher
Flavor-neutral worker bean carrying the Mustache raw-template-dispatch logic shared by the MustacheMixin mixin, the MustacheServlet servlet, and the MustacheResource child flavors.

This is the flavor-neutral worker for the Mustache 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 / lazy default factory) plus the raw-serve and factory-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 whether the bridge-default MustacheFactory caches compiled templates.
      Returns:
      The cache flag. Defaults to DEFAULT_CACHE_TEMPLATES.
    • 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).
    • resolveMustacheFactory

      public com.github.mustachejava.MustacheFactory resolveMustacheFactory(RestRequest req)
      Resolves the active MustacheFactory.

      Lookup order:

      1. req.getContext().getBeanStore().getBean(MustacheFactory.class) — any user-supplied bean (Spring @Bean, microservice BasicBeanStore.put, etc.). Never cached or rebuilt by this bridge — isCacheTemplates() does not apply to user-supplied factories.
      2. Bridge default — a DefaultMustacheFactory anchored on a resource root derived from basePath (leading + trailing slashes trimmed). When isCacheTemplates() is true (the default), a single instance is lazily built once and reused (mustache.java's own DefaultMustacheFactory compiles-and-caches templates internally, so reuse means "compile once"). When false, a fresh factory with an empty compile-cache is built on every call, so every render recompiles the template from its current classpath contents (hot-reload).
      Parameters:
      req - The current REST request.
      Returns:
      The active Mustache factory. Never null.
    • buildDefaultFactory

      protected com.github.mustachejava.MustacheFactory buildDefaultFactory()
      Constructs the bridge-default MustacheFactory.

      DefaultMustacheFactory anchored on a classpath resource root derived from basePath (leading + trailing slashes trimmed). A "/" base yields a no-prefix factory. Subclasses may override to plug in custom resolvers / object handlers without registering a separate @Bean MustacheFactory.

      Returns:
      A new MustacheFactory instance.
    • render

      public void render(String path, RestRequest req, RestResponse res) throws IOException, BasicHttpException
      Renders a raw template through the Mustache 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).