how to query bufferInput stream in mule esb - mule

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!

Related

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.

Mule print BufferInputStream using payloadAs

The log message below is aimed at printing out a json error response from my api.
<logger message="#[exception.getCauseException()?.getMuleMessage()?.getPayload()]" level="INFO" doc:name="Logger"/>
The result of this log message is
org.glassfish.grizzly.utils.BufferInputStream#486bf09a
I have a requirement to pass the json response. How can I turn the above log line into something like this where it will pass the stream rather than just printing BufferInputStream.toString()...
<logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
I tried the following below that does not work.
<logger message="#[exception.getCauseException()?.getMuleMessage().payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
By the way logging a stream is not the real purpose as to why I want to parse the stream. I know that is an expensive operation. I really need to pass the stream to get some meaningful information from the response.
thanks
Yes, you are correct, you are going to have to convert the output into a string to access it. Then you will have to manipulate the output using string methods to get the desired information.

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

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