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.

OutcomeDescription
RETURNChanges the direction of flow. The request handling mechanisms of the active plugins get activated.
CONTINUEContinues the flow.
ABORTAborts 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)ObjectRuns JSONPath on decoded body; returns null on errors.

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

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

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

Example: getDefaultSessionLifetime('loginInterceptor')
isBearerAuthorization()booleanAuthorization 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)booleanScope present?

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

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

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

Example: isJSON()
isXML()booleanMessage body is XML.

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

Example: base64Encode('string')