How to get the value using 'http.uri.params' in mule - mule

I am trying to get the value through URI params in mule. I am using
`#[message.inboundProperties.'http.uri.params'.Id]`
to get the value. When I pass a value, (for eg 16) it is returning as Id=16. Since I am passing this value into a database stored procedure, I need the value alone. Could anyone help me out in this.

This works for me in Mule 3.7.3:
<http:listener-config name="listener2" host="0.0.0.0" port="8083"/>
<flow name="uri">
<http:listener path="uri/{param}/resource" config-ref="listener2" />
<expression-transformer expression="#[message.inboundProperties.'http.uri.params'.param]" />
</flow>
Running curl http://127.0.0.1:8083/uri/value/resource returns value which is the expected according to the documentation.

What about storing "id=16" as a string variable and splitting it on the =?

Using
#[message.inboundProperties.'http.uri.params'[0]['Id']
will retrieve only the value of the id. This will surely work.

Related

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"/>

to fetch a specific value from resultset in Mule

I have a flow that calls a MS SQL stored procedure which returns a value. The output that i get from the DB is {resultset1=[{id=30}]} i would like to store the value of id to flow variable
couldn't find a successful way to do this, Could somebody please help me in this
I'am using mule server 3.4.0 EE
Thank you in Advance
First place a json-to-object-transformer and then try the following :-
<json:json-to-object-transformer returnClass="java.util.HashMap" />
<set-variable variableName="id" value="#[payload.resultset1[0].id]" doc:name="Variable"/>

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

append specieal charecters in payload tag in Mule ESB

i want payload value like below in <http:outbound-endpoint>:
<set-variable variableName="url" value="#[json:url]" doc:name="Variable"></set-variable>
<set-variable variableName="payload" value="#[json:param]" doc:name="Variable"></set-variable>
<http:outbound-endpoint exchange-pattern="request-response" address="http://admin:admin##[url.substring(7)]" method="POST" doc:name="HTTP" password="admin" user="admin">
<set-payload value="action=start&params={input:#[payload]}&createTask=false&parts=all"/>
</http:outbound-endpoint>
but it is giving error.
thanks.
You can get rid of the error by replacing & with &
However, that is not your only problem.
payload is a reserved variable to hold the current payload. You can not use set-variable with variableName="payload", your setting will just be ignored when you call #[payload] later.
Your POST data would be something like action=start&params={input:yourdata}&createTask=false&parts=all. This is some kind of hybrid of JSON and HTTP GET syntax, and I doubt that this is what you are trying to achieve. If you want to send a POST request in key=val format, set a payload of type Map. If you want to send the request as JSON, set a payload with a JSON String, you have the object-to-json-transformer to help in Mule. If you want to have parameters in the URL, put them in the URL. But you can not mix these different syntaxes.

Mule Performing a string manipulation

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