Sunday, October 23, 2016

How To Add new Users, Roles and Tenants to WSO2 Automation Test Framework.

If we want to add a new user or new role or tenant we should update automation.xml accordingly.

a. How to add new Role
Add any role with a name and a key inside the <roles> tag of <userManagement>. You have to list the permissions of each role inside the <permissions> tag.
   
 <roles>  
   <role name = "AdminRole" key = "AdminRole">  
     <permissions>  
       <permission>/permission/admin</permission>  
     </permissions>  
   </role>  
   <role name = "SubscribeRole" key = "SubscribeRole">  
     <permissions>  
       <permission>/permission/admin/login</permission>  
       <permission>/permission/admin/manage/webapp/subscribe</permission>  
     </permissions>  
   </role>  
 </roles>  
   

b. How to add new users to super tenant
You can add any user with a key inside the <tenant> tag under the <superTenant> tag of
<userManagement> tag.
   
 <superTenant>  
   <tenant domain = "carbon.super" key = "superTenant">  
     <admin>  
       <user key = "superAdmin">  
         <userName>admin</userName>  
         <password>admin</password>  
       </user>  
     </admin>  
     <users>  
       <user key = "testuser1">  
         <userName>testuser1</userName>  
         <password>testuser1</password>  
       </user>  
     </users>  
   </tenant>  
 </superTenant>  
   

c. How to assign roles to users
If you want to assign  roles to the user, there are 2 ways.

1. Get Role from the automation.xml (Like we defined in step a) Then add the role key inside the user tag.
   
 <user key = "testuser1">  
   <userName>testuser1</userName>  
   <password>testuser1</password>  
   <roles>  
     <role>SubscribeRole</role>  
   </roles>  
 </user>  
   

2. You can see existing roles from management console as well. Then set role name under the <user> tag like this.
   
 <user key = "AppCreator">  
   <userName>appcreator</userName>  
   <password>appcreatorpass</password>  
   <roles>  
     <role>Internal/creator</role>  
   </roles>  
 </user>  
   

d. Add new tenants
You can add any tenant with a domain name and key inside the <tenants> tag of <userManagement> tag. Inside that tag, you can add admin user information, user information as below.
   
 <tenants>  
   <tenant domain = "wso2.com" key="wso2">  
     <admin>  
       <user key = "admin">  
         <username>admin</username>  
         <password>admin</password>  
       </user>  
     </admin>  
     <users>  
       <user key = "myuser">  
         <username>mytestuser</username>  
         <password>mytestuserpass</password>  
       </user>  
     </users>  
   </tenant>  
 </tenants>  
   

Wednesday, October 19, 2016

Start Multiple WSO2 IoTS Instances on the Same Computer

If you want to run multiple WSO2 IoTS on the same machine, you have to change the default ports with an offset value to avoid port conflicts. The default HTTP and HTTPS ports (without offset) of a WSO2 product are 9763 and 9443 respectively.

Here are the steps to offset ports. Let's assume you want to increase all ports by 1.

  1. Set Offset value to 1 in <IoTS_HOME>/repository/conf/carbon.xml
  2.    
        <Offset>1</Offset>  
       
    
  3. Change the hostURL under <authenticator class="org.wso2.carbon.andes.authentication.andes.OAuth2BasedMQTTAuthenticator"> in <IoTS_HOME>/repository/conf/broker.xml according to the port offset.
  4.    
        <authenticator class = "org.wso2.carbon.andes.authentication.andes.OAuth2BasedMQTTAuthenticator">  
         <property name = "hostURL">https://<IoTS_HOST>:<IoTS_PORT>/services/OAuth2TokenValidationService</property>  
          <property name = "username">admin</property>  
          <property name = "password">admin</property>  
          <property name = "maxConnectionsPerHost">10</property>  
          <property name = "maxTotalConnections">150</property>  
       </authenticator>  
       
       
    
  5. Start the Server.

Wednesday, October 12, 2016

[WSO2 App Manager]Registry Extension (RXT) Files

All data related to any application you create in WSO2 App Manager is stored in the registry which is embedded to the server. Those data is stored in a format which is defined in a special set of files called “Registry Extensions (RXTs)”[1] . When you save a web application, the format it is saved in the registry is given in “webapp.rxt”, and that of mobile applications is given in “mobileapp.rxt”. You can see these files in the file system under <APPM_HOME>/repository/resources/rxts folder. When you want to add a new field to an application you create, you need to edit these RXT files.

These RXT files can also be found in Home > Extensions > Configure > Artifact Types in App Manager management console like below.


But when you want to edit these file, it is better to edit them from the file system, because every time a new tenant is created, it picks relevant RXTs from the file system to populate the data in the registry.

App Manager reads RXT files from the file system and populates them in the management console only if they are not already populated. So, whenever you edit RXTs from the file system, you have to delete the rxt files from the management console and restart the server to populate updated RXT files in the management console. If you have multiple tenants, you need to delete RXT files of each tenant from the management console.

There are “field” tags in every RXT file. In each field tag, it contains field type and if it is a required field or not. See below two examples.

eg :
<field type = "text" required = "true">
 <name>AppId</name>
</field>

<field type = "text-area">
 <name>Terms and Conditions</name>
</field>

In rxt files, there are two types of fields. They are, “text” and “text-area”. “text” is used for text fields, and “text-area” is used for large text contents.  If you want to set the type of a field as double, Integer etc., you have to use a “text” field and have a type validation in application code.

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