Thursday, December 20, 2012

Google Analytics Tracking for WSO2 API Manager

WSO2 API Manager is a platform for creating, managing, consuming and monitoring APIs. It employs proven SOA best practices to solve a wide range of API management challenges such as API provisioning, API governance, API security and API monitoring.

You can download API Manager latest version from:

and find the online documentation from:

With WSO2 API Manager 1.3.0 release you can track your API invocations through Google Analytics!

First let me explain you what is google analytics. Google analytics is a service offered by Google which tracks vists to a web site and generates detailed statistics based on those visits.
Now WSO2 API Manager latest 1.3.0 release fascilitates you to integrate with Google Analytics to track run time statistics for API invocations.

Here is the step by step guide to do this.

1. Setup a Google analytics account.

If you already have a google account, Sign-In. Else Create a New Google Account and Sign-In.

Then go to and Sign-In

Click on the Admin Icon on top right

Then Click Create New Account as highlighted in below image.

Now select 'Web Site' for what would you like to Track and give information about the account as follows. Please note that since we are actually not tracking a site, give any site URL for 'Web Site URL'.

Now Click Get 'Tracking ID' bottom of the page.

Accept the Google Analytics Terms of Service and you will directed to a page with Tracking information.

Now you have sucessfully created your Tracking account and you will need the obtained Tracking-ID for further steps.

2. Enable Google Analytics Tracking in API Manager.

Open API_MANAGER_HOME/repository/conf/api-manager.xml. Locate the and set Enabled to true and enter the Tracking-ID obtained in as follows.

Save the changes to API_MANAGER_HOME/repository/conf/api-manager.xml and restart your API Manager running instance.

Now we have successfully integrated WSO2 API Manager with Google Analytics. Now lets see how we can see the statistics.

3. View Google Analytics statistics.

Real Time Statistics

First create and publish an API. Then through the API store subscribe to that API.

Now go to Google Analytics [] and select the above created 'APISTORESTAT' account from the list of Accounts.

On the appearing page go to 'Home' tab.

Now you will see 'Real-Time' icon in left menu. Click on that and select 'Overview'.

Then invoke/call the above created API through embedded RESTClient or Curl.

Now you will be able to see a hit on 'PageViews' Per second graph and also 'Right Now' users as 1.

Reporting Statistics

Google analytics reporting statistics population takes more than 24hours from the invocation. Therefore to view the statistics on the Dashboard you need to wait!

Here is a sample Dashboard with populated statistics.

Here as you can see there are Widgets with statistics related to Audience, Traffic, Page Content, Visit Duration etc. You can add any widgets for your preference through 'Add Widget'.

Thursday, May 17, 2012

How to enable remote access to mysql database server

Enable remote access to mySQL database server

1. Open the MySQL server configuration file my.cnf.

sudo gedit /etc/mysql/my.cnf

2. Change the bind-address value to your MySQL server ip. If the MySQL server IP=, change it as follows, save and close my.cnf file.


3. Now Grant access to remote IP.
If the remote IP= 

Grant permission to a specific database (ex: testbd)

GRANT ALL ON testdb.* TO testuser@'' IDENTIFIED BY 'test123';

Grant all permissions


4. Open port 3306, if firewalls are enabled. Following is a sample iptables rule to open Linux iptables firewall.

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

5. logout of mysql.

6. Connect from remote host

mysql -u root -h -p

Monday, March 19, 2012

Smooks Entity Persistence Frameworks - Hibernate

With the new Smooks Persistence cartridge in Smooks 1.2, we can directly use several entity persistence frameworks from within Smooks (Hibernate, JPA etc). So in this blog post let's try out the Hibernate sample specified in here.
Since smooks have given the basic configurations only, this blog post is going to assist the peolple who need full implementation.

What we are going to do is process the following XML message and store the product information to a bean and persist the order information.



First we have to Specify our Entity classes (Order, OrderLine, Product)
//Order class
package example.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

public class Order {
    private Integer ordernumber;
    private String customerId;
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private List orderItems = new ArrayList();
    public void addOrderLine(OrderLine orderLine) {
    public Integer getOrdernumber() {
 return ordernumber;

   public void setOrdernumber(Integer ordernumber) {
 this.ordernumber = ordernumber;

   public String getCustomerId() {
 return customerId;

   public void setCustomerId(String customerId) {
 this.customerId = customerId;

   public List getOrderItems() {
 return orderItems;

   public void setOrderItems(List orderItems) {
 this.orderItems = orderItems;

//OrderLine class 
package example.entity;

import javax.persistence.*;

@Table(name = "orderlines")
public class OrderLine {

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Integer id;

 @JoinColumn(name = "orderid")
 private Order order;
 private Integer quantity;

 @JoinColumn(name = "productid")
 private Product product;

 public Integer getId() {
  return id;

 public void setId(Integer id) { = id;

 public Order getOrder() {
  return order;

 public void setOrder(Order order) {
  this.order = order;

 public Integer getQuantity() {
  return quantity;

 public void setQuantity(Integer quantity) {
  this.quantity = quantity;

 public Product getProduct() {
  return product;

 public void setProduct(Product product) {
  this.product = product;

//Product Class
package example.entity;

import javax.persistence.*;

@Table(name = "products")
public class Product {
    private Integer id;
    private String name;

 public Integer getId() {
  return id;

 public void setId(Integer id) { = id;

 public String getName() {
  return name;

 public void setName(String name) { = name;

Here is the Smooks configuration to bind xml message to java beans and persist the order entity. 

        from Product p where = :id
So now you would wonder where we store the datasource configuration. If we just use Hibernate we can configure datasource information inside hibernate.cfg.xml

The database represented in the connection must have 4 tables.

customers (id int(11), name varchar(30))
products (id int(11), name varchar(30))
orders (ordernumber int(11), customerId varchar(30))
orderlines (id int(11), quantity int(11), orderid int(11), productid int(11))

Insert the following sample data to customers and procucts tables.

insert into customers (id, name) values (123456, 'Devin Snow');
insert into customers (id, name) values (789101, 'Homer Simpson');
insert into customers (id, name) values (999999, 'John Doe');

insert into products  (id, name) values (11, 'Cheese cake');
insert into products  (id, name) values (22, 'Chocolate 300gr');
insert into products  (id, name) values (33, 'Beer');
insert into products  (id, name) values (44, 'Smooks in Action');
insert into products  (id, name) values (55, 'Ultimate guide to Smooks');

Let's execute Smooks now. 

package example.main;


import javax.xml.transform.Source;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.milyn.persistence.util.PersistenceUtil;
import org.milyn.scribe.adapter.hibernate.SessionRegister;
import org.milyn.scribe.register.DaoRegister;
import org.xml.sax.SAXException;

public class Main {

 public static void main(String args[]) throws IOException, SAXException {
  Smooks smooks = new Smooks("smooks-hbt-config.xml");

  ExecutionContext executionContext = smooks.createExecutionContext();

  SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  DaoRegister register = new SessionRegister(session);
  // This sets the DAO Register in the executionContext for Smooks //to access it. 
  Transaction transaction = session.beginTransaction();
  Source source = new StreamSource(new File("input.xml"));
  smooks.filterSource(executionContext, source);


Wednesday, February 22, 2012

A way to transform message content in between the client and the backend server - WSO2 Payload Factory Mediator

WSO2 ESB version - 4.0.3

WSO2 PayLoad Factory Mediator is for transforming or replacing message content in between the client and the backend server. Incomming message from client can be transformed by the Payload Factory mediator into the format expected by the service. Then the response message can be formatted into which is expected by the client.

Each argument in the mediator configuration could be a static value or an XPath expression. When an expression is used, argument value is fetched at runtime by evaluating the provided XPath expression against the existing SOAP message. Also the format of the request or response message can be configured which is mapped with the arguments provided in order.

For instance in the following the values for format parameters Code & Price will be assigned with values evaluated from args given in the specified order.


A sample service of WSO2 Payload Factory Mediator can be found from here. The sample mediates the incomming message, send to the server and response is sent back to the client.  Inside 'In' mediator, PayloadFactory mediator transforms the message. The response message is transformed inside 'Out' mediator.

We can use PayloadFactory mediator in WSO2 ESB proxy services as well. In the inSequence we can transform the request message, and in outSequence we can transform the response message. Here the publishWSDL must be specified with the WSDL which is visible to the user.

Imagine that there is a web service endpoint which has the getQuote operation with 'symbol' parameter. But client who invokes the service see that there is a getQuote operation with 'Code' parameter. So request message will be sent as,

But server expects a message as,

The PayloadFactory mediator does this transformation as follows. 

Similarly the web service will send the response in the format of,

But the client expects the response message as,


So this response message transformation happend on the outSequennce.