Mule Performing a string manipulation - mule

What is the best way to perform a string manipulation. I wish to perform a substring on a email address to extract the domain detail and populate this to a variable.
a java transformer is a possibilty, but i was hoping if i could use a message enricher with a expression to perform this operation.
pardon me but i am still a greenhorn on Mule.
here is the excerpt from my mule flow which is failing with error cannot resolve method string length.
<enricher target="#[flowVars['FromAddressDomain']]" doc:name="Message Enricher">
<expression-transformer expression="#[ payload.fromAddr.substring(payload.fromAddr.lastIndexOf('#')+ 1,payload.fromAddr.lenth())]" doc:name="Expression"></expression-transformer>
</enricher>

Simply use:
<set-variable variableName="FromAddressDomain"
value="#[org.mule.util.StringUtils.substringAfter(payload.fromAddr, '#')]" />

You can use dataweave transform on payload and use the operator splitby and spilt on # character. Please take a look at below link for more information on splitby operator
https://docs.mulesoft.com/mule-user-guide/v/3.9/dataweave-operators#split-by

Related

how to query bufferInput stream in mule esb

I am spending a lot time to understand how to convert a bufferInput stream or Dataweaveoutputhandler to other datatypes such as string or object or xml
I am constantly getting this type of error whenever I changed expression
Execution of the expression "xpath3('/*',payload,'NODESET')" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: BufferInputStream (org.mule.api.transformer.TransformerMessagingException). Message payload is of type: BufferInputStream
Please help me to know if anyone has solved this issue.
Exception is thrown by wrong xpath3 expression, not wrong payload type. You use xpath3 in splitter? Paste some xml, below working xpath3 expression example.
<splitter expression="#[xpath3('//YOUR_NODENAME',payload, 'NODESET')]" doc:name="Splitter"/>
If you want to log payload just try:
<logger message="Response := #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
Apache provide us with great library to convert between various datatypes.
http://axis.apache.org/axis2/java/core/apidocs/org/apache/axis2/databinding/utils/ConverterUtil.html#convertToDataHandler(java.lang.String)
kindly go with this.
Thanks!

Mule:Retrieving object store in an MEL in Mule 3.5

Having a requirement to test a object store whether it contains a key or not in a choice router
<objectstore:config name="storeDownload" doc:name="ObjectStore" persistent="false" partition="test"/>
<choice>
<when expression="#[app.registry.storeDownload.contains('#[flowVars.startKey]').equals('false')]">
Getting an error
1. Expression Evaluator "registry" with expression "ON" returned null but a value was required. (org.mule.api.expression.ExpressionRuntimeException)
org.mule.expression.RegistryExpressionEvaluator:101 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/ExpressionRuntimeException.html)
2. Failed to invoke store. Message payload is of type: byte[] (org.mule.api.MessagingException)
The main issue is that you are embedding MEL into MEL which can't work. Also the boolean-as-string comparison is dodgy.
Replace this:
#[app.registry.storeDownload.contains('#[flowVars.startKey]').equals('false')]
with that:
#[!(app.registry.storeDownload.contains(flowVars.startKey))]
My use case was a bit different to Nazar's I needed to monitor a long running process which can take up to four hours.
In the first flow I generate a key value with a time stamp in it as the payload and then use it to set the ProcessState to the static value 'Started' in an ObjectStore as shown below. After which I fire a Quartz Outbound Endpoint with a four hour delay.
<objectstore:store config-ref="MonitoredProcess" value-ref="Started" key="#[payload]" doc:name="ObjectStore"/>
<quartz:outbound-endpoint jobName="ProcessMonitor" responseTimeout="10000" doc:name="Quartz"
repeatInterval="0" repeatCount="0" startDelay="${process.monitor.event.start.delay}">
<quartz:scheduled-dispatch-job>
<quartz:job-endpoint address="vm://processMonitorQueue"/>
</quartz:scheduled-dispatch-job>
</quartz:outbound-endpoint>
And I got the same exception.
After scratching my head and lots of searches the name of the variable 'value-ref' in combination with David's answer above finally revealed my problem namely the MEL is always invoked for this ref field.
As soon as I changed the field to an expression #['Started'] that MEL could evaluate my problem went away.
<objectstore:store config-ref="MonitoredProcess" value-ref="#['Started']" key="#[payload]" doc:name="ObjectStore"/>
For completeness I've included the code that retrieves the ProcessState from the ObjectStore. Note the defaultValue-ref also needs to use MEL
<vm:inbound-endpoint exchange-pattern="one-way" path="processMonitorQueue" doc:name="VM" />
<objectstore:retrieve config-ref="MonitoredProcess" defaultValue-ref="#['DoesNotExist']" key="#[payload]" targetProperty="processState" doc:name="ObjectStore"/>

Mule MEL to do substring on a flow variable

I have a variable my_variable with value as a dynamic URL like --
http://stackoverflow.com/questions/ask
I want to do a substring on this dynamic URL to find the string after last "/" i.e. in case above mentioned URL, I want to get the substring "ask"
How can I use MEL to do that?
You can use the string functions which are availalbe from the java.lang package.
#[flowVars['my_variable'].substring(flowVars['my_variable'].lastIndexOf('/'))]
Hope this helps.
You can use JDK classes in MEL, in fact these packages are auto imported (http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Reference):
java.lang.*
java.io.*
java.net.*
java.util.*
java.math.BigDecimal
java.math.BigInteger
javax.activation.DataHandler
javax.activation.MimeType
java.util.regex.Pattern
org.mule.api.transformer.DataType
org.mule.transformer.types.DataTypeFactory
This flow receives requests on port 8081 and return the last part of the path:
<flow name="testedbFlow3">
<http:inbound-endpoint host="0.0.0.0" port="8081" />
<expression-transformer expression="#[message.inboundProperties['http.request'].split("^.*/")[1]]"/>
</flow>
You can use basic Java String methods such as substring() and lastIndexOf() on the variable.
#[flowVars['my_variable'].substring(flowVars['my_variable'].lastIndexOf('/'))]
which is simpler one

XPATH in mule flow returns multiple values

I have a requirement where I run xPath evaluator against xml payload in mule flow. This xPath evaluator can return single or multiple values. I need to store these values in flow variable and use somewhere later in the flow. Can someone help me implementing this changes?
Appreciate your help on this.
Thanks
For extracting values from an XML document, use the XPath extractor.
<mulexml:xpath-extractor-transformer expression="/a:my/b:xpath/text()"/>
You can also use Mule Expression Language to create dynamic XPath expressions:
<expression-transformer mimeType="text/xml" evaluator="xpath" expression="//school/day[#date= #[function:datestamp:yyyy-MM-dd] ]/name
"/>
However this can get somewhat messy for complicated expressions, so I have created my own dynamic XPath transformer:
<dx:dxpath expression="/b:team[name = $teamName]/b:player[b:name = $playerName]/b:goals/text()">
<dx:variable key="playerName" value="#[header:invocation:playerName]"/>
<dx:variable key="teamName" value="#[header:invocation:teamName]"/>
<!-- unlimited number of variables -->
</dx:dxpath>
which is somewhat more easy on the eyes.
Then Wrap your flow with an ericher:
<enricher target="#[variable:myData]">
<processor-chain>
<!-- your flow here -->
</processor-chain>
</enricher>

Mule choice compare

In mule flow I need to compare the last two params from an input string message
2012-04-30,2012-05-30,1,5
And if param1(1) <= param2(5), I need perform jms:outbound-endpoint.
Is there a standard choice element to do it? Could you please provide example.
While using mule 3.2.x your best option is to use the groovy evaluator and use a choice router that look like the following
<when expression="#[groovy:payload.split(',')[2] <= payload.split(',')[3] ]">
<processor-chain>
<jms:outbound-endpoint queue="out" doc:name="JMS"/>
</processor-chain>
</when>
Otherwise if you are willing to upgrade to Mule 3.3.0 you can take advantage of the new Mule Expression Language that allows to do the same without the need of the groovy operator
you can also use splitter and split on the basis of ',' and then have the compare logic for the params
You need not use Groovy for this. You can directly apply the split() function on payload.
<when expression="#[payload.split(',')[2]<=payload.split(',')[3]]">
<processor-chain doc:name="Processor Chain">
<jms:outbound-endpoint queue="data" connector-ref="Active_MQ"doc:name="JMS"/>
</processor-chain>
</when>
Apart from expression filter you can also use choice flow control. Capture the 2 values to be compared in a flowvars and use below expression in when condition of choice flow control.
[flowVars.param1 <= flowVars.param1]
Giving below the link for more information.
https://docs.mulesoft.com/mule-user-guide/v/3.7/choice-flow-control-reference
you can take advantage of the new Mule Expression Language that allows to do the same without the need of the groovy operator