Thursday, 14 April 2016



Call an RMP REST Web Service from a Xamarin mobile app

This entry shows how to call a RESTful Fujitsu RunMyProcess (RMP) Web Service from a mobile Xamarin App.

Intro

First we will create a datastore within RMP and then create a web service to retrieve data from it. The Second part shows how to create the Xamarin app and how to access the web service.   The datastore (called a Collection within RMP) will contain contact information such as name and email address.   The Xamarin app will call the Web Service using a name and retrieve the email address from RMP and display it . 

RMP Web Service and Collection

Create a project

From the RMP ide select new project.   I have called the project 'Demo Rest Call' and selected the entity that will have rights to edit this project.   I have selected the 'Fujitsu' group that was automatically created to match the name of the company I registered during the RMP registration process. 


Create Collection

Create a new collection by selecting new and then Collection within the project just created.


Call the new collection 'Contacts', make the collection public by ticking box and then save it by clicking the icon of the disk. 


Create Test Data 

Normally you would create a web interface to allow entry of data into the collection, but for the purposes of this example we will create a Composite API that will directly add a row into the collection.  So to start create a new Composite API from the project new menu.  Call it 'capi - Create Test Data' and select the same entity as before, in my case its called 'Fujitsu'. Create the process flow as follows.


The test data will be created by inserting a new document into the test collection via the process flow.  Open the output variables tab of the process and then select the script icon to allow entry of freemarker code. 


Next enter the following freemarker code into the editor.  This will insert one document into the test collection that we will retrieve via the mobile app. select the test button and get confirmation from the output window the document has been added.


Create the RMP Web Service

Create another Composite API from with the new project and call it 'capi - Get Contact', selecting your company as the entity again.  The web service takes a name as an input, searches for a contact with the same name in the 'Contacts' collection and then returns the email address associated with contact.




Create process as follows in the process designer view of the new composite API.





Next we need to add an output variable to the start node.  This will retrieve the name of the contact we need to search for from the input parameters. add an name variable and value ${name} to retrieve the name from the incoming JSON request.




We now need to add code to search the collection for the name and then return the email address associated to the contact.   Open the properties tab of the 'Get Contact' process and create a new attribute called email.




Then with in the script of the new attribute add the following freemarker to retrieve the email

<#assign query >
  {"name": "${name}"}
</#assign >
<#assign contact = load_object(query,"Contacts")>
${contact.email}

The created web service address can be retrieved from configuration tab of the Composite API.
for example the address of mine is:

https://live.runmyprocess.com/live/xxxxxxxxxxx/host/141905/service/225679?P_version=${P_version}&P_mode=TEST

I have replaced part of it with x's so you cant break mine and set the mode to test mode by setting the P_mode to TEST.

The web service is secured by Basic Auth and so your username and password for RMP needs to be added to the header of the request calling the web service also the Content-Type will be 'application/json' in our case.  The request body for our example will look like the following JSON.

{ "name" : "Ben" }


Xamarin Android App


Here we create a Xamarin Android app that retrieves an email address using a name that is entered.  This app issues a request to the RMP REST service.  



In our example app we send the name in a HTTP request, the resulting email address information is returned in JSON format. 
First we create a Xamarin Android app, the example screenshots in this article were taken using the Android Developer Preview. 



The view can be created using the Designer view, first replace the contents of the Resources/layout/Main.axml with the xml example below.   The view contains an EditText field to allow entry of a name, a button used to initiate the web service call and get the email and then a finally a TextView to display the retrieved email address. 




Next we need to wire up the Activity to the view.  Within the OnCreate method we connect the name and email fields from the view and add a click event to call the web service. In the MainActivity subclass add the following code.



The FetchEmailAsync method creates the request JSON, adds the basic auth header, posts the request and then parses the response.   The username and password will need to match the one you have setup within RMP.  



The source for the Xamarin project can be found in github


Sunday, 14 December 2014

Rails and RabbitMQ RPC

Integrate Ruby on Rails with RabbitMQ RPC

This post shows how to implement RabbitMQ RPC using Ruby on Rails. 
I have implemented the RabbitMQ Request-Response pattern. This is the standard request/response pattern used by messaging systems. The client sends a request and then blocks until a response is received. The client creates a callback queue for the response and matches the response using a correlation id. An RPC worker written in Ruby is waiting for requests and acts as the server.



This is my implementation of the RabbitMQ tutorial shown at www.rabbitmq.com ruby tutorial six.  I have used the bunny gem to connect to rabbitMQ.  My project has been uploaded to GitHub at RailsRabbit. Most of the client and server source code can be found in rabbitmixin.rb.

I added the following code to my create method in my controller to access RabbitMQ using bunny and my rabbitmixin.  The controller creates a connection, channel, client and sends a message, waits for a response and then closes the channel and connection.

The client creates a uuid to be used as a correlation id, a lock to wait for the response and sends the message.  When the message is sent the reply queue is set to wait for the response and then returned to the calling controller.

The server waits for a message, reads the message, reverses it and then sends it back on the reply queue set by the client and also sets the correlation id.


The following ruby script is used to start the server process and waits for requests.

The ruby server is started using command:
$ ruby -rubygems rpc_server.rb

I found it really easy to get Rails integrated to RabbitMQ and running.  I am impressed with how quick I got the app working, much better than some messaging systems I have used in the past.



Friday, 7 November 2014

How to setup a Rails Apache Passenger Ubuntu Server

This post describes how I set-up a Rails environment on Ubuntu. I used Passenger and Apache to serve the application. It details all the commands and configuration changes I did to get it working.

During the installation I used the following application versions

Ubuntu 14.04
Ruby 2.1.4
Rails 4.1.7
Passenger 4.0.53

Step 1 - install ubuntu


For the first step we need to get an OS ready to use for the installation.  I chose to use Ubuntu 14.04 LTS installed on an Amazon EC2 instance.

Step 2 -  update ubuntu


Log onto the server and download the latest package lists and repositories using the following command.


sudo apt-get update


Step 3 - create the application user


create a new user to be used to install the application

sudo adduser httpuser

Use the following command to set the password

sudo passwd httpuser

allow the new user to sudo

sudo visudo

 add the following lines to the file and save

httpuser ALL=(ALL) ALL


Step 4 - install ubuntu dependencies for Ruby


sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev nodejs python-software-properties


Step  5 - install Apache 2


This command will install Apache 2.4

sudo apt-get install apache2


Step 6 - install rvm, Ruby and rails


I choose to use rvm to manage Ruby. The follow method installs it as root.

\curl -sSL https://get.rvm.io | sudo bash -s stable

Note: I got an error when i initially did this command, stating the public key could not be found. I did the suggested command and then reran the command to install rvm.

source the rvm scripts by entering

source "/usr/local/rvm/scripts/rvm"

Login as root and install Ruby using rvm.

rvm install 2.1.4

Change the default version and test the correct version of Ruby is intalled.

rvm use 2.1.4 --default
ruby -v

Update the RubyGems

gem update --system

Install the rails gem

gem install rails


Step 7 - create a Rails test app to be used by Passenger


Login as the application user httpuser.

sudo mkdir /var/www/demoapp

Change the permissions to allow application user to write to directory

cd /var/www/demoapp
sudo chmod 777 .

Create a new rails app in the current directory /var/www/demoapp using the following command

rails new . 

Create a welcome page for the application

rails generate controller welcome index

Amend routes.rb to create a root to the new page
I uncomment root welcome#index


Step 8 - install Passenger


Install the passenger gem

gem install passenger

run the passenger apache2 install script

passenger-install-apache2-module

Note: during the install the script notice a few pre-requisite packages were missing so I installed them.

sudo apt-get install apache2-threaded-dev
sudo apt-get install libapr1-dev
sudo apt-get install libaprutil1-dev

Create a Passenger load configution for Apache

sudo vi /etc/apache2/mods-available/passenger.load

Add the following lines to the file and save it.

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.1.4/gems/passenger-4.0.53/buildout/apache2/mod_passenger.so

Create passenger conf file for Apache

sudo vi /etc/apache2/mods-available/passenger.conf

Add the following lines to file and save it.

<IfModule mod_passenger.c>
  PassengerRoot /usr/local/rvm/gems/ruby-2.1.4/gems/passenger-4.0.53
  PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.1.4/wrappers/ruby
</IfModule>

Enable the new apache passenger module

sudo a2enmod passenger


Step 9 - Configure Apache


Create the apache application virtual host config

sudo vi /etc/apache2/sites-available/demoapp.conf

and add the following  to the conf file

<VirtualHost *:80>
   ServerName localhost
   # !!! Be sure to point DocumentRoot to 'public'!
   DocumentRoot /var/www/demoapp/public       
   <Directory /var/www/demoapp/public>
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
      # Uncomment this if you're on Apache >= 2.4:
      Require all granted
   </Directory>
</VirtualHost>

Add secret to environment variables to enable the application to work.  Login as httpuser and perfotm the following.

cd /var/www/demoapp
rake secret

Edit the apache2 environment file.

sudo vi /etc/apache2/envvars

Add the following to the file:

export SECRET_KEY_BASE=xxxxxxx

where xxxxxx is the secret key returned from the rake secret command
restart apache to enable the new configuration

sudo service apache2 restart

Step 10 - Test the app


browse to http://localhost/welcome/index to view your app!