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.