Package com.trafficparrot.sdk.handlebars
Class TrafficParrotHelper<T>
java.lang.Object
com.trafficparrot.sdk.handlebars.TrafficParrotHelper<T>
- Type Parameters:
T- the type of the helper's first parameter;Objectunless 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 Summary
FieldsModifier and TypeFieldDescriptionprotected final org.slf4j.LoggerA logger named after the helper's own class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCalled by the template engine.static StringblockHelperErrorMessage(String name, String message) The error text of a block helper:{{#name ERROR: message }}.protected abstract ObjectWhat the helper does: works out the value the template shows in its place.protected StringerrorMessage(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), orblockHelperErrorMessage(java.lang.String, java.lang.String)for ablock helper.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.getName()The name templates call this helper by.static StringinlineHelperMessage(String name, String message) The error text of a helper that is not a block:{{ name ERROR: message }}.protected booleanWhether templates use this helper as a block,{{#name}}...{{/name}}.static voidsetThrowErrors(boolean throwErrors) Sets whether a helper error fails the request.protected static booleanWhether a helper error fails the request (trafficparrot.virtualservice.handlebars.throwErrors).
-
Field Details
-
logger
protected final org.slf4j.Logger loggerA logger named after the helper's own class.
-
-
Constructor Details
-
TrafficParrotHelper
protected TrafficParrotHelper()Creates the helper with itslogger.
-
-
Method Details
-
setThrowErrors
public static void setThrowErrors(boolean throwErrors) Sets whether a helper error fails the request. Traffic Parrot sets it fromtrafficparrot.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 forapply(T, com.github.jknack.handlebars.Options)to throw aTPHBException, 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 escapingdoApply(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
Called by the template engine. RunsdoApply(T, com.github.jknack.handlebars.Options)and turns any exception it throws into a logged error and either anerror messagein the response or, when helper errors fail the request, aTPHBException. OverridedoApply(T, com.github.jknack.handlebars.Options), not this.- Specified by:
applyin interfacecom.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 noneoptions- 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- ifdoApply(T, com.github.jknack.handlebars.Options)threw and helper errors fail the requestIOException- never: anIOExceptionfromdoApply(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 noneoptions- 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 howerrorMessage(java.lang.String)writes an error.- Returns:
- false, unless overridden
-
getName
The name templates call this helper by. By default the class's simple name without a trailingHelper, its first letter in lower case:ShoutHelperandShoutare 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
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), orblockHelperErrorMessage(java.lang.String, java.lang.String)for ablock 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.contextmessage- what went wrong- Returns:
- a map that answers every key, with no entries of its own
-
blockHelperErrorMessage
The error text of a block helper:{{#name ERROR: message }}.- Parameters:
name- the helper's name in a templatemessage- what went wrong- Returns:
- the error text
-
inlineHelperMessage
The error text of a helper that is not a block:{{ name ERROR: message }}.- Parameters:
name- the helper's name in a templatemessage- what went wrong- Returns:
- the error text
-