Class TrafficParrotHelper<T>

java.lang.Object
com.trafficparrot.sdk.handlebars.TrafficParrotHelper<T>
Type Parameters:
T - the type of the helper's first parameter; Object unless every template passes one type
All Implemented Interfaces:
com.github.jknack.handlebars.Helper<T>

public abstract class TrafficParrotHelper<T> extends Object implements com.github.jknack.handlebars.Helper<T>
The base class of a custom Handlebars helper, which a response template calls by name: {{shout request.query.name}}.

 public class ShoutHelper extends TrafficParrotHelper<Object> {
     @Override
     protected Object doApply(Object parameter, Options options) {
         return String.valueOf(parameter).toUpperCase();
     }
 }
 
Put the jar in lib/external, add the class name to the helpers property of each protocol that is to use it (trafficparrot.http.handlebars.helpers, trafficparrot.jms.handlebars.helpers, trafficparrot.ibmmq.handlebars.helpers, trafficparrot.file.handlebars.helpers) and restart Traffic Parrot. One instance serves every request, on several threads at once, so keep nothing about a request in a field.

An exception thrown by doApply(T, com.github.jknack.handlebars.Options) never reaches the template engine as it is: apply(T, com.github.jknack.handlebars.Options) logs it, then either puts an error message in the response where the helper's output would be or, with trafficparrot.virtualservice.handlebars.throwErrors=true, fails the request with a TPHBException.

  • Field Details

    • logger

      protected final org.slf4j.Logger logger
      A logger named after the helper's own class.
  • Constructor Details

    • TrafficParrotHelper

      protected TrafficParrotHelper()
      Creates the helper with its logger.
  • Method Details

    • setThrowErrors

      public static void setThrowErrors(boolean throwErrors)
      Sets whether a helper error fails the request. Traffic Parrot sets it from trafficparrot.virtualservice.handlebars.throwErrors; a helper's unit test may set it too, and should put it back afterwards, because the setting is shared by every helper in the JVM.
      Parameters:
      throwErrors - true for apply(T, com.github.jknack.handlebars.Options) to throw a TPHBException, false for it to return an error message
    • throwErrorsEnabled

      protected static boolean throwErrorsEnabled()
      Whether a helper error fails the request (trafficparrot.virtualservice.handlebars.throwErrors). Only an exception escaping doApply(T, com.github.jknack.handlebars.Options) reaches that check, so a helper that reports a failure without throwing reads this to decide whether to throw instead.
      Returns:
      true if a helper error fails the request, false if it shows as an error message in the response
    • apply

      public Object apply(T parameter, com.github.jknack.handlebars.Options options) throws IOException
      Called by the template engine. Runs doApply(T, com.github.jknack.handlebars.Options) and turns any exception it throws into a logged error and either an error message in the response or, when helper errors fail the request, a TPHBException. Override doApply(T, com.github.jknack.handlebars.Options), not this.
      Specified by:
      apply in interface com.github.jknack.handlebars.Helper<T>
      Parameters:
      parameter - the helper's first parameter in the template, or the template's current context when the helper is given none
      options - the helper's other parameters, its hash arguments and, for a block helper, its body
      Returns:
      what doApply(T, com.github.jknack.handlebars.Options) returned, or the error message
      Throws:
      TPHBException - if doApply(T, com.github.jknack.handlebars.Options) threw and helper errors fail the request
      IOException - never: an IOException from doApply(T, com.github.jknack.handlebars.Options) is reported like any other exception
    • doApply

      protected abstract Object doApply(T parameter, com.github.jknack.handlebars.Options options) throws IOException
      What the helper does: works out the value the template shows in its place.
      Parameters:
      parameter - the helper's first parameter in the template, or the template's current context when the helper is given none
      options - the helper's other parameters (options.param(0) is the second one in the template), its hash arguments (options.hash("name")) and, for a block helper, its body (options.fn())
      Returns:
      the value to show in the helper's place
      Throws:
      IOException - if the helper cannot read or render what it needs; apply(T, com.github.jknack.handlebars.Options) reports it like any other exception
    • isBlockHelper

      protected boolean isBlockHelper()
      Whether templates use this helper as a block, {{#name}}...{{/name}}. It decides only how errorMessage(java.lang.String) writes an error.
      Returns:
      false, unless overridden
    • getName

      public String getName()
      The name templates call this helper by. By default the class's simple name without a trailing Helper, its first letter in lower case: ShoutHelper and Shout are both {{shout}}. Override it to choose another. A helper with the name of a built-in one replaces it.
      Returns:
      the helper's name in a template
    • errorMessage

      protected String errorMessage(String message)
      The text that shows in the response in place of this helper's output when it fails, for a helper that reports a problem without throwing: inlineHelperMessage(java.lang.String, java.lang.String), or blockHelperErrorMessage(java.lang.String, java.lang.String) for a block helper.
      Parameters:
      message - what went wrong
      Returns:
      the error text, naming this helper
    • errorMessageMap

      protected Map<String,Object> errorMessageMap(com.github.jknack.handlebars.Context context, String message)
      An error result for a helper whose result is a map the template then reads keys from. Reading any key gives the template context's value for that key if it has one and the error text otherwise, and the map itself prints as the error text.
      Parameters:
      context - the template context, options.context
      message - what went wrong
      Returns:
      a map that answers every key, with no entries of its own
    • blockHelperErrorMessage

      public static String blockHelperErrorMessage(String name, String message)
      The error text of a block helper: {{#name ERROR: message }}.
      Parameters:
      name - the helper's name in a template
      message - what went wrong
      Returns:
      the error text
    • inlineHelperMessage

      public static String inlineHelperMessage(String name, String message)
      The error text of a helper that is not a block: {{ name ERROR: message }}.
      Parameters:
      name - the helper's name in a template
      message - what went wrong
      Returns:
      the error text