Monday, February 6, 2012

Date conversion function in XSLT

Recently, i found a need to convert one format to different date formats using XSLT script..This is a sample script i wrote to convert      Thu Jun 07 18:00:00 EDT 2012 --to--> 2012-06-07T18:00:00  which may be useful to others also...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
    <xsl:template match="/">
   
        <xsl:element name="PickUpDateTime">
            <xsl:call-template name="FormatDate">
                <xsl:with-param name="DateTime" select="//date" />
            </xsl:call-template>
        </xsl:element>
   
    </xsl:template>
   
    <xsl:template name="FormatDate">
        <xsl:param name="DateTime" />   
        <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
        <xsl:variable name="day">
            <xsl:value-of select="substring($DateTime,1,3)" />
        </xsl:variable>
       
        <xsl:variable name="month-temp">
            <xsl:value-of select="substring-after($DateTime,' ')" />
        </xsl:variable>       
        <xsl:variable name="month">
            <xsl:value-of select="substring($month-temp,1,3)" />
        </xsl:variable>       
       
        <xsl:variable name="date-temp">
            <xsl:value-of select="substring-after($month-temp,' ')" />
        </xsl:variable>
        <xsl:variable name="date">
            <xsl:value-of select="substring($date-temp,1,2)" />
        </xsl:variable>       
       
        <xsl:variable name="year">
            <xsl:value-of select="substring-after($DateTime,'EDT ')" />
        </xsl:variable>           
       
        <xsl:variable name="time">
            <xsl:value-of select="substring-after($date-temp,' ')" />
        </xsl:variable>
           
        <xsl:variable name="hh">
            <xsl:value-of select="substring($time,1,2)" />
        </xsl:variable>
        <xsl:variable name="mm">
            <xsl:value-of select="substring($time,4,2)" />
        </xsl:variable>
        <xsl:variable name="ss">
            <xsl:value-of select="substring($time,7,2)" />
        </xsl:variable>

        <xsl:value-of select="$year" />

        <xsl:value-of select="'-'" />

        <xsl:choose>
            <xsl:when test="$month = 'Jan'">01</xsl:when>
            <xsl:when test="$month= 'Feb'">02</xsl:when>
            <xsl:when test="$month = 'Mar'">03</xsl:when>
            <xsl:when test="$month = 'Apr'">04</xsl:when>
            <xsl:when test="$month = 'May'">05</xsl:when>
            <xsl:when test="$month = 'Jun'">06</xsl:when>
            <xsl:when test="$month = 'Jul'">07</xsl:when>
            <xsl:when test="$month = 'Aug'">08</xsl:when>
            <xsl:when test="$month = 'Sep'">09</xsl:when>
            <xsl:when test="$month = 'Oct'">10</xsl:when>
            <xsl:when test="$month = 'Nov'">11</xsl:when>
            <xsl:when test="$month = 'Dec'">12</xsl:when>
        </xsl:choose>

        <xsl:value-of select="'-'" />
   
        <xsl:value-of select="$date" />
        <xsl:value-of select="'T'" />
        <xsl:value-of select="$hh" />
        <xsl:value-of select="':'" />
        <xsl:value-of select="$mm" />
        <xsl:value-of select="':'" />
        <xsl:value-of select="$ss" />
    </xsl:template>
</xsl:stylesheet>
 

Monday, January 2, 2012

Defining JMS proxy to listen to a particular queue in wso2esb

There are  use cases, where user needs to define number of jms queues for different purposes and proxies will listen to those queues and do further processing with messages.
There is a predefined service level parameter available in axis2   to make 'jms' proxies to listen particular queues.
Check the following simple sample(for qpid), where 'ErrorProxy" listens a  queue named as "errorqueue".

<proxy xmlns="http://ws.apache.org/ns/synapse" name="ErrorProxy" transports="https,http,jms" statistics="disable" trace="disable" startOnLoad="true">
   <target inSequence="ErrorInSequence" endpoint="ErrorQueueEndpoint" />
   <parameter name="transport.jms.ContentType">
      <rules>
         <jmsProperty>contentType</jmsProperty>
         <default>text/xml</default>
      </rules>
   </parameter>
   <parameter name="transport.jms.ConnectionFactory">myQueueConnectionFactory</parameter>
   <parameter name="transport.jms.DestinationType">queue</parameter>
   <parameter name="transport.jms.Destination">errorqueue</parameter>
</proxy> 
You have to enable jms transport in your axis2.xml.

eg:
<transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
  <parameter name="myQueueConnectionFactory" locked="false">
   <parameter name="java.naming.factory.initial" locked="false"> org.apache.qpid.jndi.PropertiesFileInitialContextFactory</parameter>
   <parameter name="java.naming.provider.url" locked="false">repository/conf /jndi.properties</parameter>
   <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false"> QueueConnectionFactory</parameter>
   <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue< /parameter>
  </parameter>

Need to add following entry at jndi.properties file
  queue.errorqueue = example.errorqueue


ErrorInSequence Configuration
--------------------------------------
<sequence xmlns="http://ws.apache.org/ns/synapse" name="ErrorInSequence">
   <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" />
   <property name="OUT_ONLY" value="true" scope="default"/>
     <log level="custom">
      <property name="Picked message" value="************" />
   </log>
   <description></description>
</sequence> 

This sequence receives the error message,which is stored at error queue