Mule mel 5_tips

7
Mule Concepts Mule Expression Language (MEL) Basics -V

Transcript of Mule mel 5_tips

Page 1: Mule mel 5_tips

Mule Concepts

Mule Expression Language (MEL) Basics -V

Page 2: Mule mel 5_tips

• Payload and Attachments– To copy the current payload in a flow variable

named originalPayload then restore it :<set-variable variableName="originalPayload" value="#[message.payload]" /> <set-payload value="#[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]

Mule Expression Language (MEL)

Page 3: Mule mel 5_tips

• 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.#[regex(regularExpression [, melExpression [, matchFlags]])]– Example :

• to select all the lines of the payload that begin with To:, From:, or Cc: use:#[regex('^(To|From|Cc):')]

Mule Expression Language (MEL)

Page 4: Mule mel 5_tips

• XPath Support– XPath helper functions return DOM4J nodes.– By default the XPath expression is evaluated

onmessage.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]

Mule Expression Language (MEL)

Page 5: Mule mel 5_tips

• 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')].priceThe 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]]" />

Mule Expression Language (MEL)

Page 6: Mule mel 5_tips

• Including DataWeave code– You can carry out powerful complex data transformations by including

MEL DataWeave Functionsthat use DataWeave Language code. – You can include this code via two different functions in MEL: 'dw()' and

'split()'.– 'dw' simply executes the DataWeave code you pass as an argument

and returns the transformation’s result– 'split()' executes the code you pass as an argument and returns an

iterator that allows you to process each instance of the output as a separate message.

dw("myobject:{id:payload.accountid, user:payload.user}")

Mule Expression Language (MEL)

Page 7: Mule mel 5_tips

• Miscellaneous Operations– Assign to variable lastname the value of the message

inbound property lastname:#[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()]

Mule Expression Language (MEL)