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"/>  
   


Monday, September 5, 2016

[WSO2 App Manager] How to Add a Custom Radio Button Field to a Webapp

In WSO2 App Manager, when you create a new web app, you have to fill a set of predefined values. If you want to add any custom fields to an app, you can easily do it.

Suppose you want to add custom radio button field to webapp create page. Say the custom radio button field name is "App Category".

First, Let's see how to add a custom field to UI (Jaggery APIs).
  1. Modify <APPM_HOME>/repository/resources/rxt/webapp.rxt.
  2.    
       <field type="text" required="true">  
         <name>App Category</name>  
       </field>  
       
    
    Note : If you don't want add the custom field as mandatory, required="true" part is not necessary.
  3. Login to Management console and navigate to Home > Extensions > Configure > Artifact Types and delete "webapp.rxt"
  4. Add below code snippet to the required place of <APPM_HOME>repository/deployment/server/jageeryapps/publisher/themes/appm/partials/add-asset.hbs
  5.    
       <div class="form-group">  
         <label class="control-label col-sm-2">App Category: </label>  
         <div class="col-sm-10">  
           <div class="radio">  
             <label>  
               <input type="radio" data-value="free" class="appCategoryRadio" name="appCategoryRadio">  
               Free  
             </label>  
           </div>  
           <div class="radio">  
             <label>  
               <input type="radio" data-value="premium" class="appCategoryRadio" name="appCategoryRadio">  
                 Premium  
              </label>  
            </div>  
          </div>  
       </div>  
       
       <input type="hidden" class="col-lg-6 col-sm-12 col-xs-12" name="overview_appCategory"  
               id="overview_appCategory">  
       
    
  6. Add below code snippet to <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/partials/edit-asset.hbs.
  7.    
       <div class="form-group">  
         <label class="control-label col-sm-2">App Category: </label>  
         <div class="col-sm-10">  
           <div class="radio">  
             <label>  
               <input type="radio" data-value="free" class="appCategoryRadio" name="appCategoryRadio">  
                 Free  
             </label>  
           </div>  
           <div class="radio">  
             <label>  
               <input type="radio" data-value="premium" class="appCategoryRadio" name="appCategoryRadio">  
                 Premium  
             </label>  
           </div>  
         </div>  
       </div>  
        
       <input type="hidden"  
             value="{{{snoop "fields(name=overview_appCategory).value" data}}}"  
             class="col-lg-6 col-sm-12 col-xs-12" name="overview_appCategory"  
             id="overview_appCategory">  
       
    
  8. To save selected radio button value in registry, you need to add below function inside $(document ).ready(function() {} of <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/js/resource-add.js
  9.    
       $(".appCategoryRadio").click(function(){  
         var output = [];  
         $( ".appCategoryRadio" ).each(function( index ) {  
           var categoryValue = $(this).data('value');  
           if( $(this).is(':checked')){  
             output.push(categoryValue);  
           }  
         });  
         $('#overview_appCategory').val(output);  
       });  
       
    
  10. To preview the selected radio button value in app edit page, add below code snippet inside $(document ).ready(function() {} of  <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/js/resource-edit.js.
  11.    
       $(".appCategoryRadio").click(function(){  
         var output = [];  
         $( ".appCategoryRadio" ).each(function( index ) {  
           var categoryValue = $(this).data('value');  
           if( $(this).is(':checked')){  
             output.push(categoryValue);  
           }  
         });  
         $('#overview_appCategory').val(output);  
       
       
       });  
       
       var appCategoryValue = $('#overview_appCategory').val().split(',');  
       $( ".appCategoryRadio" ).each(function( index ) {  
         var value = $(this).data('value');  
         if($.inArray(value, appCategoryValue) >= 0){  
           $(this).prop('checked', true);  
         }  
       });  
       
    
  12. When you create a new version of an existing webapp, to copy the selected radio button value to the new version, add below line to
    <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/partials/copy-app.hbs
  13.    
       <input type='text' value="{{{snoop "fields(name=overview_appCategory).value" data}}}" name="overview_appCategory" id="overview_appCategory"/>  
       
    
    Now, Let's see how to add customized fields to the REST APIs.
  14. Go to Main -> Browse -> in Management console and navigate to   /_system/governance/appmgt/applicationdata/custom-property-definitions/webapp.json and click on "Edit As Text". Add the custom fields which you want to add.
  15.    
       {  
         "customPropertyDefinitions":  
           [  
             {"name":"overview_appCategory"}  
           ]  
       }  
       
    
  16. Restart App Manager.
  17. Web app create page with the newly added radio button will be shown as below. save image

[WSO2 App Manager] How to Add a Custom Checkbox to a Webapp

WSO2 App Manager supports users to customize the server as they need. Here, I'm going to talk about how to add a custom checkbox to webapp create page.

First, Let's see how to add a custom checkbox field to UI (Jaggery APIs).

For example,  let's 
take the checkbox attribute name as “Free App”.
  1. Modify <APPM_HOME>/repository/resources/rxt/webapp.rxt.
  2.    
       <field type="text">  
         <name label="Free App">Free App</name>  
       </field>  
       
    
  3. Login to Management console and navigate to Home > Extensions > Configure > Artifact Types and delete "webapp.rxt"
  4. Add below code snippet to the required place of <APPM_HOME>repository/deployment/server/jageeryapps/publisher/themes/appm/partials/add-asset.hbs
  5.    
       <div class="form-group">  
         <label class="control-label col-sm-2">Free App: </label>  
           <div class="col-sm-10 checkbox-div">  
             <input type="checkbox" class="freeApp_checkbox">  
           </div>  
       </div>  
       
       <input type="hidden" required="" value="FALSE" class="col-lg-6 col-sm-12 col-xs-12" name="overview_freeApp" id="overview_freeApp">  
       
    
  6. Add below code snippet to <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/partials/edit-asset.hbs as well.
  7.    
       <div class="form-group">  
         <label class="control-label col-sm-2">Free App: </label>  
         <div class="col-sm-10 checkbox-div">  
           <input type="checkbox" class="freeApp_checkbox" value="{{{snoop "fields(name=overview_freeApp).value" data}}}">  
         </div>  
       </div>  
       
       <input type="hidden" required="" value="{{{snoop "fields(name=overview_freeApp).value" data}}}"  
        class="col-lg-6 col-sm-12 col-xs-12" name="overview_freeApp" id="overview_freeApp">  
       
    
  8. To save newly added value in registry, add below function inside   $(document).ready(function(){} of <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/js/resource-add.js
  9.    
       $(".freeApp_checkbox").click(function(){  
         var output = [];  
         $( ".freeApp_checkbox" ).each(function( index ) {  
           if( $(this).is(':checked')){  
             output.push("TRUE");  
           } else {  
             output.push("FALSE");  
          }  
         });  
         $('#overview_freeApp').val(output);  
       });  
       
    
  10. To preview check box value in app edit page, add below code snippet inside $( document ).ready(function() {} of  <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/js/resource-edit.js.
  11.    
       var freeAppValue = $('#overview_freeApp').val();  
       $(".freeApp_checkbox").each(function (index) {  
         if (freeAppValue == "TRUE") {  
           $(this).prop('checked', true);  
         } else {  
           $(this).prop('checked', false);  
         }  
       });  
       
       $(".freeApp_checkbox").click(function(){  
         var output = [];  
         $( ".freeApp_checkbox" ).each(function( index ) {  
           if( $(this).is(':checked')){  
             output.push("TRUE");  
           } else {  
             output.push("FALSE");  
           }  
         });  
         
         $('#overview_freeApp').val(output);  
       });  
       
    
  12.  When you create a new version of an existing webapp, to copy the checkbox value to the new version, add below line to
    <APPM_HOME>/repository/deployment/server/jaggeryapps/publisher/themes/appm/partials/copy-app.hbs
  13.    
       <input type='text' value="{{{snoop "fields(name=overview_freeApp).value" data}}}" name="overview_freeApp" id="overview_freeApp"/>  
       
    
    Now, Let's see how to add customized fields to the REST APIs.
  14. Go to Main -> Browse -> in Management console and navigate to   /_system/governance/appmgt/applicationdata/custom-property-definitions/webapp.json and click on "Edit As Text". Add the custom fields which you want to add.
  15.    
       {  
          "customPropertyDefinitions":  
           [  
             {"name":"overview_freeApp"}  
           ]  
       }  
       
    
  16. Restart App Manager.
  17. Web app create page with the newly added checkbox will be shown as below.save image