Tuesday, February 10, 2015

Using xslt mediator to update file transported with VFS

There can be situations where you need to update xml files when mediating through an ESB. Wso2 ESB supports this ,with the use of xslt mediator where you can add your own xslt definition and perform the transformations.

In this example, I will be looking at a sample where you need to transform a file located in your local file system. First of all, you need to download a fresh binary pack of ESB( http://wso2.com/products/enterprise-service-bus/).  
First of all, we need to enable vfs transport in esb. After unzipping the downloded esb pack following configurations need to be made .
In <ESB_HOME>/repository/conf/axis2/axis2.xml uncomment below lines. 



<transportreceiver name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportListener"/>
...
<transportSender name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportSender"/>

Next step is to write your xslt mediator as below.  
<definitions xmlns="http://ws.apache.org/ns/synapse">
<registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
<parameter name="root">file:./repository/samples/resources/</parameter>
<parameter name="cachableDuration">15000</parameter>
</registry>
<localEntry key="xslt-key-req" src="file:repository/samples/resources/update.xslt"/>
<proxy name="FileProxy" transports="vfs" startOnLoad="true" trace="disable">
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.PollInterval">15</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///Users/himasha/Desktop/original</parameter>
<parameter name="transport.vfs.FileURI">file:///Users/himasha/Desktop/in</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///Users/himasha/Desktop/failure</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.xml</parameter>
<parameter name="transport.vfs.ContentType">application/xml</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>

<target>
<inSequence>
<log level="custom">
<property name="sequence" value="fileProxy"/>
</log>

<xslt key="xslt-key-req"/>

</inSequence>
</target>

</proxy>

</definitions> 

Here the xslt transformation file is retrieved as a registry resource. As above configuration you need to place your xslt file in repository/samples/resources. According to transport.vfs parameter values, if the process is successful then the input.xml file will be moved to the location given in MoveAfterProcess. If it fails, according to MoveAfterFailure value it will be moved to a folder named failure. (Note that you need to create these folders beforehand). Information and location about the file to be transformed is given in parameters FileUri,FileNamePattern,ContentType. Note that here since my input file is a .xml the contentType and FileNamePattern values are added to match that. 

When running this sample, when the in sequence is getting executed you may see a property log as added in the custom log. Also if u need to store the transformed file back to local file system you need to add an endpoint as below. 
<endpoint>
    <address uri="vfs:file:///home/user/test/out"/>
</endpoint>
 

No comments:

Post a Comment