Scripting

Throughout Membrane, certain plugins like:

support expressions written in Javascript, Groovy or SpEL.

Examples

...

Context Attributes

When using Membranes scripting capabilities, a set of pre-defined attributes and APIs are available to provide access to useful information about the current context:

Outcomes

Outcomes are enums that depict the current state of the request/response flow.
Next to the enum class itself Outcome, the individual enum states are available as well.

Outcome Description
RETURN Changes the direction of flow. The request handling mechanisms of the active plugins get activated.
CONTINUE Continues the flow.
ABORT Aborts the current flow, the direction is changed and the abort handling mechanisms of the active plugins get activated.

Exchange

The exc keyword grants access to the current Exchange object, housing request and response information.

Message

The message keyword is a shortcut to exc.getRequest()/exc.getResponse().
Depending on direction of flow, message can stand for a request or response object.

Params

The params keyword can be used to access request query parameters.

JSON

If the body of a message is of type application/json, json is a parsed JSON object representation of the body's JSON document.

Flow

The flow keyword houses the Flow enum. Values are REQUEST and RESPONSE.
These stand for the direction of flow.

Properties

The properties keyword is a shortcut to exc.getProperties().

Spring

The spring keyword grants access to Membrane's Spring ApplicationContext.

SpEL Reference

Context Variables

The following variables are available in SpEL expressions:

VariableTypeDescription
exchangeExchangeCurrent exchange.
messageMessageCurrent message depending on flow (request/response).
bodyBodyMessage body
headers / headerMap<String, Object>Header map
cookies / cookieMap<String, Object>Cookie map
properties / propertyMap<String, Object>Exchange properties.
params / paramMap<String, Object>Query parameters.
pathParamMap<String, Object>Path parameters from URI templates.
pathStringRequest path
methodStringHTTP method
statusCodeintResponse status code (if response exists).
requestRequestRequest wrapper.
responseResponseResponse wrapper.
jsonMap<String,Object>Parsed JSON body as map.
scopesStringList of authentication scopes (may be null).
flowFlowREQUEST or RESPONSE.

Aliases: header=headers, cookie=cookies, property=properties, param=params.

Built-in Functions

SignatureDescription
jsonPath(String jsonPath)Object Runs JSONPath on decoded body; returns null on errors.

Example: jsonPath('$.user.id')
weight(double percent)boolean Random boolean with given true probability (0–100%).

Example: weight(10)
isLoggedIn(String beanName)boolean Verified session present for given interceptor bean?

Example: isLoggedIn('loginInterceptor')
getDefaultSessionLifetime(String beanName)long Default session lifetime in seconds; -1 if unavailable.

Example: getDefaultSessionLifetime('loginInterceptor')
isBearerAuthorization()boolean Authorization header starts with Bearer?

Example: isBearerAuthorization()
scopes()List<String> All scopes from available security schemes.

Example: scopes()
scopes(String scheme)List<String> Scopes for a scheme (http, apiKey, oauth2).

Example: scopes('oauth2')
hasScope(String scope)boolean Scope present?

Example: hasScope('admin')
hasScope()boolean At least one scope present.

Example: hasScope()
hasScope(List<String> scopes)boolean Contains all given scopes.

Example: hasScope({'read','write'})
isJSON()boolean Message body is JSON.

Example: isJSON()
isXML()boolean Message body is XML.

Example: isXML()
base64Encode(String s)String Base64 encodes the input string.

Example: base64Encode('string')