Showing posts with label ESB. Show all posts
Showing posts with label ESB. Show all posts

Wednesday, October 26, 2016

How to Calculate Time Difference Between Request and Response in WSO2 ESB

If you want to calculate the time difference between request and response, we can use a script mediator.
The Script Mediator is used to script with languages such as JavaScript, Groovy, or Ruby.

First, you have to get request timestamp using a property mediator. For that, add below line inside "inSequence".
    
   <property name = "REQUEST_TIMESTAMP" expression = "get-property('SYSTEM_TIME')"/>  
   
Then, add below line inside "outSequence"  to get response timestamp.
    
   <property name = "RESPONSE_TIMESTAMP" expression = "get-property('SYSTEM_TIME')"/>  
   

Now, you can calculate response time using below code. (Script mediator)
    
   <script language = "js">  
      var requestTimeStamp = mc.getProperty("REQUEST_TIMESTAMP");  
      var responseTimeStamp = mc.getProperty("RESPONSE_TIMESTAMP");  
      var responseTime = responseTimeStamp - requestTimeStamp;  
      mc.setProperty( "RESPONSE_TIME", responseTime);  
   </script>  
   

A sample proxy with a script mediator to calculate response time is in below.
    
 <?xml version = "1.0" encoding = "UTF-8"?>   
  <proxy xmlns = "http://ws.apache.org/ns/synapse"   
    name = "CalculatingTimeDifference"   
   transports = "https,http"  
     statistics = "disable"  
     trace = "disable"  
     startOnLoad = "true">  
   <target>   
   <inSequence>   
      <property name = "REQUEST_TIMESTAMP" expression = "get-property('SYSTEM_TIME')"/>   
   </inSequence>   
   <outSequence> <sequence xmlns = "http://ws.apache.org/ns/synapse" name = "responseMessage">  
     <property name = "RESPONSE_TIMESTAMP" expression = "get-property('SYSTEM_TIME')"/>  
        <script language = "js">  
          var requestTimeStamp = mc.getProperty("REQUEST_TIMESTAMP");  
          var responseTimeStamp = mc.getProperty("RESPONSE_TIMESTAMP");  
          var responseTime = responseTimeStamp - requestTimeStamp;  
          mc.setProperty("RESPONSE_TIME", responseTime);  
      </script>  
      <log level = "custom">   
          <property name = "Response Time(ms)" expression = "$ctx:RESPONSE_TIME"/>  
          </log>  
    </outSequence>  
    </target>   
    <description/>   
  </proxy>   
   


You can see a log message like below with the response time.

[2016-10-03 09:41:43,531]  INFO - LogMediator API Response Time(ms) = 624.0

Thursday, September 8, 2016

How to Receive Emails to WSO2 ESB



  1. Uncomment below line in <ESB_HOME>/repository/conf/axis2/axis2.xml/axis2.xml to enable Email transport listener.
  2.    
     <transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener">  
       
    
  3. Restart WSO2 ESB if has already started.
  4. Log in to Management console and add below proxy. 
  5. Here, proxy transport type is mailto. The mailto transport supports sending messages (E-Mail) over SMTP and receiving messages over POP3 or IMAP.
       
     <?xml version="1.0" encoding="UTF-8"?>  
     <proxy xmlns="http://ws.apache.org/ns/synapse"  
         name="EmailReciever"  
         startOnLoad="true"  
         statistics="disable"  
         trace="disable"  
         transports="mailto">  
       <target>  
        <inSequence>  
          <log level="custom">  
           <property expression="$trp:Subject" name="Subject"/>  
          </log>  
          <drop/>  
        </inSequence>  
        <outSequence>  
          <send/>  
        </outSequence>  
       </target>  
       <parameter name="transport.PollInterval">5</parameter>  
       <parameter name="mail.pop3.host">pop.gmail.com</parameter>  
       <parameter name="mail.pop3.password">wso2pass</parameter>  
       <parameter name="mail.pop3.user">wso2user</parameter>  
       <parameter name="mail.pop3.socketFactory.port">995</parameter>  
       <parameter name="transport.mail.ContentType">text/plain</parameter>  
       <parameter name="mail.pop3.port">995</parameter>  
       <parameter name="mail.pop3.socketFactory.fallback">false</parameter>  
       <parameter name="transport.mail.Address">wso2user@gmail.com</parameter>  
       <parameter name="transport.mail.Protocol">pop3</parameter>  
       <parameter name="mail.pop3.socketFactory.class">javax.net.ssl.SSLSocketFactory</parameter>  
       <description/>  
     </proxy>  
       
    
  6. Send email to wso2user@gmail.com. You can see the email recieving to ESB from the logs.
Note : If you are using gmail to receive emails, you have to allow external apps access in your google account as mention in here.

Wednesday, September 7, 2016

[WSO2 ESB]How to Aggregate 2 XMLs Based on a Element Value Using XSLT Mediator

The XSLT Mediator applies a specified XSLT transformation to a selected element of the current message payload.

Suppose you are getting 2 XML responses from 2 service endpoints like below.

summary.xml
   
 <?xml version="1.0" encoding="UTF-8"?>  
 <policyList xmlns="http://insurance.org/policyservice">  
   <policy>  
    <policyType>Life</policyType>  
    <holderName>Ann Frank</holderName>  
    <policyId>SWXU9124</policyId>  
    <premium>100000</premium>  
   </policy>  
   <policy>  
    <policyType>Life</policyType>  
    <holderName>Shane Watson</holderName>  
    <policyId>SWXABS4</policyId>  
    <premium>70000</premium>  
   </policy>  
 </policyList>  
   

detail.xml
   
 <?xml version="1.0" encoding="UTF-8"?>  
 <policyList xmlns="http://insurance.org/policyservice">  
   <policy>  
    <policyType>Life</policyType>  
    <policyId>SWXABS4</policyId>  
    <validityPeriod>3Years</validityPeriod>  
    <startedDate>10/10/2014</startedDate>  
    <claims>  
      <claim>  
       <id>2016AAASI900</id>  
       <date>01/06/2016</date>  
       <claimAmount>1400</claimAmount>  
      </claim>  
    </claims>  
   </policy>  
   <policy>  
    <policyType>Life</policyType>  
    <policyId>SWXU9124</policyId>  
    <validityPeriod>1Years</validityPeriod>  
    <startedDate>01/01/2016</startedDate>  
    <claims>  
      <claim>  
       <id>2016TABSI981</id>  
       <date>15/02/2016</date>  
       <claimAmount>9900</claimAmount>  
      </claim>  
    </claims>  
   </policy>  
 </policyList>  
   


Above two responses are dynamic responses. So, above 2 XMLs should be aggregated to one xml before applying XSLT mediator. For that you can use PayloadFactory Mediator, and get aggregated XML like below.
   
 <?xml version="1.0" encoding="UTF-8"?>  
 <policyList xmlns="http://insurance.org/policyservice">  
   <summary>  
    <policy>  
      <policyType>Life</policyType>  
      <holderName>Ann Frank</holderName>  
      <policyId>SWXU9124</policyId>  
      <premium>100000</premium>  
    </policy>  
    <policy>  
      <policyType>Life</policyType>  
      <holderName>Shane Watson</holderName>  
      <policyId>SWXABS4</policyId>  
      <premium>70000</premium>  
    </policy>  
   </summary>  
   <details>  
    <policy>  
      <policyType>Life</policyType>  
      <policyId>SWXABS4</policyId>  
      <validityPeriod>3Years</validityPeriod>  
      <startedDate>10/10/2014</startedDate>  
      <claims>  
       <claim>  
         <id>2016AAASI900</id>  
         <date>01/06/2016</date>  
         <claimAmount>1400</claimAmount>  
       </claim>  
      </claims>  
    </policy>  
    <policy>  
      <policyType>Life</policyType>  
      <policyId>SWXU9124</policyId>  
      <validityPeriod>1Years</validityPeriod>  
      <startedDate>01/01/2016</startedDate>  
      <claims>  
       <claim>  
         <id>2016TABSI981</id>  
         <date>15/02/2016</date>  
         <claimAmount>9900</claimAmount>  
       </claim>  
      </claims>  
    </policy>  
   </details>  
 </policyList>  
   

Assume you want to get the response as follows.
   
 <?xml version="1.0" encoding="UTF-8"?>  
 <ns:policyList xmlns:ns="http://insurance.org/policyservice">  
   <policy xmlns="http://insurance.org/policyservice">  
    <policyType>Life</policyType>  
    <holderName>Ann Frank</holderName>  
    <policyId>SWXU9124</policyId>  
    <premium>100000</premium>  
    <claims>  
      <claim>  
       <id>2016TABSI981</id>  
       <date>15/02/2016</date>  
       <claimAmount>9900</claimAmount>  
      </claim>  
    </claims>  
    <startedDate>01/01/2016</startedDate>  
    <validityPeriod>1Years</validityPeriod>  
   </policy>  
   <policy xmlns="http://insurance.org/policyservice">  
    <policyType>Life</policyType>  
    <holderName>Shane Watson</holderName>  
    <policyId>SWXABS4</policyId>  
    <premium>70000</premium>  
    <claims>  
      <claim>  
       <id>2016AAASI900</id>  
       <date>01/06/2016</date>  
       <claimAmount>1400</claimAmount>  
      </claim>  
    </claims>  
    <startedDate>10/10/2014</startedDate>  
    <validityPeriod>3Years</validityPeriod>  
   </policy>  
 </ns:policyList>  
   

You can use the XSLT mediator to above transformation. Use below XSL file with XSLT mediator. Upload following XSL file into registry.

merger.xslt
   
 <?xml version="1.0" encoding="UTF-8"?>  
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sn="http://insurance.org/policyservice" version="1.0">  
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />  
   <xsl:strip-space elements="*" />  
   <xsl:key name="policy-by-id" match="sn:details/sn:policy" use="sn:policyId" />  
   <!-- identity transform -->  
   <xsl:template match="@*|node()">  
    <xsl:copy>  
      <xsl:apply-templates select="@*|node()" />  
    </xsl:copy>  
   </xsl:template>  
   <xsl:template match="/sn:policyList">  
    <xsl:copy>  
      <xsl:apply-templates select="sn:summary/sn:policy" />  
    </xsl:copy>  
   </xsl:template>  
   <xsl:template match="sn:policy">  
    <xsl:copy>  
      <xsl:apply-templates />  
      <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:claims" />  
      <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:startedDate" />  
      <xsl:copy-of select="key('policy-by-id', sn:policyId)/sn:validityPeriod" />  
    </xsl:copy>  
   </xsl:template>  
 </xsl:stylesheet>  
   

Define XSLT mediator like this in your sequence
   
  <xslt key="gov:/xslt/merger.xslt"/>