Class TemplateContext

java.lang.Object
ru.vyarus.guicey.gsp.views.template.TemplateContext

public class TemplateContext extends 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 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

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

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

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

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

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

      public void setAnnotationTemplate(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 String lookupTemplatePath(@Nullable 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:
      NullPointerException - if template path not set
      TemplateNotFoundException - if template not found
    • loadAsset

      public URL loadAsset(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(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