<expression-component>
targetDir = new java.io.File(server.tmpDir, 'target');
targetDir.mkdir();
payload = targetDir
</expression-component>
MEL Cheat Sheet
The following document reproduces the content from the original Mule Expression Language (MEL) Reference card. See also Mule Expression Language Reference.
Prerequisites
This document assumes you are familiar with Mule Expression Language (MEL).
Server, Mule, Application, and Message Variables
Overview of the global contexts and the variables to which they give access.
server | mule | app | message |
---|---|---|---|
fileSeparator |
clusterId |
encoding |
ID |
host |
home |
name |
rootId |
ip |
nodeId |
standalone |
correlationId |
locale |
version |
workDir |
correlationSequence |
javaVersion |
registry |
correlationGroupSize |
|
javaVendor |
replyTo |
||
osName |
dataType |
||
osArch |
payload |
||
osVersion |
inboundProperties |
||
systemProperties |
inboundAttachments |
||
timeZone |
outboundProperties |
||
tmpDir |
outboundAttachments |
||
userName |
exception |
||
userHome |
|||
userDir |
Create a directory named target
in the system’s temporary directory and set the payload to the variable storing the file object:
Set the username and password for an HTTP request at runtime based on inbound message properties:
<http:request-config name="HTTP_Request_Configuration" host="api.acme.com/v1" port="8081" doc:name="HTTP">
<http:basic-authentication username="#[message.inboundProperties.username]" password="#[message.inboundProperties.password]"/>
</http:request-config>
<flow>
...
<http:request config-ref="request-config" path="users" doc:name="HTTP Connector"/>
...
</flow>
Variables, Integers, and Strings
Mule accepts #[payload]
as an expression, rather than #[message.payload]
because it knows to automatically evaluate the expression for the message context object. This shortcut only applies to the payload
field.
-
Mule assumes that any unquoted string contained in a Mule Expression is a flow variable. To set the value of flow variable 'foo' to 'bar', the expression
#[foo == bar]
actually evaluates as:#[flowVars.foo == flowVars.bar]
-
To access the flow variable, use one of the following expressions:
#[flowVars.foo]
#[foo]
-
To create an integer:
#[12]
-
To create a string:
#['mystring']
Flow and Session Variables
Flow variables are available in the flowVars
context or directly as top level variables (unless autoResolveVariables is false or their name violates the MEL naming conventions). Session variables are accessible across flows, applications and even servers. You can set a session variable using the session variable transformer.
The following shows how a bean can be dynamically retrieved from the registry, based on a name created by taking a value from a bean payload with a getTargetService() accessor:
<set-variable variableName="beanName" value="#[message.payload.targetService+'Processor']" />
<set-variable variableName="bean" value="#[app.registry[beanName]]" />
Payload and Attachments
Copy the current payload in a flow variable named originalPayload
then restore it:
<set-variable variableName="originalPayload" value="#[message.payload]" />
<set-payload value="#[flowVars['originalPayload']]" />
To retrieve the message payload in a particular format, using Mule’s auto-transformation capability, use payloadAs
:
<logger message="#[message.payloadAs(java.lang.String)]" />
To extract all *.txt and *.xml attachments, use a filtered projection:
<expression-transformer expression="#[($.value in message.inboundAttachments.entrySet() if $.key ~= '(.*\\.txt|.*\\.xml)')]" />
To ask for a null payload:
#[payload is NullPayload]
Regex Support
Regular expression helper functions retrieve null
, a single value or an array of values, depending on matches. The forms that take a melExpression
argument apply the regex to the result of its evaluation instead of message.payload
. For more information, see MEL Regex.
#[regex(regularExpression [, melExpression [, matchFlags]])]
For example to select all the lines of the payload that begin with To:
, From:
, or Cc:
use:
#[regex('^(To|From|Cc):')]
XPath3 Support
XPath3 helper functions can return a DOM Node, or collection of DOM Nodes, or text.
For more information, see XPath and
MEL XPath.
By default the XPath3 expression is evaluated on message.payload
unless an xmlElement
is specified:
xpath3(xPathExpression [, xmlElement])
To get the text content of an element or an attribute:
#[xpath3('//title').text]
#[xpath3('//title/@id').value]
JSON Processing
MEL has no direct support for JSON. The json-to-object-transformer
can turn a JSON payload into a hierarchy of simple data structures that are easily parsed with MEL.
For the equivalent of this JSON path expression:
$..[? (@.title=='Moby Dick')].price
The following uses a filtered projection:
<json:json-to-object-transformer returnClass="java.lang.Object" />
<expression-transformer
expression='#[($.price in message.payload if $.title =='Moby Dick')[0]]" />
Tests
-
Return
true
if thelastname
query string parameter from an HTTP listener is notnull
:#[message.inboundProperties.'http.query.params'.lastname != null]
-
Return the number of elements in
http.method
:#[message.inboundProperties.'http.query.params'.size()]
-
Return
true
if the number of elements in the maphttp.query.params
is greater than 50:#[message.inboundProperties.'http.query.params'.size() > 50]
-
Testing for Emptiness: The special literal
empty
tests the emptiness of a value. It returns an empty value depending on context.empty
evaluates to:-
null
-
boolean
false
-
empty strings or strings containing only white space
-
zero
-
empty collections
The expression
#[foo == empty]
evaluates to true if the value iffoo
satisfies any of the requirements for emptiness.
-
Testing for NullPayload: Return true if message payload is null:
#[payload == null]
-
Chained Elements
For chained methods or properties, the expression #[[a.b.c] == 'foo']
evaluates correctly even in the case that c
is a null value. However, if b
is a null value, the expression throws a NullPointerException
.
In this example, if a field named address is null, the expression throws a NullPointerException
.
#[payload.address.zipcode]
To make this same expression null
safe, use the .? operator.
#[payload.address.?zipcode]
Using this operator avoids a NullPointerException
if address is an empty value, instead it returns null
.
If you’d like the expression to return a different value if no address is defined, you can use a chained or
operator.
#[payload.address.?zipcode or 'Zipcode not set']
Global Configuration
Define global imports, aliases and global functions in the global configuration element. Global functions can be loaded from the file system, a URL, or a classpath resource (see extraFunctions.mvel
in line 6 below). Flow variables auto-binding can be turned off via the autoResolveVariables attribute in line 2.
<configuration>
<expression-language autoResolveVariables="false">
<import class="org.mule.util.StringUtils" />
<import name="rsu" class="org.apache.commons.lang.RandomStringUtils" />
<alias name="appName" expression="app.name" />
<global-functions file="extraFunctions.mvel">
def reversePayload() { StringUtils.reverse(payload) }
def randomString(size) { rsu.randomAlphanumeric(size) }
</global-functions>
</expression-language>
</configuration>
Advanced Tips
Accessing the Cache
You can access the Mule cache through the object store that serves as the cache repository. Depending on the nature of the object store, you can count, list, remove, or perform other operations on entries.
The code below shows the XML representation of a cache scope that uses a custom object store class.
<ee:object-store-caching-strategy name="CachingStrategy">
<custom-object-storeclass="org.mule.util.store.SimpleMemoryObjectStore" />
</ee:object-store-caching-strategy>
The object store above is an implementation of a ListableObjectStore
, which allows you to obtain lists of the entries it contains. You can access the contents of the cache by invoking the getStore
method on the CachingStrategy
property of app.registry
.
The expression below obtains the size of the cache by invoking allKeys()
, which returns an iterable list.
#[app.registry.CachingStrategy.getStore().allKeys().size()]"
If you need to manipulate the registry in a Java class, you can access it through muleContext.getRegistry()
.
Boolean Operations Gotchas
-
Boolean evaluations sometimes return unexpected responses, particularly when the value of a variable contains "garbage." See tables below.
Expression When value of var1
is…… The expression evaluates to… #[var1 == true]
'true'
true
#[var1 == true]
'True'
'false'
false
#[var1 == true]
'u5hsmg930'
true
Expression When the value of something
is…… And the value of abc
is…… MEL successfully evaluates the expression. #[payload.something.abc == 'b']
'something'
'null'
✔
#[payload.something.abc == 'b']
'null'
'abc'
X
Produces a NullPointer exceptionNote also that, if given the expression
#[flowVars.abc.toString()]
and the value of ‘abc’ isnull
, Mule throws aNullPointerException
.
Miscellaneous Operations
-
Assign to variable
lastname
the value of the message inbound propertylastname
:#[lastname = message.inboundProperties.lastname]
-
Append a string to the message payload:
#[message.payload + 'mystring']
-
Call a static method:
#[java.net.URLEncoder.encode()]
-
Create a hash map:
#[new java.util.HashMap()]
More MVEL Goodness
Quick access to the MVEL 2.0 Documentation is available from github in MVEL (MVFLEX Expression Language).
-
Java interoperability - Creates a random UUID and use it as an XSL-T parameter:
<mulexml:context-property key="transactionId" value="#[java.util.UUID.randomUUID().toString()]" />
-
Safe bean property navigation - Retrieves
fullName
only if thename
object is not null:<set-variable variableName="fullName" value="#[message.payload.?name.fullName]" />
-
Local variable assignment - Works to split a multi-line payload into rows and drops the first row, as in this splitter expression:
splitter expression='#[rows=StringUtils.split(message.payload,'\n\r'); ArrayUtil.subarray(rows,1,rows.size())]" />
-
Elvis operator - Returns the first non-null value of a list of values:
#[message.payload.userName or message.payload.userId]
Note: Mule checks the operands for emptiness, but not when a value is set to
null
.For example:
If you set
myop = ""
, Mule detects the operand as null. However, if you setmyop = null
, Mule does not detect thatmyop
is null.