My DataWeave configuration is as below.
<dw:transform-message metadata:id="5b272b5c-4f37-4e09-8608-756169041833" doc:name="Transform Message">
<dw:set-payload resource="file:D:/Disc/sample.dwl"></dw:set-payload>
</dw:transform-message>
I am trying to externalize the transformation logic from the above.
My question is: Instead of using the file path directly in the RESOURCE attribute can I use a variable which has the file path in it? If yes, how do I achieve it? Thanks in advance.
Use properties, with properties you can replace this value and store them in an external file:
https://docs.mulesoft.com/mule-user-guide/v/3.7/configuring-properties
Related
I am new to Mule and I am using Mule version 3.x. We can set values of variable using Set Variable Component. And using expression component, we can set multiple variables at a time. But expression component does not allow to Declare a flow variable. So I have to first use 3 Set Variable component and then use expression to set them at one go(if it needs to be changed).
In case of multiple variables, is there any way to declare and initialize variable in one component instead of having a separate Set Variable component to declare it?
The Message Properties Transformer
If you want to set multiple flow vars in a single component in Mule 3, and you don't need to do any transformation beforehand, the Message Properties transformer is probably the most succinct ways to do this. I like it because in addition to being able to set multiple flow vars in a single component, the XML is clear, and when you click on the component in AP Studio, the UI makes it immediately obvious that you're setting multiple variables with a single component. Just make sure to use scope="invocation" so that you're setting flow vars:
<message-properties-transformer scope="invocation" doc:name="Set flowVars">
<add-message-property key="varName1" value="1"/>
<add-message-property key="varName2" value="2"/>
<add-message-property key="varName3" value="2"/>
</message-properties-transformer>
If you need to do small transformations, you can always call DataWeave from MEL. Here's an example:
...
<add-message-property key="varName1" value="dw('payload map $.id')"/>
...
The Transform Message Component (DataWeave)
You can also do this in DataWeave with the Transform Message component as well. I do feel it has a couple disadvantages that I should point out, though. The first is the XML is more verbose and difficult to read:
<dw:transform-message doc:name="Transform Message">
<dw:set-variable variableName="varName1"><![CDATA[%dw 1.0
%output application/java
---
1]]></dw:set-variable>
<dw:set-variable variableName="varName2"><![CDATA[%dw 1.0
%output application/java
---
2]]></dw:set-variable>
<dw:set-variable variableName="varName3"><![CDATA[%dw 1.0
%output application/java
---
3]]></dw:set-variable>
</dw:transform-message>
In addition, there is no obvious way to tell from the AP Studio UI that this particular transform message component is setting multiple variables:
If you're using enterprise edition, please use dataweave to set multiple variables in one component.
Runtime 4.1
https://docs.mulesoft.com/mule-runtime/4.1/dataweave-variables
Runtime 3.8
https://docs.mulesoft.com/mule-runtime/3.8/dataweave
I would like to know why do you need to just declare variables. Anyways if in case you just need it blank while declaring, you can simply set it as below.
<expression-component doc:name="Expression">
<![CDATA[flowVars.var1 ="" ;
flowVars.var2="";]]>
</expression-component>
Hope this help.
I want to print the mule configuration file name, in the logger in the flow, how can I get it?
Suppose the configuration file name in test.xml, inside that a flow is having logger, which prints test.xml, how can I get this?
<flow name="filenameFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/Hello" doc:name="HTTP"/>
<logger message="#[app.name.toString()]" level="INFO" doc:name="Logger"/>
</flow>
[name.flow] is not correct one.
you should go with #[flow.name] which is the correct form. Don't mislead by your answers.
Thanks,
Should print out the name of your application, in you case "test". This is not however the name of the xml file. #[flow.name] will give you the name of the flow currently executing.
Try these expressions:
1) #[message.outboundProperties['originalFileName']]
2) #[header:originalFilename]
I have done almost the same thing a few days ago.
Add a global element of type property placeholder, give location: mule-deploy.properties.
In logger, use ${config.resources}.
It will work if there is only one config file.
Just as #dlb explained, I am also wondering you may have better solution for your requirement, basically I am asuming that you want to make log more transparent, and easier to locate which flow caused any event/error.
As such, it makes more sense to log flow name rather than the config file name, which may contain multiple flows.You can utilize the catagory in log component for this purpose:
<logger level="INFO" category="${application-prefix}.myMainFlow" doc:name="Logger" message="#['payload is ---\n' + payload]"/>
In each and every log component (logs should be used in important places kind of milestones), input ${application-prefix}.flowName in catagory (property is used for reusing application's name in all logs, and flowName should be hardcoded), then you will find logs like below in runtime:
INFO 2016-09-07 17:00:27,566 [[test].HTTP_Listener_Configuration.worker.01] com.myOrg.myApp.myMainFlow: payload is ---
Hello World
#[message.outboundproperties[originalFilename]]
Try this expression.
I want to enrich my message (POJO) properties from original payload stored in flow variable
<set-variable variableName="SupplierRequest" value="#[payload]" doc:name="SupReq"/>
<flow-ref name="GetSupplierRequestDetail" doc:name="GetReqData"/>
<set-variable variableName="SupplierRequestData" value="#[payload]" doc:name="SupReqData"/>
In above code, I need couple of SupplierRequestData POJO properties to be set with properties from SupplierRequest POJO.
Do I need to write custom transformer or any other solution?
Ideally you should use the enritcher. But given that you already have the original payload in a flow variable you could just use an expression component as an expression transformer would imply a transformation from A to B while this is modification of A with B:
<expression-component><![CDATA[message.payload.propertyName = flowVars.myOrigPayload.myProp]]></expression-component>
I have seen this answer but it does not show how you use the MEL to send the file in the value field. If you enter some value in there that is the content of the file. I assume you have to move the payload from the file endpoint connector to the attachment value property using MEL.
Also how can you set the content type dynamically
Mule SMTP - send email with attachment
Thanks
Jaco.
You can use the file-to-string-transformer to transform your file to string. You can also use Mule variables, properties, etc for defining the content type or other params. Example:
<file:inbound-endpoint path="/tmp/attachments" responseTimeout="10000"/>
<file:file-to-byte-array-transformer/>
<set-variable variableName="ct" value="test/plain" />
<set-attachment attachmentName="#[message.outboundProperties.filename]" value="#[payload]" contentType="#[flowVars['ct']]"/>
<set-payload value="this is my message"/>
<smtp:outbound-endpoint...
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