In WSO2 ESB, when you want to write the current message to a file, you usually use the VFS transport. This means calling an Address Endpoint and using a VFS URI containing the location of the file to write the message to. Unfortunately, WSO2 ESB does not support using XPath expressions in endpoints or endpoint templates.
I had a similar question before. It was slightly different in that it required only the filename to be dynamic. For this there is the “transport.vfs.ReplyFileName” transport property. When you put the filename in this property the call to the VFS endpoint will use that property to determine the name of the file that will be written:
<property name="transport.vfs.ReplyFileName”    expression="$ctx:some-property-containing-the-filename"    scope="transport"/> |
It’s easy to be fooled by the name of this property. “ReplyFileName” suggests that it has something to do with the response. But it is actually used generically to determine the name of the file when the VFS Transport Sender is used. So this works for “Send” and “Call” mediators that use a VFS endpoint.
Back to our initial problem…
The above example show that it is easy to make the filename dynamic. You can use any XPath expression to build the ReplyFileName-property. However, how to do the same for the directory where the file should be placed?
In my sequence the Call step looked something like:
<call> Â Â Â <endpoint> Â Â Â Â Â Â Â <address uri="vfs:file:///home/user/somedir"/> Â Â Â </endpoint> </call> |
Â
The “uri” attribute in the endpoint is static. You cannot use XPath expressions. Using an Endpoint Template also does not help. So what to do to make this directory dynamic?
After browsing the source code (one of the many benefits of WSO2 being an Open Source project!), I discovered:
When the property “transport.vfs.ReplyFileName” contains an absolute local file path then it completely overrides the URI that is defined in the Address Endpoint.
There we go! The solution to the problem: just use a full file path in the property:
<property name=”filename” value=”somefile.txt”/> <property name="transport.vfs.ReplyFileName”    expression="concat($ctx:path,$ctx:filename)"    scope="transport"/> |
Nice, how a property can have it’s secrets!
Note that this solution works for local paths. I did not try if and how this works for other types of file-systems like SFTP.
If you have any questions about this blogpost contact us via the comments section of this blog. View also our WSO2 Tutorials, webinars or white papers for more technical information. Need support? We do deliver WSO2 Product Support, WSO2 Development Support, WSO2 Operational Support and WSO2 Training Programs.