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.

Thursday, April 11, 2013

Sending JSON Responses from a Proxy Service in WSO2 ESB

If you have come up with the requirement of sending JSON responses from a proxy service, then following proxy configuration can be used straightaway to try that out.

i.e. Create a proxy service named JSONProxy with the following content. Then, you can try that out by issuing HTTP GET to http://localhost:8280/services/JSONProxy

Proxy Configuration
<proxy xmlns="http://ws.apache.org/ns/synapse" name="JSONProxy" transports="https,http" 
       statistics="disable" trace="disable" startOnLoad="true"> 
   <target> 
      <inSequence> 
         <header name="To" action="remove"/> 
         <property name="RESPONSE" value="true"/> 
         <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/> 
         <payloadFactory> 
            <format> 
               <person> 
                  <fullName> 
                     <firstName>Ruchira</firstName> 
                     <lastName>Wageesha</lastName> 
                  </fullName> 
                  <birthDate> 
                     <month>May</month> 
                     <date>31</date> 
                     <year>1984</year> 
                  </birthDate> 
                  <address> 
                     <street>Flower Road</street> 
                     <province>Western</province> 
                     <city>Colombo</city> 
                     <country>Sri Lanka</country> 
                     <postalCode>0007</postalCode> 
                  </address> 
               </person> 
            </format> 
         </payloadFactory> 
         <property name="messageType" value="application/json" scope="axis2"/> 
         <send/> 
      </inSequence> 
   </target> 
   <description></description> 
</proxy>
JSON Response
{
    "person": {
        "fullName": {
            "firstName": "Ruchira",
            "lastName": "Wageesha"
        },
        "birthDate": {
            "month": "May",
            "date": "31",
            "year": "1984"
        },
        "address": {
            "street": "Flower Road",
            "province": "Western",
            "city": "Colombo",
            "country": "Sri Lanka",
            "postalCode": "0007"
        }
    }
}

Wednesday, April 3, 2013

Error Handling in Jaggery

Proper error handling is a key points in any application. If you are using WSO2 Jaggery, then you might have also come up with the same requirement.

Jaggery is JavaScript, hence try, catch and finally blocks can be used within your code as you usual. Also,  passed JavaScript Error object can be used to get a brief information about the error. But, as we are dealing with a server side environment, we might need to know additional details about the errors, such as stacktrace, line numbers etc.

Using the additional properties/methods which can be found in the passed Error object, error details can be accessed as below.
var log = new Log();

try {
    parse(null); //code where exception is thrown
} catch (e) {
    log.error(e.lineNumber); // Error occured line number
    log.error(e.fileName); // Error occured script name
    log.error(e.stack); // Executed JavaScript file stack
    log.error(e.message); // JavaScript error message

    log.error(e.rhinoException); // Rhino exception object
    log.error(e.javaException); // Java excepton object

    log.error(e); // Logs the stack trace

    print(e); // prints the stack trace
} finally {
    log.info('finally :)');
}