Class TemplateContext
Template context object is thread-bound and available during template rendering request processing.
- Since:
- 25.10.2018
-
Constructor Summary
ConstructorsConstructorDescriptionTemplateContext(String appName, String rootUrl, String restSubContext, String restPrefix, io.dropwizard.views.common.ViewRenderer directTemplateRenderer, AssetLookup assets, ErrorRedirect errorRedirect, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) -
Method Summary
Modifier and TypeMethodDescriptionio.dropwizard.views.common.ViewRendererView renderer used for direct templates render (which can't be done with default dropwizard view mechanism due to the need of correct exception handling).static TemplateContextjakarta.servlet.http.HttpServletRequestMethod may be used to access original request object (in edge cases).jakarta.servlet.http.HttpServletResponseRaw response is required for redirection logic to avoid response processing loops due to hk wrappers (if hk injection were used for response object injection it would always be a proxy).Context rest mapping path.Different rest prefix may be mapped to sub context (e.g.getUrl()Each template render is redirected to rest resource so it's impossible to obtain original uri from request object inside the resource.booleanTrue means that one of registered view renderers recognize path as template file.Load asset from one of registered locations.lookupTemplatePath(String template) Lookup relative template path either relative to resource class (if annotated withTemplateor in one of pre-configured classpath locations.booleanPerform redirection to error page (if registered) or handle SPA route (if 404 response and SPA support enabled).voidsetAnnotationTemplate(String template) Used byTemplateAnnotationFilterto set template file declared inTemplateannotation on rest resource.voidsetManualErrorHandling(boolean manualErrors) Disables GSP error pages support.voidsetResourceClass(Class base) Set resource class to check template relative to class.
-
Constructor Details
-
TemplateContext
public TemplateContext(String appName, String rootUrl, String restSubContext, String restPrefix, io.dropwizard.views.common.ViewRenderer directTemplateRenderer, AssetLookup assets, ErrorRedirect errorRedirect, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
-
-
Method Details
-
getInstance
- Returns:
- thread bound template context instance
-
getAppName
- Returns:
- server pages application name
-
getRootUrl
- Returns:
- root url for server pages application
-
getRestSubContext
Different rest prefix may be mapped to sub context (e.g. /sub/ -> com.foo.app/). If such sub context detected (during rest redirection) then original url miss such sub context. In order to properly resolve templates (assets may also be mapped to sub context) original path is required.- Returns:
- current sub context mapping (after rest view, mapped to sub context, redirection) or empty string if no sub context
-
getRestPrefix
Context rest mapping path. Important for direct template resource to properly identify target path because direct template resource may appear on any level (due to sub mappings or mapping in other applications).- Returns:
- rest prefix used under current template call
-
isDirectTemplate
public boolean isDirectTemplate()True means that one of registered view renderers recognize path as template file. In real life, such path may be handled with special rest mapping instead, so this flag is useful only for cases when no matching rest found for path (because, without it, it would be impossible to differentiate template not found and rest path not matched cases).- Returns:
- true if current path could be direct template call
-
getDirectTemplateRenderer
public io.dropwizard.views.common.ViewRenderer getDirectTemplateRenderer()View renderer used for direct templates render (which can't be done with default dropwizard view mechanism due to the need of correct exception handling).- Returns:
- view renderer, recognized template
-
getUrl
Each template render is redirected to rest resource so it's impossible to obtain original uri from request object inside the resource.- Returns:
- original call url
-
getRequest
public jakarta.servlet.http.HttpServletRequest getRequest()Method may be used to access original request object (in edge cases).- Returns:
- original request object (before any redirection)
- See Also:
-
getResponse
public jakarta.servlet.http.HttpServletResponse getResponse()Raw response is required for redirection logic to avoid response processing loops due to hk wrappers (if hk injection were used for response object injection it would always be a proxy).Method may be used to handle response directly (in edge cases)
- Returns:
- original response object
-
setResourceClass
Set resource class to check template relative to class. Used byTemplateAnnotationFilter.- Parameters:
base- resource class
-
setAnnotationTemplate
Used byTemplateAnnotationFilterto set template file declared inTemplateannotation on rest resource.- Parameters:
template- template file path
-
setManualErrorHandling
public void setManualErrorHandling(boolean manualErrors) Disables GSP error pages support. Activated by presence ofManualErrorHandlingannotation on resource method or resource itself. Used byTemplateAnnotationFilter.May be set manually, but it is not recommended - prefer annotations usage to clearly declare "exceptions" from global errors handling.
- Parameters:
manualErrors- true to disable GSP errors handling, false to activate GSP error pages
-
lookupTemplatePath
Lookup relative template path either relative to resource class (if annotated withTemplateor in one of pre-configured classpath locations. If passed template is null it will be taken fromTemplateannotation from resource class.When provided template path is absolute - it is searched by direct location only.
- Parameters:
template- template path or null- Returns:
- absolute path to template
- Throws:
NullPointerException- if template path not setTemplateNotFoundException- if template not found
-
loadAsset
Load asset from one of registered locations.Method assumes to load absolute classpath location (through all custom class loaders, if registered). But, if direct lookup fails, it will perform relative resolution (search in all registered locations).
If custom class loaders used for assets declarations then template engines must be customized to resolve templates through this method (otherwise they would not be able to find it in custom class loader). For freemarker integration already provided and could be activated with
ServerPagesBundle.ViewsBuilder.enableFreemarkerCustomClassLoadersSupport().- Parameters:
path- absolute or relative path- Returns:
- resource url or null if not found
-
redirectError
Perform redirection to error page (if registered) or handle SPA route (if 404 response and SPA support enabled).When only resulted status code is known use
WebApplicationException(code)as argument for redirection.It is safe to call redirection multiple times: only first call will be actually handled (assuming next errors appear during error page rendering and can't be handled).
Method is not intended to be used directly, but could be in specific (maybe complex) edge cases.
- Parameters:
ex- exception instance- Returns:
- true if redirect performed, false if no redirect performed
-