Class MustacheDispatcher
java.lang.Object
org.apache.juneau.rest.server.view.mustache.MustacheDispatcher
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault base path applied when noMustacheDispatcher.Builder.basePath(String)call has been made.static final booleanDefault cache-templates setting —true(cache forever; production-safe).static final StringDefault template suffix — empty (literal template names, no implicit suffix). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBuilder constructor. -
Method Summary
Modifier and TypeMethodDescriptionapplyTemplateSuffix(String name) AppendsgetTemplateSuffix()tonameif not already present (idempotent).protected com.github.mustachejava.MustacheFactoryConstructs the bridge-defaultMustacheFactory.static MustacheDispatcher.Buildercreate()Creates a new builder.Returns the base path under which template resources are resolved.Returns the configured template-name suffix.booleanReturns whether the bridge-defaultMustacheFactorycaches compiled templates.voidrender(String path, RestRequest req, RestResponse res) Renders a raw template through the Mustache engine onto the response.com.github.mustachejava.MustacheFactoryResolves the activeMustacheFactory.
-
Field Details
-
DEFAULT_BASE_PATH
Default base path applied when noMustacheDispatcher.Builder.basePath(String)call has been made.- See Also:
-
DEFAULT_TEMPLATE_SUFFIX
Default template suffix — empty (literal template names, no implicit suffix).- See Also:
-
DEFAULT_CACHE_TEMPLATES
Default cache-templates setting —true(cache forever; production-safe).- See Also:
-
-
Constructor Details
-
MustacheDispatcher
Builder constructor.- Parameters:
builder- The builder. Must not benull.
-
-
Method Details
-
create
Creates a new builder.- Returns:
- A new builder.
-
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
Returns whether the bridge-defaultMustacheFactorycaches compiled templates.- Returns:
- The cache flag. Defaults to
DEFAULT_CACHE_TEMPLATES.
-
applyTemplateSuffix
AppendsgetTemplateSuffix()tonameif not already present (idempotent).- Parameters:
name- The template name. Must not benull.- Returns:
- The template name with the configured suffix appended (if applicable).
-
resolveMustacheFactory
Resolves the activeMustacheFactory.Lookup order:
req.getContext().getBeanStore().getBean(MustacheFactory.class)— any user-supplied bean (Spring@Bean, microserviceBasicBeanStore.put, etc.). Never cached or rebuilt by this bridge —isCacheTemplates()does not apply to user-supplied factories.- Bridge default — a
DefaultMustacheFactoryanchored on a resource root derived frombasePath(leading + trailing slashes trimmed). WhenisCacheTemplates()istrue(the default), a single instance is lazily built once and reused (mustache.java's ownDefaultMustacheFactorycompiles-and-caches templates internally, so reuse means "compile once"). Whenfalse, 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
Constructs the bridge-defaultMustacheFactory.DefaultMustacheFactoryanchored on a classpath resource root derived frombasePath(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
MustacheFactoryinstance.
-
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:
renderin interfaceRawTemplateDispatcher- Parameters:
path- The trailing path segment after the mount prefix (the template name relative to the configuredbase 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).
-