I have an XML Message with two parameters in it which I use to call a REST service endpoint. However, if any of them are a certain value I would like to change them before my call, for example
<Interface Server="ABC" Server2="DEF"/>
If any of those have the value "ABC" it should always be replaced with "BC" and in my call to the REST service I would send param1="BC" and param2="DEF" in the above example.
I was thinking of a Choice router and check if Server is "ABC" then set a flow-variable param1="BC" but then I realized I would have to do the same again for Server2 if that one is "ABC" ...and that feels like.. it must be an easier way to achieve this?
Am I right? Could I use some clever MEL or XPATH3 expression to always substitue the values to "BC" if any of them are "ABC"?
Regards
You can try the following configuration:
<enricher doc:name="Message Enricher">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var evaluation = "ABC"
%var substitution = "BC"
%function substitute(serverVal)(
serverVal when serverVal != evaluation otherwise substitution
)
---
payload.Interface.# mapObject {
($$): substitute($)
}
]]></dw:set-payload>
</dw:transform-message>
<enrich source="#[payload.Server]" target="#[variable:param1]"/>
<enrich source="#[payload.Server2]" target="#[variable:param2]"/>
</enricher>
Regardless how many attribute in your XML source, you just need to add the enricher element accordingly.
For example, you have a new XML source: <Interface Server="ABC" Server2="DEF" Server3="ABC"/>
Then you only need to add: <enrich source="#[payload.Server3]" target="#[variable:param3]"/> to set the new variable.
Notes: DataWeave is one of the EE features. For CE, you can replace it with other transformer, for example: Groovy. In the example below, the payload is in form of String. The original application/xml format is transformed to String using byte-array-to-string-transformer.
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[def attributeMap = new XmlSlurper().parseText(payload).attributes()
attributeMap.each() {
it.value = it.value == "ABC" ? "BC" : it.value
}
payload = attributeMap]]></scripting:script>
</scripting:component>
Related
In my case, I'm doing a migration from Mule 3 to Mule 4.
This flow, which includes transformers like DOM to XML, XML to DOM, and an expression component, needs to be migrated.
In Mule 4, I want to reuse the xrp file.
My flow for XML payload transform uses an XPR file by the expression component in Mule 3.
<flow name="rate-dtostepFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/dtostep" allowedMethods="POST" doc:name="HTTP"/>
<mulexml:xml-to-dom-transformer returnClass="org.dom4j.Document" doc:name="XML to DOM"/>
<set-variable variableName="domPayload" value="#[payload]" doc:name="set domPayload "/>
<expression-component file="xpr/responseStubCannasure.xpr" doc:name="Expression"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
</flow>
Input XML: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml
Output XML: https://github.com/Manikandan99/rate-dtostep/blob/master/response.xml
my MULE 3 application : https://github.com/Manikandan99/rate-dtostep/tree/master/rate-dtostep
ResponseStubcannsure xpr file:
import org.dom4j.*;
import java.util.*;
import java.util.logging.Logger;
Logger logger = Logger.getLogger("");
dtoCoverageStepsNodes = flowVars.domPayload.selectNodes("//DTOCoverage[#Status=\'Active\']/DTOSteps");
for (Node node : dtoCoverageStepsNodes){
//logger.info("inside: detach");
node.detach();
}
dtoCoverageNodes = flowVars.domPayload.selectNodes("//DTOCoverage[#Status=\'Active\']");
int i = 500;
for (Node node : dtoCoverageNodes){
//node.detach();
//logger.info("inside: assign prem");
node.addAttribute("FullTermAmt", Integer.toString(i));
node.addElement("DTOSteps");
stepNode = node.selectSingleNode("DTOSteps");
stepNode.addElement("DTOStep")
.addAttribute("Order","1")
.addAttribute("Name","Final Premium")
.addAttribute("Desc","Final Premium Desc")
.addAttribute("Operation","=")
.addAttribute("Factor",Integer.toString(i))
.addAttribute("Value",Integer.toString(i));
i+=1;
}
The xpr file transform the xml payload in the following ways:
updated the value of the DTOStep node.
The attribute value of DTOStep is autoincremented from 500 each time.
Please assist me.
You need to migrate the complete flow to Mule 4. The file responseStubCannasure.xpr is just a script in MEL (Mule 3 expression language). The extension is irrelevant, it could have been anything. MEL is very similar to Java so you could reuse the logic by encapsulating it into a Java class. You will need to add to the Java code the conversion to DOM4J from the input XML because Mule 4 doesn't support it. Probably is slightly easier to migrate the MEL script to a Groovy script because the basic syntax is very similar and both support scripts. Migrating to Java is just taking the additional steps of encapsulating the script into a method of a class and defining explicitly the types for variables.
Alternatively you could just delete the last 4 operations of the flow and replace them with a DataWeave transformation. Using a recursive function to navigate the keys and value recursively, using a condition to check if we are in the element DTOCoverage, with attribute Status == "Active" and then replace the nested element with the DTOSteps/DTOStep combination. Which is what your script does.
Example:
%dw 2.0
output application/xml
var startingValue=500
fun transformSteps(x, index)=
x filterObject ($$ as String != "DTOSteps") ++
{
DTOSteps: DTOStep #(Order:1, Factor: index + startingValue, Value: index + startingValue, Name:"Final Premiun", Operation:"=", Desc: "Final Premium Desc"): null
}
fun transformCoverage(x, index)=
{
val: x match {
case is Object -> x mapObject
if ($$ as String == "DTOCoverage" and $$.#Status == "Active")
{
DTOCoverage #(( $$.# - "FullTermAmt" ), FullTermAmt: $$$ + startingValue):
transformSteps($, index)
}
else
(($$): transformCoverage($, index+1))
else -> $
},
index: index
}
---
transformCoverage(payload,1).val
This solution doesn't completely resolve the Value and Factor (why two attributes with the same value?) sequential increase. You may need to do an additional transformation, or use Groovy or Java code to renumber them.
I have a sub flow where I want to do some action depending on uri.params values but facing issue then they are case sensitive that stop me to make a check as in some cases they come as AbcDef and sometimes as abcDef and also can be abcdef so is it possible to set value ignoring a case?
like:
%dw 1.0
%output application/java
---
{
MyKey: inboundProperties['http.uri.params'].equalsIgnoreCase(abcdef)
}
so in transform component I am expecting to receive such:
%dw 1.0
%output application/java
---
{
MyKey1: inboundProperties['http.uri.params'].abcd when
inboundProperties['http.uri.params'].abcd != null otherwise 0
MyKey2: inboundProperties['http.query.params'].efgh when
inboundProperties['http.query.params'].efgh != null otherwise 1
}
and then api call is:
http://*/flowName/{abcd}?efgh=SomeString
all working fine, but if
api call is:
http://*/flowName/{AbCd}?Efgh=SomeString
my subflow get nulls instead of values
in Dataweave there's no out-of-the-box function as in Java, but there's a simple alternative using either the upper or lower functions at the boolean condition, so you can compare both strings in the same representation.
e.g.
key: upper(inboundProperties['http.uri.params.ID']) == "ABCDEF"
The documentation for the upper function can be found here
The usual method to avoid case sensitiveness is to just change everything to lower or uppercase. In my example I just change the keys of the uri params map to lower case, so any later comparison should be made to lower case strings.
Example:
<http:listener config-ref="HTTP_Listener_Configuration" path="/test/{Param1}" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message">
<dw:input-payload mimeType="application/java" />
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
inboundProperties['http.uri.params'] mapObject (lower $$): lower $
]]>
</dw:set-payload>
</dw:transform-message>
Test:
$ curl http://localhost:8081/test/aBc
{
"param1": "aBc"
}
Hi I would like to seek for assistance for a DataWeave usage scenario.
I need to check if a variable for a card name exists (the card name is dynamic and cannot forsee beforehand).
If the variable already exists, then append current payload to that card name variable;
Else create the variable with the current payload
The problem is I do not have idea on how to refer to a variable with dynamic name.
I can save the current card name to a variable say "cardName", but how can I refer to the variable in DataWeave code afterwards?
Pseudoly below is what I would like to achieve
May I seek for advice on the way to achieve it?
You can access the vars using below notation
vars[dynamic variable]
As I do not know how your flow looks like and assuming you have a payload,
{
"data": [
{
"cardName": "cardName1",
"dataToMap": "first data"
},
{
"cardName": "cardName2",
"dataToMap": "2nd data"
},
{
"cardName": "cardName1",
"dataToMap": "2nd data for card name 1"
}
]
}
You can loop through the payload.data (using for each) and you can map it as
%dw 2.0
output application/java
var varName = payload.cardName
---
if (vars[varName] != null)
vars[varName] ++ "** **" ++ payload.dataToMap
else
payload.dataToMap
and have a set variable with name set with #[****] to dynamically choose the variable.
End result of this will have two vars with name cardName1 and cardName2 and their corresponding value will be "first data** **2nd data for card name 1" and "2nd data", respectively.
Here is an example flow of dynamically naming variables using a Set Variable component inside a For-Each loop.
This is a good way to persist data after the For-Each loop exits, since the payload resets to the payload before the For-Each scope is called.
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<flow name="dynamic-varsFlow" >
<scheduler doc:name="Scheduler" >
<scheduling-strategy >
<fixed-frequency frequency="10000"/>
</scheduling-strategy>
</scheduler>
<set-payload value='#[output application/json
var someInput = {
"data": [
{
"cardName": "cardName1",
"dataToMap": "first data"
},
{
"cardName": "cardName2",
"dataToMap": "2nd data"
},
{
"cardName": "cardName1",
"dataToMap": "2nd data for card name 1"
}
]
}
---
someInput.data]' doc:name="Set Payload" />
<foreach doc:name="For Each" >
<set-variable value='#[output application/json
var varName = payload.cardName as String
---
(if (vars[varName] != null)
vars[varName] ++ "** **" ++ payload.dataToMap
else
payload.dataToMap
)]' doc:name="Set Variable" variableName="#[payload.cardName]"/>
</foreach>
<foreach doc:name="Copy_of_For Each" >
<logger level="INFO" doc:name="Logger"
message="#[output application/json --- {varName: payload.cardName, varValue: vars[payload.cardName]}]"/>
</foreach>
</flow>
</mule>
`
Here are the log messages for each of the dynamically named vars inside the second For-Each loop. Notice there are two new vars dynamically named from the initial Set Payload data. In a more real-world flow, the payload would be read from the output of the Event Source, such as from an HTTP Connector or DB Connector:
Here is the payload and vars after the second For-Each scope is exited. The payload reverts back to it's initial value, but the changes made inside the first For-Each scope persist in the two vars:
You should use a Java map instead and use cardName as the key.
ok i might have not understood the ask 100% but i did a pretty simple flow to illustrate setting a variable dynamically if that variable doesnt exist. hope that helps you upto some extent. Here is how my complete flow looks like
in the set payload i am setting a dummy payload:
in the transform message next i declared a variable card which doesnt exists until then as you can see in the flow
and finally in the last set payload i am setting the payload with whatever vars.card hold
and i get the response back when i test my API
now if you want to dynamically declare the variable name with some value i believe that can be done too.
How to use the lookup function in data weave(1.0) which calls a flow with some parameters. Flow functionality is to convert the values in the .csv file to JSON based on the parameter in the lookup function.
The lookup fucntion docs are here:
https://docs.mulesoft.com/mule-runtime/3.9/dataweave-language-introduction
You pass in the payload as the second parameter as a Map (object):
%dw 1.0 %output application/json
---
{ a: lookup("mySecondFlow",{b:"Hello"}) }
Here is a Mule flow that can accept this object with the 'b' key:
<flow name="mySecondFlow">
<set-payload doc:name="Set Payload" value="#[payload.b + ' world!' ]"/>
</flow>
This example will produce this output:
{
"a": "Hello world!"
}
I am trying convert xlsx to json with dataweave. JSON output is getting generated without headers from the xlsx file. My current code is skipping fisrt row of an excel file. I also need to include firstrow from the input file to output JSON. Below is my dataweave:
<dw:transform-message doc:name="Transform Message" metadata:id="8211af7d-2465-4ecd-80ea-3b6771d094e5">
<dw:input-payload mimeType="application/xlsx"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
Sheet1:payload.Sheet1 map
{
col1:$[0],
col2:$[1],
col3:$[2]
},
Sheet2:payload.Sheet2 map
{
col1:$[0],
col2:$[1],
col3:$[2]
}
}]]></dw:set-payload>
</dw:transform-message>
Input Excel file is as:
Total Value Count
Col1 Col2 Col3
Val Val Val
I was able to figure it out by adding below code to my xml:
<dw:input-payload mimeType="application/xlsx">
<dw:reader-property name="header" value="false" />
</dw:input-payload>
This worked for my requirement.