SVL Variables
In the previous examples, there were several cases where embedded variables were contained within annotation values:
@Rest(
title="$L{my.label}"
)
Variables take the form $X{contents}
where X
can consist of zero or more ASCII characters and contents can be
virtually anything.
This is called Simple Variable Language, or SVL, and is defined here: Simple Variable Language.
Features
- Variables can be nested arbitrarily deep (e.g.
$X{$Y{foo}}
).
- Variables can contain arguments (e.g.
$L{my.label,arg1,arg2}
).
- Variables are recursively resolved. i.e., if a variable results to a value with another variable in it, that variable will also be resolved (restricted for security reasons on variables that involve user input).
There are two distinct groups of variables:
- Initialization-time variables: These are variables that can be used in many of the annotations in @Rest. The RestContext.getVarResolver() method returns initialization-time variables only.
- Request-time variables: These are variables that are available during HTTP-requests and can be used on annotation such as @HtmlDoc. RestRequest.getVarResolverSession() method returns initialization and request-time variables.
The following is the default list of supported variables:
Default REST SVL Variables:
Module | Class | Pattern | Initialization time | Request time | Examples |
---|---|---|---|---|---|
juneau-svl | EnvVariablesVar | $E{key[,default]} | yes | yes | $E{PATH} |
SystemPropertiesVar | $S{key[,default]} | yes | yes | $S{java.home} | |
ArgsVar | $A{key[,default]} | yes | yes | $A{foo,null} | |
ManifestFileVar | $MF{key[,default]} | yes | yes | $MF{Main-Class} | |
IfVar | $IF{arg,then[,else]} | yes | yes | $IF{$S{my.boolean.property},foo,bar} | |
SwitchVar | $SW{arg,p1:then1[,p2:then2...]} | yes | yes | $SW{$S{os.name},*win*:Windows,*:Something else} | |
CoalesceVar | $CO{arg1[,arg2...]} | yes | yes | $CO{$S{my.property},$E{my.property},n/a} | |
PatternMatchVar | $PM{arg,pattern} | yes | yes | $PM{$S{os.name},*win*} | |
NotEmptyVar | $NE{arg} | yes | yes | $NE{$S{foo}} | |
UpperCaseVar | $UC{arg} | yes | yes | $UC{$S{foo}} | |
LowerCaseVar | $LC{arg} | yes | yes | $LC{$S{foo}} | |
juneau-config | ConfigVar | $C{key[,default]} | yes | yes | $C{REST/staticFiles} |
juneau-rest-server | FileVar | $F{path[,default]} | yes | yes | $F{resources/MyAsideMessage.html, Oops not found!} |
ServletInitParamVar | $I{name[,default]} | yes | yes | $I{my.param} | |
LocalizationVar | $L{key[,args...]} | no | yes | $L{MyMessage,foo,bar} | |
RequestAttributeVar | $RA{key1[,key2...]} | no | yes | $RA{attrName} | |
RequestFormDataVar | $RF{key1[,key2...]} | no | yes | $RF{paramName} | |
RequestHeaderVar | $RH{key1[,key2...]} | no | yes | $RH{Header-Name} | |
RequestPathVar | $RP{key1[,key2...]} | no | yes | $RP{pathVar} | |
RequestQueryVar | $RQ{key1[,key2...]} | no | yes | $RQ{paramName} | |
RequestSwaggerVar | $RS{key} | no | yes | $RS{title} | |
RequestVar | $R{key1[,key2...]} | no | yes | $R{contextPath} | |
SerializedRequestAttrVar | $SA{contentType,key[,default]} | no | yes | $SA{application/json,$RA{foo}} | |
UrlVar | $U{uri} | no | yes | $U{servlet:/foo} | |
UrlEncodeVar | $UE{uriPart} | yes | yes | $U{servlet:/foo?bar=$UE{$RA{bar}}} | |
Widget | $W{name} | no | yes | $W{MenuItemWidget} |
Custom variables can be defined on resources via the following API:
Example
// Defined a variable that simply wrapps all strings inside [] brackets.
// e.g. "$BRACKET{foobar}" -> "[foobar]"
public class BracketVar extends SimpleVar {
public BracketVar() {
super("BRACKET");
}
@Override /* Var */
public String resolve(VarResolverSession session, String arg) {
return '[' + arg + ']';
}
}
// Register it with our resource.
@Rest(...)
public class MyResource extends BasicRestObject {
@RestInit
public void init(RestContext.Builder builder) {
builder.vars(BracketVar.class);
}
}
The methods involved with variables are: