REST Server SVL Variables
For the Var API itself, see SVL Variables.
When to use it
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} | |
| EnvFileVar | $EF{key[,default]} | yes | yes | $EF{MY_KEY,default} | |
| DotenvVar | $DE{key[,default]} | yes | yes | $DE{MY_KEY,default} | |
| ArgsVar | $A{key[,default]} | yes | yes | $A{foo,null} | |
| ManifestFileVar | $MF{key[,default]} | yes | yes | $MF{Main-Class} | |
| 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} |
Transformation/conditional logic that used to be a dedicated Var class ($UC{}, $IF{},
$SW{}, $PM{}, $PR{}, $PE{}, $NE{}, $CO{}, $LN{}, $ST{}, $LC{}) is now expressed
via the #{name(args)} function-call syntax introduced in 10.0.0. See
Simple Variable Language — #{...} function-call syntax
and the 10.0.0 release notes for the migration table.
Custom variables can be defined by supplying a named varResolver bean via
@Bean.
import org.apache.juneau.commons.inject.Bean;
// Defined a variable that simply wraps 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 BasicRestResource {
@Bean(name="varResolver")
public static VarResolver varResolver(VarResolver.Builder b) {
return b.vars(BracketVar.class).build();
}
}
For framework-managed types the resource has a single per-context instance; the bean name is an alias for it, and the bean is not inherited by @Rest(children).
The methods involved with variables are: