Here I will be discussing on how to transfer a set of files defined in a document, to another location using a proxy in WSO2 ESB 4.8.1. Suppose the names of the files are defined in the file-names.xml file. Suppose the file has the following xml structure.
<files>
<files-set name="files-set-1">
<file name="img1">image1.png</file>
<file name="img2">image2.png</file>
</files-set>
<files-set name="files-set-2">
<file name="img3">image6.png</file>
<file name="img4">image4.png</file>
<file name="img5">image5.png</file>
</files-set>
</files>
The procedure i'm going to follow is, read the file names defined in the file-names.xml using vfs, and transfer the files using the file connector. First we need to enable the vfs in the proxy. Please refer [1] on how to enable vfs transport to a proxy. Once vfs is enabled, the proxy should look like follows.<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="FileProxy" transports="vfs" statistics="disable" trace="disable" startOnLoad="true"> <target> </target> <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter> <parameter name="transport.PollInterval">15</parameter> <parameter name="transport.vfs.MoveAfterProcess">file:///some/path/success/</parameter> <parameter name="transport.vfs.FileURI">file:///some/path/in/</parameter> <parameter name="transport.vfs.MoveAfterFailure">file:///some/path/failure</parameter> <parameter name="transport.vfs.FileNamePattern">.*.xml</parameter> <parameter name="transport.vfs.ContentType">application/xml</parameter> <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter> <description /> </proxy>Here "FileURI" is the directory location of the file-names.xml. "MoveAfterProcess" is the directory where the file-names.xml should be moved after successfully reading. "MoveAfterFailure" is the location where the file-names.xml file should be moved if some failure occurs during reading it.
Now we need to extract the file names defined in this file-names.xml. For that i'm going to iterate through the file names using iterator mediator [2].
<iterate preservePayload="true" attachPath="//files/files-set" expression="//files/files-set/file" > <target> <sequence> <property name="image" expression="//files/files-set/file"/> </sequence> </target> </iterate>Finally, move the files using the file-connector[3] , as follows.
<property name="fileName" expression="//files/files-set/file"/> <property name="fileLocation" value="ftp://some/path/in/"/> <property name="newFileLocation" value="ftp://some/path/success/"/> <fileconnector.move> <filelocation>{$ctx:fileLocation}</filelocation> <file>{$ctx:fileName}</file> <newfilelocation>{$ctx:newFileLocation}</newfilelocation> </fileconnector.move>
Here, "fileLocation" refers to the directory in which the files we need to move are located, and "newFileLocation" refers to the directory to which the files should be moved. You can use either a localfile system location as well as ftp location for both these properties.
References:
[1] https://docs.wso2.com/display/ESB481/VFS+Transport#VFSTransport-Enablingthetransport[2] https://docs.wso2.com/display/ESB481/Iterate+Mediator
[3] https://docs.wso2.com/display/ESBCONNECTORS/File+Connector
Wrote by Supun Setunga