Programmer Zen

Just read this quote and I loved it:

A beginning programmer writes her programs like an ant builds her hill, one piece at a time, without thought for the bigger structure. Her programs will be like loose sand. They may stand for a while, but growing too big they fall apart.

Realizing this problem, the programmer will start to spend a lot of time thinking about structure. Her programs will be rigidly structured, like rock sculptures. They are solid, but when they must change, violence must be done to them.

The master programmer knows when to apply structure and when to leave things in their simple form. Her programs are like clay, solid yet malleable.

— Master Yuan-Ma, The Book of Programming

Authenticating using Facebook

I had figured out how to authenticate users using Facebook a couple of months back. But I didn’t document it, and so yesterday I again wasted my time figuring it out. So here is the brain dumb so that I won’t waste my time again at the time of need. :)

Prerequisite: Create an app using Facebook Developer account and create the necessary App ID and App Secret keys.

If you are testing out your app and you are running it locally, the site URL should be something like http://localhost:8090/

At the client side:

Asking for permission and login:

  • Use the SDK provided by facebook
  • I am assuming Javascript SDK is used. Inspect the response returned.
    • Send the authResponse.accessToken and authResponse.userID to the server for authentication.

At the server side:

Authentication:

  • Verify the user by calling the Facebook Graph API:

https://graph.facebook.com/debug_token?input_token={authResponse.accessToken}&access_token={app_id|app_secret}

Response:

{
    "data": {
        "app_id": "1234",
        "application": "testapp",
        "expires_at": 1426158000,
        "is_valid": true,
        "scopes": [
                "public_profile",
                "email"
                ],
        "user_id": "111112223333"
     }
}

  • input_token is the authResponse.accessToken we obtained as a result of the login process from Facebook.
    access_token is the App Token. It can either be generated as mentioned in this link. Or instead of generating we can use the concatenated form: {app_id} + ‘|’ + {app_secret}. I have used this second form above.
  • Verify the JSON obtained and check whether “is_valid” == True and “user_id” == authResponse.userID to make sure user is a valid one.
  • The authResponse.userID returned can be used to identify the user and used instead of username to lookup users.

Using the Graph API:

Suppose we want to obtain the user’s email Id at the server side:
Call the API as follows:
https://graph.facebook.com/{authResponse.userID}?fields=email&access_token={authResponse.accessToken}

Again Agastyakoodam !

When I completed the Agastyakoodam trek two years back, I had decided to visit this awesome place again. It happened this year about two months back.

This time it was a group of 6 friends, including me. We were very much fortunate to get the entry permit on the first day of the trekking season. This gave us the forest in a much more pristine and virgin form. Because, as people start coming forest loses its glory!

There is not much to write as I have already explained about the trek in my previous blog entry. But I’ll highlight the differences that happened this year.

This year onwards, permit is being sanctioned online by the Kerala Forest Department. This saved us a lot of hassle and we got the permit for the first day itself(the season started on Jan 15th this time).

We started on January 15 and completed the trek in 3 days, unlike the last time where we did it in 2 days. The result? We enjoyed the trek much more. It was a relaxed trek and we enjoyed it to the core. We took ample photos and videos too, something we couldn’t so last time because of time constraints :). I would recommend everyone to complete the trek in 3 days.

This time our gang was in a uniform (except one – he could make it only at the last moment, and so couldn’t have the luxury of being in our uniform). See our uniform for yourself. Bhakti exploding :D

IMG_1047

In fact, the forest warden in the base camp told us seeing our uniform that we had immense Bhakti inside us :D Little did he know that it was just a show off. Any way, this uniform was one of our best decisions. Trekking wearing this attire was the most comfortable thing!

So here is the trip in short – we started on Jan 15th morning at about 11:30 (we reached their late) and reached the base camp by 6 PM. The next morning we started the ascend to the peak at about 7 in the morning. This time since we were not at all in a hurry, we took enough time in enjoying each step ;). We reached the top at 2 PM! (thats’s too late by any standard, they want you to start the downward trek by 2:45PM max). Unlike last time, it was not at all windy, also it was too hot at the top. We started the downward journey by 2:30PM and reached the base camp by about 5:30PM (ya, downward journey was completed pretty quickly). We rested off the night. Next day morning, our departure was delayed by unexpected rains. So we could only start by 9 AM. We reached forest outpost by about 3:30PM. And thus our trek was done.

We had tracked our journey with GPS. I’ll update this post once I get the track details from my friend.

Keep Travelling :) Enjoy the world. :) Don’t disturb what’s left off the forest and don’t leave any traces :)

angamaly-marakkamo

“മനുഷ്യരെ രക്ഷിക്കേണ്ടത് ദൈവമാണ്. അല്ലാതെ ദൈവത്തെ രക്ഷിക്കേണ്ടത് മനുഷ്യരല്ല. ദൈവത്തെ മനുഷ്യൻ രക്ഷിക്കേണ്ടി വരുമ്പോൾ ദൈവത്തിന്റെ വിലയിടിയുന്നു.” – അങ്കമാലി മറക്കാമോ? – P.J.Antony

P.J. Antony writes that people were fooled into taking part in the Liberation Struggle (Vimochanasamaram) by the Church, the Congress party and everyone else (Uncle Sam’s CIA??) who were afraid of the Communist party getting their first foothold in India

Another NoSQL to the kitty

Graph database is a great tool for mapping and traversing data with relationships. But doesn’t RDBMS have relationships – one-to-one, many-to-one etc? Why should we go for graphs?
Its simple! Graph databases are best suited for those cases in which a graph comes to our mind instead of a table!
I finished the online course on Neo4J two days back. Its a really simple graph database, and a great starting point to experiment graph databases.

Now the NoSql databases I am familiar with are HBase(tried it about a year back), Redis and Neo4J.

Read and explore stuffs outside your domain. Happy coding :)

Jackson JSON and Dynamic JSON creation

FasterXML Jackson is a great tool for converting objects to/from POJOs.

Its a great tool when we know the structure of JSON beforehand. But what if we don’t know that? In that case we can use a Map<String, Object> to store the unknown objects. Below two links provide useful info:

http://stackoverflow.com/questions/18043587/why-im-not-able-to-unwrapp-and-serialize-java-map-using-jackson-java-library

http://www.cowtowncoder.com/blog/archives/2011/07/entry_458.html

Happy Coding :)

Creating collection of Spring Beans

Some times there might arise situations where you want to create a number of Spring Beans, but you don’t know how many beans are there at coding time.

Usually, you create a Spring bean as follows:

But what if you want to create 50 such beans which differ only in the argument supplied to the constructor? Or a similar case arise where the number of beans to be created cannot be determined at coding time?
In such cases, we can create the bean definitions and add the beans to Spring container at runtime using BeanFactoryPostProcessor.

Implement BeanFactoryPostProcessor in either a @Configuration file or a @Component bean(make sure this bean gets scanned and created).

If there are more than one constructor arguments, the call can be chained as follows:

All this works fine when you have to create a bean using constructor. But :( I haven’t figured out how to do this when we use a chain of factory methods to create a bean as follows:

Happy Coding! :)