Thursday, November 10, 2016

Google Analytics Tracking for WSO2 App Manager

Google Analytics is a free Web analytics service that provides statistics and basic analytical tools. We can configure WSO2 App Manager to track web application and sites invocation statistics through Google Analytics.

First, let's see how to setup a google Analytic account.

1. Go to http://www.google.com/analytics/ and Click on Analytic tab.
2. Then, click on "Admin" tab and create a New Account.
3. Click on "Website" and give account information like in below. Here, you have to fill the information which you want to track.
4. Click on 'Get Tracking Id'. Then you will redirect to a page like below. From there you can get the Tracking Id.
5.  Configure WSO2 App Manager with received Tracking code. Enable google analytics and add TrackingID of <APPM_HOME>/repository/conf/app-manager.xml as shown below.
   
  <GoogleAnalyticsTracking>  
 <!--Enable/Disable Google Analytics Tracking-->  
      <Enabled>true</Enabled>  
      <!--Google Analytics Tracking ID-->  
      <TrackingID>UA-86711225-1</TrackingID>  
 </GoogleAnalyticsTracking>  
   
6. Restart server.
7. Place the below JavaScript code snippet into the pages that you need to track with your Google Analytics account.
   
 <script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript">  
   function invokeStatistics(){      
   var tracking_code = ["<TRACKING_CODE>"];      
   var request = $.ajax({      
     url: "http://<AM_GATEWAY_URL>:8280/statistics/",      
     type: "GET",      
     headers: {        
       "trackingCode":tracking_code,      
       }         
     }  
   );    
 }  
 </script>  
   

Now we have successfully integrated WSO2 App Manager with Google Analytics. Let's see how we can see the statistics.

Real-time statistics


Then go to Google Analytics [http://www.google.com/analytics/] and select the created account above.

The following image shows an invocation of a specific Web application. The Google Analytics graphs and statistics are displayed at runtime in its Real-Time view. In below graphs, you will able to see a hit on "PageViews" per second and active users.


Reporting statistics

Google Analytics reporting statistics will take more than 24 hours from the time of invocation.

A sample dashboard with populated statistics is shown in below.


Monday, November 7, 2016

[WSO2 App Manager] How to Add a Custom Dropdown 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 a custom dropdown field to webapp create page. Say the custom dropdown field name is "App Network Type". 

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">  
           <name label = "App Network Type">App Network Type</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">App Network Type : </label>  
       <div class = "col-sm-10">  
         <select id = "appNetworkType" class = "col-lg-6 col-sm-12 col-xs-12">  
           <option value = "None">None</option>  
           <option value = "Online">Online</option>  
           <option value = "Offline">Offline</option>  
         </select>  
       </div>  
       <input type = "hidden" required = "" class = "col-lg-6 col-sm-12 col-xs-12" name = "overview_appNetworkType"  
           id = "overview_appNetworkType">  
     </div>  
       
    
  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 Network Type : </label>  
       <div class = "col-sm-10">  
         <select id = "appNetworkType" class = "col-lg-6 col-sm-12 col-xs-12">  
           <option value = "None">None</option>  
           <option value = "Online">Online</option>  
           <option value = "Offline">Offline</option>  
         </select>  
       </div>  
       
       <input type='hidden' value="{{{snoop "fields(name=overview_appNetworkType).value" data}}}"  
       name="overview_appNetworkType" id="overview_appNetworkType"/>  
     </div>  
       
    
  8. To save selected 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.    
     $("#appNetworkType").change(function() {  
       var selectedNetworkType = $('#appNetworkType').find(":selected").text();  
       $('#overview_appNetworkType').val(selectedNetworkType);  
     });  
       
    
  10. To preview the selected dropdown field 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 selectedNetworkType = $('#overview_appNetworkType').val();  
     $( "#appNetworkType" ).each(function( index ) {  
       $(this).val(selectedNetworkType);  
     });  
       
     $("#appNetworkType").change(function() {  
       var selectedNetworkType = $('#appNetworkType').find(":selected").text();  
       $('#overview_appNetworkType').val(selectedNetworkType);  
     });  
       
    
  12. When you create a new version of an existing webapp, to copy the selected dropdown 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_appNetworkType).value" data}}}" name = "overview_appNetworkType" id = "overview_appNetworkType"/>   
       
    

    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_appNetworkType"}   
         ]   
      }   
       
    
  16. Restart App Manager.
  17. Web app create page with the newly added dropdown field will be shown as below. 

Wednesday, November 2, 2016

[WSO2 App Manager] How to Disable App Types

WSO2 App Manager facilitates creating, publishing, and managing Webapps, Sites, mobile applications.  By default, these three app types are enabled in WSO2 App Manager. Enabled app types has mentioned under <EnabledAssetTypeList> in <AppM_Home>/repository/conf/app-manager.xml
   
   <EnabledAssetTypeList>  
     <Type>webapp</Type>  
     <Type>mobileapp</Type>  
     <Type>site</Type>  
   </EnabledAssetTypeList>  
   

If you want to disable any app type from App Manager, you can easily do it. You just have to remove unwanted app types from above configuration and restart the server.

If you disable mobileapp from App Manager, Publisher shows as below.

Store will show like below.



If you want remove "Site" from App Manager, you have to do two additional steps. Because Webapps and Sites are using same creating and editing pages in Publisher.
You have not to allow users to create "Sites" from Publisher. For that, you have to remove the relevant div(as shown in below image) from Publisher UI.




Comment below code block of
<AppM_Home>/deployment/server/jaggeryapps/publisher/themes/appm/partialspublisher/themes/appm/partials/add-asset.hbs

   
   <div class = "form-group" type = 'hidden'>  
     <label class = "control-label col-sm-2">Treat as a Site: </label>  
     <div class = "col-sm-10 checkbox-div">  
       <input type = "checkbox" class = "treatAsASite_checkbox">  
      </div>  
   </div>  
   
   
Comment below code block of
<AppM_Home>/deployment/server/jaggeryapps/publisher/themes/appm/partialspublisher/themes/appm/partials/edit-asset.hbs

   
   <div class = "form-group" type = "hidden">  
     <label class = "control-label col-sm-2">Treat as a Site: </label>  
     <div class = "col-sm-10 checkbox-div">  
      <label>  
        <input type = "checkbox" class = "treatAsASite_checkbox"  
          value = "{{{snoop "fields(name=overview_treatAsASite).value" data}}}">  
       </label>  
     </div>  
   




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

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.