Thursday, April 9, 2015

The Simple Life...

Earlier today.. I got the chance to offer a lift to a distant uncle of mine, on my way to work. He is a simple man with few embellishments, a past government servant and now a farmer by profession.

We talked.. about what we Sri Lankans usually talk about when met with a relative : which cousin is doing what, how crazy the political situation in the country is and the rising cost of living.. amongst other things. Lastly though just as he was about to get off.. we started talking about his profession : cultivating of paddy fields.

He was talking about how difficult it has become to find good help.. and the increasingly decreasing amount of cows around. Being an old school man, with over 4 decades of experience growing rice crops, he was all skeptical about these miracle seeds that promised tons and tons of output from a couple of Acres of paddy. I couldn't help but notice the look of pride with a whiff of sadness that clouded his face when he tried to remember how things were a couple of decades back when most of land from the little town of Yatawara, where he recides to Wattegama was covered in Acres and Acres of Paddy Fields. This brought back a torrent of memories of times past, the chocolaty mud of the freshly ploughed rice fields, how once our Land Cruiser got stuck in one.. how we used to go walking in the afternoons to the Yatawara temple situated right in the midst of a cluster of golden crops, all ripe and ready for harvest. Good times indeed... and times I've come to miss.

After dropping him off at the bus stand.. while driving to work, I was thinking how drastically different my life is, from that of my uncle's. Twice a year, he would spend barely 20,000 rupees (About 150 US dollars) to plant an acre of rice crops that would keep him and his family fed for whole year, he doesn't own or need a car to get him places... he take a bus and has been doing so, all his life. He doesn't own a fancy flat screen TV, or a posh stereo.. cos he simply doesn't need em.
In stark contrast here I am contemplating the purchase of a 2000 dollar DSLR, and worried how shabby my 4x4 looks compared to that of my friend's without an off road bumper up front.. I could not help but feel like an idiot. Something was wrong. Terribly wrong. I need to get my priorities straight. So does most of us, really!

Reality today, is that we go and buy the things that we don't need. We yearn to do things that we don't need to do.. We are caught up in this frenzy trying to become hip. become cool. become more social. but why??? is it because we are a bunch of unhappy bastards on a pursuit of happiness.. unknowingly, unwittingly searching for something that makes our lives make sense..?? or is it because we are all just a bunch of bored fools, wasting our lives one day at a time looking for distractions to keep us occupied. I am yet to figure out the answer to this question. Let me know if you have... :)


Thursday, February 6, 2014

Mediators : How to use Enrich Mediator to download an image from a URL and use Synapse functions to base64 encode same using WSO2 ESB

We can do this by doing a "GET" on the image URL, this will make the ESB download the contents, after which you may use the Enrich mediator to assign the "body" contents, to a synapse variable.



 
  
 


 
 


 


That should do it. We are not done however, when you run this config you will notice a huge error on the ESB console, like below.

ERROR - DeferredMessageBuilder Error building message using POX Builder
javax.xml.stream.XMLStreamException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

This is because ESB is trying to build the image as a XML, since we have not assigned a specific builder to tackle with the its type, which is : image/jpg. We can make this config change in the axis2.xml. (Please refer [1] for further details on this)

<messageBuilder contentType="image/jpeg" class="org.wso2.carbon.relay.BinaryRelayBuilder"/>

<messageFormatter contentType="image/jpeg" class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

You need to add the formatter under the <messageFormatters> section and builder under the <messageBuilders>.

Ok. We are good to go. Now all that remains to be done is the base64 encoding part. We can simply do this by calling the synapse function base64Encode

The following is an example on how it can be done.

<property name="encoded_image" expression="base64Encode($ctx:image_val)" scope="default" type="STRING" description="base64 encode Image"/>

Thats it.


References :

[1] - http://wso2.com/library/articles/binary-relay-efficient-way-pass-both-xml-non-xml-content-through-apache-synapse/

Wednesday, February 5, 2014

Mediators : How to use WSO2 Script Mediator to do date validations.

The WSO2 ESB provides a whole suite of mediators with which you can do all sorts of request/response/variable manipulations and validations.

This article will be the first of a series of posts to follow on various mediators.

For the sake of this article, we will be using javascript inside the script mediator to do a simple XML DATE validation.



The above code would convert a XML DATE which is of the following format "YYYY-MM-DDTHH:MM:SS" (E.g. : 2010-08-01T00:00:00) to a regular Javascript date, add 5 years to the same and see if current date is greater than said value. Accordingly it would assign a variable to synapse messagecontext, which you may access later as you would any synapse property. (Please refer [1] for additional methods, you may call on the Synapse MessageContext from the script mediator.)

You may use a filter/switch mediator later on in your flow to execute appropriate logic based on the "result" variable you assigned from inside the script.

The script also has logic on how we can do xpath extractions from javascript itself.

var endDay = mc.getPayloadXML()..*::Date.toString(); 

This will seek out an xml elements like <Date>2010-08-01T00:00:00</Date> or JSON elements like
{"Date" : "2010-08-01T00:00:00"}. The principal to remember here is that WSO2 ESB converts JSON requests/responses to XML format in it the run time. So with the above line of code, you may access both XML and JSON payloads, like you would any XPATH.

Note that you may either embed the script inline (like above) or either store it in the registry as a file and point like

<script language="js" key="gov:trunk/scripts/test.js" function="validateDate"/>

References :

Monday, August 26, 2013

Bruno, the Bear!

Nope. You were not the brightest one in the litter. You were not even the biggest. Nor the most dominant. Yeah, that was your sister, remember seeing her trying to chew off your little ear. There was something about you. The most adorable little cub I had ever seen, you were like a little bear, with them big furry paws, little teeth and barely open eyes. So we get you home, give you some milk soaked bread (which you don't bother to eat much), and place you on the floor.. and what do you do?? you lazy old bum! you spread all four leg and start napping like a baby.. still remember that day like yesterday, even after 7 years had come to pass.

Bruno Gunner of Silverfern, that was your given name, don't think you ever gave a damn about that, I bet you remember all them trash names I used to call you with.. Balla, Buchi, Baba, Modaya, Haraka, Thalapatha, Rotiya.. and most of the time.. Putha.. Ring any Bells?

As time came to pass you grew in to a HULK of a dog, for most people you were a HUMONGOUS dog. Weighing at more than 70 Kg, you were quite a handful and hands down the biggest Rottweiler I've ever known. You were always a gentle giant though, the innocent, yet jealous as heck type. You loved everyone! And I mean everyone, People would pea in their pants when you got near, noo.. that is not because you were fierce.. thats just because you were tremendous. Maaan! Food was your passion... you greedy Pig! You were the king of munch town. :) :) and exactly the right fit for our family. Bruno Yatawara. Haha!

We had our ups and downs, I still yield the scars from all those battles we had, all those times you tried to chew off my hand, remember I even got vaccinated once after you bit my arm (was it the arm, I don't remember) I miss all them fights, I miss that fierceness, both of us... we were two peas off the same pod. We liked it rough... the violence we exhibited our anger flaring... that was a fundamental part of our relationship.. you would growl at me, and I would growl at you, 5 minutes we would fighting.. and in another 5 minutes, you would be lying on the ground with all 4 paws up, looking at me with that weird grin in your face, tongue sticking out, while I rubbed your tummy... those were the days my friend. Those were the days.

I don't think anybody would understand what we had, and I don't expect them to.. I'm writing this because I was not able to be at your side, in your dying hour. This is the speech that I always wanted you to hear from me. And this is the speech I never got to give you. Rest in peace my Son!. May you attain a better life, a better family that would give you all the fried fish and corned mutton, and crunchy chewy stuff you liked all so much. Farewell Boy! Hope we meet again, in this life or the next.






Sunday, August 11, 2013

Driving Fast

Why do people drive fast? Well.. If we leave out get away situations and emergencies.. People do it generally for the thrill they get out of it, or because they want to prove something, or simply because they are just inconsiderate morons looking for trouble.

Then again this this post is not about other people. It's about why I do it. So here goes.


Its about the RUSH. The same rush a junkee would get from sniffing coke. 

Its an ecstatic feeling. Makes you feel nothing else in the world matters. All your worries, troubles hopes, dreams, commitments disappear for that brief moment. Your senses on full alert. Adrenaline rushing through your system. Because you know, you are taking the vehicle to the absolute limit, and no matter what safety precaution your car came packed with, if you make a mistake it's going to end badly, either for you or for someone on the road. Life hangs in the balance. The beauty of the situation is that, in this chaos you find clarity, absolute focus. It's so pristine and otherworldly that you just lose yourself in the indulgence, and give in; For, that brief moment in time, You are alive and free.




All that being said, going fast and reckless on public roads is just plain wrong.

Sunday, August 4, 2013

How to configure WSO2 Api Manager with PostgreSQL


Most Enterprise level application developers tend to use PostgreSQL over MySql as their open source Database of choice, due to it's close compliance with SQL standards, compared to the former. What this means is, if at some point the application owner chooses to go ahead with a closed-source solution such as Oracle for its database, the swap can be accomplished with much of a hassle.

This article will discuss the installation of PostgreSQL and configuration of the same with WSO2 Api Manager, on a linux Debian distribution.

Please note that if you have several installations of PostgreSQL locally or have had trouble getting it to work due to corrupt installations and etc. I would suggest starting off with a clean slate. Hence, you may run the commands listed below to completely uninstall PostgreSQL from your system.

apt-get --purge remove postgresql\*

Now lets delete all configurations.

rm -r /etc/postgresql*
rm -r /var/lib/postgresql*
userdel -r postgres
groupdel postgres

So lets start with the installation. Just type the following command.

sudo apt-get install postgresql

Ok, now that you have installed PostgreSQL, lets set it up. At the time of installation PostgreSQL will create a user by the name "postgres". Executing the following command will log you in to the PSQL admin console

sudo -u postgres psql

Enter the admin user password and you should see a prompt indicating "postgres=#".
Now lets go ahead and create two databases. One for the API Manager specific tables, lets call it "am_db". Another for the WSO2 Carbon tables, lets call it "carbon_db". Remember, even if you use upper case to create the databases, the persisted database name will comprise of only lowercase characters, hence might as well go with lower case at the time of creation.

create database am_db;
create database carbon_db;

Now, we need to create the tables. The API Manager pack comes with the scripts to create each set of tables they can be found inside the "dbscripts" folder of the AM zip.

The carbon DB script is at <AM_home>/dbscripts/postgresql.sql
The carbon DB script is at <AM_home>/dbscripts/apimgt/postgresql.sql

Remember what we learned earlier about the user "postgres" you will need be logged in as this user to execute whatever PostgreSQL scripts that you may wish to run.

Before attempting that, lets change the password of the 'postgres' user. I found it easier to do, rather that scour the internet  to find what the default password was. Just login as admin user and change it to a desirable value via the following command.

passwd postgres;

Lets login as 'potgres' via;

su - postgres;
and execute the two table creation scripts against each Database;

psql -d am_db -a -f <path_to_am_script>/postgresql.sql 
psql -d carbon_db -a -f <path_to_carbon_script>/postgresql.sql 

Voila! now you have the two databases setup. What's left is to configure the "master-datasources.xml" inside <AM_HOME>/repository/conf/datasources/, add the PSQL driver and start the server.

You will need to change the WSO2_CARBON_DB and WSO2AM_DB data sources in the master-datasources file as follows.


 WSO2_CARBON_DB
 The datasource used for registry and user manager
 
  jdbc/WSO2CarbonDB
 
 
  
   jdbc:postgresql://localhost:5432/carbon_db
   postgres
   postgres
   org.postgresql.Driver
   50
   60000
   true
   SELECT 1
   30000
  
 



 WSO2AM_DB
 The datasource used for registry and user manager
 
  jdbc/WSO2AM_DB
 
 
  
   jdbc:postgresql://localhost:5432/am_db
   postgres
   postgres
   org.postgresql.Driver
   50
   60000
   true
   SELECT 1
   30000
  
 

Done? great. Download the PostgreSQL driver relevant for your version from here, and copy the .jar file to the the following location inside the AM Pack.

<AM_HOME>/repository/components/lib/

That's it! go and start API Manager by running "./wso2server.sh" from inside of  <AM_HOME>/bin.