Class TemplateContext


  • public class TemplateContext
    extends java.lang.Object
    Contains context information for rendered template. The most useful information is original request path: each template rendering request is redirected into resource (rest) and so it's impossible to know original path from the request object (inside rest resource).

    Template context object is thread-bound and available during template rendering request processing.

    Since:
    25.10.2018
    • Constructor Summary

      Constructors 
      Constructor Description
      TemplateContext​(java.lang.String appName, java.lang.String rootUrl, java.lang.String restSubContext, java.lang.String restPrefix, io.dropwizard.views.common.ViewRenderer directTemplateRenderer, AssetLookup assets, ErrorRedirect errorRedirect, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getAppName()  
      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).
      static TemplateContext getInstance()  
      javax.servlet.http.HttpServletRequest getRequest()
      Method may be used to access original request object (in edge cases).
      javax.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).
      java.lang.String getRestPrefix()
      Context rest mapping path.
      java.lang.String getRestSubContext()
      Different rest prefix may be mapped to sub context (e.g.
      java.lang.String getRootUrl()  
      java.lang.String getUrl()
      Each template render is redirected to rest resource so it's impossible to obtain original uri from request object inside the resource.
      boolean isDirectTemplate()
      True means that one of registered view renderers recognize path as template file.
      java.net.URL loadAsset​(java.lang.String path)
      Load asset from one of registered locations.
      java.lang.String lookupTemplatePath​(java.lang.String template)
      Lookup relative template path either relative to resource class (if annotated with Template or in one of pre-configured classpath locations.
      boolean redirectError​(java.lang.Throwable ex)
      Perform redirection to error page (if registered) or handle SPA route (if 404 response and SPA support enabled).
      void setAnnotationTemplate​(java.lang.String template)
      Used by TemplateAnnotationFilter to set template file declared in Template annotation on rest resource.
      void setManualErrorHandling​(boolean manualErrors)
      Disables GSP error pages support.
      void setResourceClass​(java.lang.Class base)
      Set resource class to check template relative to class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TemplateContext

        public TemplateContext​(java.lang.String appName,
                               java.lang.String rootUrl,
                               java.lang.String restSubContext,
                               java.lang.String restPrefix,
                               io.dropwizard.views.common.ViewRenderer directTemplateRenderer,
                               AssetLookup assets,
                               ErrorRedirect errorRedirect,
                               javax.servlet.http.HttpServletRequest request,
                               javax.servlet.http.HttpServletResponse response)
    • Method Detail

      • getInstance

        public static TemplateContext getInstance()
        Returns:
        thread bound template context instance
      • getAppName

        public java.lang.String getAppName()
        Returns:
        server pages application name
      • getRootUrl

        public java.lang.String getRootUrl()
        Returns:
        root url for server pages application
      • getRestSubContext

        public java.lang.String 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

        public java.lang.String 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

        public java.lang.String 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 javax.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:
        for original request URI, for root mapping url
      • getResponse

        public javax.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

        public void setResourceClass​(java.lang.Class base)
        Set resource class to check template relative to class. Used by TemplateAnnotationFilter.
        Parameters:
        base - resource class
      • setAnnotationTemplate

        public void setAnnotationTemplate​(java.lang.String template)
        Used by TemplateAnnotationFilter to set template file declared in Template annotation on rest resource.
        Parameters:
        template - template file path
      • setManualErrorHandling

        public void setManualErrorHandling​(boolean manualErrors)
        Disables GSP error pages support. Activated by presence of ManualErrorHandling annotation on resource method or resource itself. Used by TemplateAnnotationFilter.

        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

        public java.lang.String lookupTemplatePath​(@Nullable
                                                   java.lang.String template)
        Lookup relative template path either relative to resource class (if annotated with Template or in one of pre-configured classpath locations. If passed template is null it will be taken from Template annotation 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:
        java.lang.NullPointerException - if template path not set
        TemplateNotFoundException - if template not found
      • loadAsset

        public java.net.URL loadAsset​(java.lang.String path)
        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

        public boolean redirectError​(java.lang.Throwable ex)
        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