mule getting value from the url to message payload - mule

I have url as:
localhost:8081/urltest?company=test?emplId=1234
I need to get the value of company and emplId in payload. Do I need to use transformer and get all the values or can get the values from payload in mule expression?

Try with MEL expresssion to get the values like :-
#[message.inboundProperties['company']] and #[message.inboundProperties['emplId']]
Check wheteher you are getting the values in logger like the following :-
<logger message="#[message.inboundProperties['company']]" level="INFO" doc:name="Logger"/>
and
<logger message="#[message.inboundProperties['emplId']]" level="INFO" doc:name="Logger"/>
and I beleive your url format should be like :-
localhost:8081/urltest/?company=test&emplId=1234
as I beleive localhost:8081/urltest?company=test?emplId=1234 may not work
for your reference .. I found an article :- http://wiki.marketruler.com/What_is_the_correct_syntax_for_query_strings%3F

use this expression you get userid from url
<logger message="#[message.inboundProperties['userid']]" level="INFO" doc:name="Logger"/>

Related

How to concatenate in mule logger?

I need to concatenate text with json payload in logger component. I have tried below ways but no luck
<logger level="INFO" doc:name="Logger" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" message="#['payload is:' + payload]"/>
Even i don't see any transformers like 'object to string converter' in mule 3.
Please suggest syntax for mule 4
Try separating the text from your dataweave
i.e.
<logger level="INFO" doc:name="Logger" mesage="Payload is: #[payload]" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" />
All the various transformers were removed in Mule 4 due to the payload always being "accessible". That is, regardless of the payload type (XML, JSON, Java, CSV...) you can access fields through payload.{fieldname}. In Mule 3.x the payload had to be coerced to a Java object to allow that. You can explicitly set the output type of any dataweave expression, so you can also try:
mesage="Payload is: #[output application/java --- payload]"
It is working with below syntax
<logger level="INFO" doc:name="Logger" doc:id="38de876a-a64f-4d83-86a1-ef4cbbda167c" message="#['payload is:' ++ payload]"/>
I had the same issue and the below worked for me...
<logger level="INFO" doc:name="Logger" doc:id="35d4566e-02ba-495a-bd40-c30aa5a90413" message="#['Get Accounts Response Paylaod : #[payload]']"/>

mule org.json.JSONObject returning property value as null though the json property does have value for it

I am using the below code to convert the input payload string to json in mule. The below code sometimes working and sometimes not. its not working on standalone and working on studio. Not able to nail down the exact cause for it. but based on the loggers that i see that the property value is coming null after the expression statement. i am suspecting this could be with the jar that's getting used here. i am still digging further on it.
<logger message="input: #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.'data']" mimeType="application/json" doc:name="Set Payload" encoding="ISO-8859-2"/>
<logger message="createConnection: #[payload]" level="INFO" doc:name="Logger"/>
<expression-component doc:name="Expression"><![CDATA[String input = payload;
payload = new org.json.JSONObject(input);
]]></expression-component>
<logger message="before json to object: #[payload.con_id] #[payload.'con_id']" level="INFO" doc:name="Logger"/>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
Input JSON:
data: {"name":"QA_tst2","description":"tst","con_id":10,"con_connection_id":null,
"verticalParam":[{"param_value":"abc","param_name":"Host"},{"param_value":"21","param_name":"Port"}],"CON_CATEGORY_NAME":"File"}
I don't think that notation will work for JSONObject, try using
payload.get('con_id')
as per the javadoc: https://stleary.github.io/JSON-java/org/json/JSONObject.html.
The reason this won't work with the notation you have tried, is that Mule supports that notation for Maps, and org.json.JSONObject does not implement java.util.Map. You could try using javax.json.JSONObject instead, which will support that notation.
I have figured out the current issue. if there is any logger added to fetch the properties from the payload right after the expression component then its screwing up further. if you just remove the logger that was added after the expression component then after json to object conversion, i am able to fetch the values. that solves the current issue. but i would like to understand the difference between fetching the properties #[payload.con_id] vs #[payload.'con_id']. i can start a separate conversation for the same.

Return individual values from linkedHashMap in Mule

I have an output from a webservice in Mule that returns a linkedHashMap and I need to get the individual values to be dynamically inserted into a template. The template is used to send email through the SMTP connector. I can get all values using MEL #[payload], but I can't get them one by one. I've tried #[payload.get(0)], #[payload[0]] but they all return null.
The Mule XML looks like this:
<flow name="MW_Flow">
<file:inbound-endpoint path="C:\....\1" connector- ref="File" responseTimeout="10000" doc:name="File" pollingFrequency="60000"/>
<ws:consumer config-ref="File_Read_WS" operation="all3" doc:name="FileRead DBWriter WS"/>
<dw:transform-message metadata:id="6ee92ba8-9f67-40d6-bfa3-3e237da20822" doc:name="Transform Message">
<foreach doc:name="For Each">
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<parse-template location="C:\.....\Templates\Mail.txt" metadata:id="b7d894eb-465b-47f7-a542-b49fc4fb53d9" doc:name="Parse Template"/>
<logger message="2: #[message.exception] #[message.dataType] #[payload]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
The template (plain text file) looks a bit like this:
Hello [name].
This is email from [name2]. The following event [event].....
All I get are null values except when using #[payload] which returns the whole row (4 values).
Any help greatly appreciated!
/Johan
If your payload is a Map then payload.get(0) or payload[0] will behave as if you are trying to get a value from map with 0 as key, which I guess doesn't exist in your map.
Try accessing it with name - #[payload.name] or #[payload.name2] or #[payload[name]]

Mule getting data from ConsumerIterator

My mule flow is:
<sfdc:query config-ref="SFDC__DevInt" query="dsql:SELECT Id FROM Account WHERE Name = #[flowVars.Name]" doc:name="Salesforce"/>
<logger message="Select query: #[message.payload]" level="INFO" doc:name="Logger"/>
<foreach doc:name="For Each">
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
<logger message="#[message.payload.Id]" level="INFO" doc:name="Logger"/>
</foreach>
Above for loops returns the id. Instead of looping is there any direct way of getting data as iterator object? i.e I need to get the value of id.
Data inside for loops returns:
SFDC:Select query: org.mule.streaming.ConsumerIterator#2f23abf
INFO 2015-06-22 13:10:25,520
{Id=0011100000uPbqeAAC, type=Account}
At a time I will be getting single data.
I tried with #[message.payload.iterator().hasNext().next()], but getting
Message : Execution of the expression "message.payload.iterator().hasNext().next()" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: ConsumerIterator
Code : MULE_ERROR--2
The message payload after sfdc:query is an instance of org.mule.streaming.ConsumerIterator, which implements java.util.Iterator. Therefore there's no reason to call iterator() on it, it is already an iterator.
Moreover hasNext returns a boolean, not an Iterator instance, so you can't chain the calls as you did.
Your expression should thus be:
#[message.payload.hasNext() ? message.payload.next() : null]
assuming that null is the value you want in case the enumeration is empty.
You can also convert the iterator into a list if you need to use for-each
#[org.apache.commons.collections.IteratorUtils.toList(payload)]

How to Extract the Flow Name and MessageProcessor Name using MEL - MULE ESB

I'm not sure, how can we extract the flow-name and message-processor Name through MEL. For example I have multiple message processor. For logging, i need to extract the flow Name and Message-processor, so that I can find out transaction has crossed this particular flow and its message processor. Is there any simple way to find out. Please guide me. Please find the screenshot below. Here i need to Extract - set payload and its flowName (flow1)
Thanks in advance.
For mule 3.8+ version onwards #[flow.name] don't work.
Use #[mule:context.serviceName] expression in logger or component to extract the name of the flow
I know this post is old but I have been trying to find a way to do this in MEL for error handling emails.
For the flow name you can use #[exception.event.flowConstruct.name]
for the failing message processor you can use #[exception.failingMessageProcessor].
Both of these work in MEL without the need to use an flowVar.
Please note however, that the failing processor does not always come back with a value but comes back with null, I'm not sure why.
You can extract the flow-name with MEL : #[flow.name]
<flow name="name" doc:name="name">
<http:inbound-endpoint address="http://localhost:8090/resources" doc:name="HTTP" />
<logger message="name of flow: #[flow.name]" level="INFO" doc:name="Logger"/>
<set-payload value="name" doc:name="Set Payload"/>
</flow>
or
flowConstruct.getName() in a Message Processor
Two ways to acthive this (from current flow name)
First one is -
<logger message="Current flowName: #[flow.name]" level="INFO" doc:name="Logger"/>
and the second one is -
<logger message="Current flowName: #[context:serviceName]" level="INFO" doc:name="Logger"/>