Friday, June 13, 2014

Introduction to Play-Framework modules

Play framework inherently support modularization. In other words we can develop play modules and reuse them in different play applications. This article provides a good guide on how to do that. But that is little bit outdated for latest Play distributions. I will describe the changes which is needed to be done to create a play module on latest play versions.
Playframework no longer has a play console. Instead of that it uses Typesafe activator. So you need to download activator and add activator to your environment path.
You can create boilerplate code for a play app using the  template called Just play java. In my case I needed to create a authentication module. So I created a Play action called auth in controllers package. Then to publish the module go the project directory and issue clean command. Then issue publish-local command. If it is successful you will get a output like this.

[info] published ivy to /home/prabhath/.ivy2/local/authmodule/authmodule_2.10/1.0-SNAPSHOT/ivys/ivy.xml
[success] Total time: 15 s, completed Jun 13, 2014 10:49:13 AM

Then create a new application which will use the previously created auth module. To add the dependency to new project update the build.sbt file. Content of my build.sbt file is like below.

name := """Test"""

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaCore,  // The core Java API
  "junit" % "junit" % "4.8.2",


Next you have to tell the path of your local repository to the application. To do that edit plugins.sbt file in /<your project>/project directory. I added the following line to plugins.sbt file.

resolvers += "Local Play Repository" at "file://home/prabhathp/.ivy2/local/"

Now build the project and you will be able to use the classes in your module/controllers package inside classes in your controllers. Note that you do not need to add imports when reusing module components.

Tuesday, April 22, 2014

Add a attachment to CouchDB with play framework JavaAPI

I tired to write a rest API which act as a mediator between clients and CouchDB API. This was done using Playframework's Java API. I wrote the following controller method which takes a POST request which contains file name, content-type and file to be stored as parameters and send as a PUT request to CouchDB.

   public static Result putAttachment(){
        Http.MultipartFormData body = request().body().asMultipartFormData();
        Http.MultipartFormData.FilePart picture = body.getFile("picture");
        String fileName=null;
        String contentType=null;
        File file=null;
        if (picture != null) {
            fileName = picture.getFilename();
            contentType = picture.getContentType();
            file = picture.getFile();
        } else {
            //TODO implement
        String restServiceUrl = ""+fileName;
        F.Promise<play.libs.WS.Response> future =play.libs.WS.url(restServiceUrl)
        return Results.ok(future.get(50000000).getBody());

Friday, April 18, 2014

Fixing Error "Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)" in Virtualbox

Recently I  to installed Oracle Virtualbox on my Ubuntu 12.04 computer to run Hortanworks sandbox. When I am trying to start the Sanbox it gave me an error like this.

Unknown error creating VM (VERR_SUPLIB_WORLD_WRITABLE).

What it says is /usr/bin forlder is world writable. Usually this is not world writable. But somehow I have unintentionally changed the permissions. I fixed this using the following command.

chmod o-w /usr/bin

Friday, February 21, 2014

Integrating an actor system with Play framework (A distributed message classifier with and Play framework)

Recently we developed a distributed message classifier. This can process rapid burst of text (email, twitter feeds, etc) and get results.

To implement the processing part we used which is an event based distributed framework. Currently to analyse messages we use a web service. But any other processing mechanism (local or remote) can be easily plugged to this.
After implementing the core we needed to publish messages to the applications using REST. To do that we integrated it with Play Framework. Also I developed an admin panel using MVC features provided by it.
While integrating the existing actor system with play framework several conflicts occurred. One reason for this was Play framwork internally uses an actor system too. So I had to do several tweaks and change some configurations. I am not going to discuss each of them here because it will be too lengthy. This is the URL to our git-hub repo You can fork it and see those by your self. It has a very descriptive user guide and wiki which explains from architecture to UI. Hope this will be useful to starters who are trying to get familiarized with and Play framework.

Thursday, November 14, 2013

Enable access from other hosts to a MySQL server

Recently I deployed a MySQL server. But there was a problem that I could not access the data base from a application which was hosted in another server. The reason for this is by default MySQL server does not accepts requests from other hosts except the localhost.
To solve this problem we have to do two tasks.

  • Create a user which has permissions to read and write to a database from a different host
ON database.*
TO ‘user’@'yourremotehost'
IDENTIFIED BY 'newpassword';

As an exmaple following query enables root user to access all the databases from any host.


  • Bind the ip adress
To do this you have to edit the MySQL configuration file. In Ubuntu this is /etc/mysql/my.cnf
There you can find an entry like
bind-address = 

If you want to enable access from all the host just remove it. If you want to limit the hosts you can add entries like
bind-address = your ip

After doing that you have to restart the MySQL server. In Ubuntu you can do that like below.
sudo /etc/init.d/mysql restart

Saturday, November 2, 2013

Install Oracle Java Development Kit on Ubuntu

Installing Java SDK is very straight forward in Windows. Download Java installer double click it and follow the steps. But installing Java in Ubuntu is not easy like that. In Ubuntu we can easily install Open JDK by providing following command. 

sudo apt-get install openjdk-6-jdk

 But this installs Open JDK not Oracle JDK. This is not a problem for entry level Java developers. But if you work in a production environment you cannot use Open JDK since most products use Oracle Java. Until recently the method I followed to install Oracle Java was downloading the tar.gz file form Oracle site and install it using the terminal. But this is very time consuming and there are lot of configurations has to be followed. But here is a very easy way to install the latest Oracle JDK in you machine without not needing to do any additional configurations. Just issue following three commands in the terminal to install Oracle JDK in your Ubuntu machine.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update 
sudo apt-get install oracle-java7-installer

Monday, July 15, 2013

How to connect to WSO2 BAM with NodeJs using REST

Recently I wanted to use WSO2 BAM REST API to with NodeJs. After playing sometime with NodeJs API I was able to POST a stream definition to BAM using NodeJs with following method.

var https = require('https');

var auth = "Basic " + new Buffer('admin:admin').toString("base64"); //You  need to replace admin:admin with your username and password

// prepare the header
    var postheaders = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        "Authorization": auth

// the post options
    var optionspost = {
        host: '',
        port: '9443',
        path: '/datareceiver/1.0.0/streams/',
        method: 'POST',
        rejectUnauthorized: 'false',
        headers: postheaders


    // do the POST call
    var reqPost = https.request(optionspost, function(res) {
        console.log("statusCode: ", res.statusCode);
        // uncomment it for header details
//  console.log("headers: ", res.headers);

        res.on('data', function(d) {
  'POST result:\n');
  '\n\nPOST completed');

    // write the json data
    reqPost.on('error', function(e) {

An important ramark is you need to put rejectUnauthorized: 'false' if you are using NodeJs default Cas. Otherwise you may get an error saying [Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE]. This is because NodeJs rejects the certificate provided by BAM in default configuration. This tweak can be applied to any situation where you get the [Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE] when try to send a https request using NodeJs.