If you want to write a CSV file from a proxy service in WSO2 ESB, then following sample would help you to understand how it works.
- Uncomment the following lines in
wso2esb-4.x.x/repository/conf/axis2/axis2.xml
.<transportreceiver class="org.apache.synapse.transport.vfs.VFSTransportListener" name="vfs"/>
<transportsender class="org.apache.synapse.transport.vfs.VFSTransportSender" name="vfs"/>
- Add below content into the resource called
/_system/governance/xml2csv.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="iso-8859-1"/> <xsl:strip-space elements="*" /> <xsl:template match="/*/child::*"> <xsl:for-each select="child::*"> <xsl:if test="position() != last()"><xsl:value-of select="normalize-space(.)"/>,</xsl:if> <xsl:if test="position() = last()"><xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text></xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
- Create a proxy service named CSVProxy with the following content.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CSVProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/> <payloadFactory> <format> <data> <row> <firstName>Ruchira</firstName> <lastName>Wageesha</lastName> </row> <row> <month>May</month> <date>31</date> <year>1984</year> </row> <row> <street>Flower Road</street> <province>Western</province> <city>Colombo</city> <country>Sri Lanka</country> <postalCode>0007</postalCode> </row> </data> </format> </payloadFactory> <xslt key="gov:/xml2csv.xslt"/> <property name="transport.vfs.ReplyFileName" expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.csv')" scope="transport"/> <property name="OUT_ONLY" value="true"/> <send> <endpoint> <address uri="vfs:file:///home/ruchira/csv"/> </endpoint> </send> <drop/> </inSequence> </target> </proxy>
Note :vfs:file:///home/ruchira/csv
is the path where generated csv files are stored. Hence replace it with a valid path in your machine. - Invoke the CSVProxy service by issuing an HTTP GET to
http://localhost:8280/services/CSVProxy
- CSV files will be created in the above specified path.