Friday, April 12, 2013

Creating CSV Files from a Proxy Service

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.

  1. 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"/>
  2. 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>
    
  3. 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.
  4. Invoke the CSVProxy service by issuing an HTTP GET to http://localhost:8280/services/CSVProxy
  5. CSV files will be created in the above specified path.

3 comments:

  1. Thanks for posting this. Exactly i was looking for.
    Chanaka

    ReplyDelete
  2. I have been trying to work on but this only works on a fixed number of addresses. I have some accounts that have more proxy addresses than others. What would be the best way of extracting these into a csv file. I need these in one column, proxyAddresses, with each address separated by a semicolon.
    Youtube Proxy

    ReplyDelete
  3. Thanks a lot for making this available. I really appreciate that.
    access Bee MP3 in UK

    ReplyDelete