Saturday, November 12, 2011

Restful urls and spring mvc

Spring 3.0 boasts of Rest support. One aspect of this is using resource-based urls. For example, using /hotels/hilton/suiteNo1 instead of /hotels?hotel=hilton&room=suiteNo1.

So how do we achieve this? The typical fashion of using spring mvc is to map all the incoming requests that match some pattern (e.g. *.form) to spring's DispatcherServlet like this -


    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>


This mandates that our application urls are like /hotels/hilton.form. This is not what we want here. We'd rather drop the .form if we can.

So lets map everything to the dispatcherServlet instead of just .form requests, shall we?



    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


This enables us to drop the .form extensions. But there's a problem lurking here. A request for a static public resource like an image also goes via the dispatcherServlet. And since there won't be a controller defined for that, it'll throw a 404. We don't really want to map css and image requests to the spring servlet.
The problem can be summarised as follows : We want to map all requests except css/image/js requests to the dispatcherServlet.


How do we achieve this? Tuckey's urlrewrite comes to the rescue. In principle, its simply a Filter that lets us rewrite urls based on certain patterns. The order of processing is important. The filter will kick in _before_ the container chooses which servlet gets to handle the request. So what we need to do in the tuckey urlrewrite filter is to 'mark' all the urls which we want spring to handle with a certain prefix (say /myapp) and leave the css/image/js urls untouched. Furthermore, we configure the spring servlet to handle only those urls with that myapp prefix in them. Here's how the tuckey configuration looks:


    <rule>
        <name>Jsp Rule</name> 
        <from>**/app/**/jsp/**</from> 
        <to last="true">$1/$2/jsp/$3</to>
    </rule>
    <rule>
        <name>Css Rule</name>
        <from>**/css/**</from>
        <to last="true">$1/css/$2</to>
    </rule>
    <rule>
        <name>Js Rule</name>
        <from>**/js/**</from>
        <to last="true">$1/js/$2</to>
    </rule>
    <rule>
        <name>Spring Rule</name>
        <from>**</from>
        <to>/app/$1</to>
    </rule>

There is some duplication here - maybe all the resources can be moved under a common directory. But I'll leave that to all you responsible developers.
The 'last=true' bit is important - and something that was missing from many other blogs. Without it, tuckey just falls through to the next rule after one rule matches. So after matching /css/style.css it'll fall through to the spring rule and change it to /app/css/style.css. Won't do. The last=true means tuckey will not process subsequent rules if there is a match on that particular rule.

And there we have it. Beautiful urls.

Wednesday, November 9, 2011

Television

I am not a big TV fanboy. I generally prefer the outdoors instead. However, these days, for better or for worse, I've been watching a bit of TV. My focus is mainly on the wildlife, travel and sports ones. My favourite channel without a doubt is National Geographic HD. The astronomy-related shows are superb. They're also based on fairly cutting-edge and recent research. So its mostly stuff that you'd probably not know via other means unless you are working in a related field. Did you know there's a jet of water shooting from one of Saturn's moons? The wildlife shows are a good reminder of what is out there and we're missing. From seeing the annual migration of the wildebeest to understanding why the shark is nature's greatest killing machine ever built - maybe you can get by without knowing. But again, you're missing out. Other favourites are TLC (Travel and Living). Their shows provide a great insight into so many different cultures. I find it very interesting. As far as sports on TV are concerned, I don't have much time left to watch any more than the occasional Soccer and Cricket. There are definitely better ways to spend your time, but its not all bad.

Tuesday, June 28, 2011

The Vicious Circle Conundrum

I skipped work today. Officially speaking, its a sick leave. But I am not really sick. (I'm positively sure my boss won't see this) Demotivated, perhaps.
So I was trying to figure out why I am demotivated. Its not that hard really. When you bang your proverbial head against a wall 8 hours a day, 5 days a week, you feel demotivated. Nothing motivates you like making progress in whatever you are doing. The feeling of 'getting somewhere' is innocuously powerful - however small the increment may be. Its probably why people suddenly start walking faster when they feel they are close to their destination. Some people think its appreciation that motivates - I disagree. Appreciation ostensibly seems to motivate - but the real cause is that it gives us a feeling of making progress. Undeserved appreciation is actually scary.
So then the answer to demotivation (and preserving your sick leaves for real illnesses) is to make progress. But hey, you can't make much progress unless you are motivated in the first place! Thats the vicious circle I'm talking about.
The only way out - as far as I can see - is to get a burst of energy when you are demotivated to propel you out of the rut and make enough progress to feel motivated again.
Good thing I'm going on a 3 week vacation in August.

We are surrounded by many such circles. E.g. Its hard to find friends unless you have some friends to start with.

Are you stuck in a circle too?

Friday, February 18, 2011

Cricket World Cup

A speculation, albeit incorrect, does not go to waste. Here, the end result is not the point. Its the stimulation of discussion and thoughts that matters.
So. Here are mine.
History shows that the cup has been won by (a) An overwhelmingly strong team OR (b) a well-settled team having strong leadership and which is on the rise and in good form.
'75, '79 - West Indies. Its no understatement that they were far ahead of the rest of the pack.
'83 - India. This was a team on the rise and in high spirits, after giving WestIndies a run for their money just before the tournament started.
'87 - Australia. Again, a team in good form and a positive mood leading to the cup.
'92 - Pakistan had just beaten WestIndies at home. Which was no mean achievement even in 1991
'96 - Sri Lanka. Strong leadership of Arjuna Ranatunga and a team on a high after beating Australia in ODIs.
'00, '03, '07 - Australia were strong favourites and dominated world cricket during this period.

So what does that mean for this tournament? For one, there aren't any overwhelming favourites. Lets look at the teams.
Pakistan look too unsettled for my liking, in spite of wining against NZ on their home turf.
England lost by a long way to Australia in the ODIs and the joy of winning the ashes has worn off. They're plagued by injuries too.
NZ got thrashed 5-0 in India. Admittedly by one of the favourites, but its hard to envisage them picking up the pieces from here.
West Indies is a team in flux. Not to mention they have loads of players who don't have much experience of playing in the subcontinent. Unlikely.
Bangladesh is an interesting one. They've beaten Zimbabwe and NZ at home. They're playing in familiar conditions. But do they have the firepower to go all the way? I don't think so. Not yet. In the series against NZ, their top batsmen was Shakib Al-Hasan, averaging 71. The next best was 34. Less than half! They are reliant on a few good players and thats not good enough to win the world cup.
Zimbabwe have lost miserably in the recent past to South Africa and then Bangladesh.

So, ignoring the minnows, we're left with Australia, India, Sri Lanka, South Africa in no particular order.
Thats the likely semis lineup for me. From here, it gets hard. It calls for a closer look. Maybe later.

Sunday, January 23, 2011

ProperNounisation

ProperNounisation (figure of speech): Capitalisation of a common noun to stress upon a recurring pattern.
e.g.
If your girlfriend finds out about this, you're in Big Trouble.
I'll be in the mall around 6 doing some General Loitering.

Thursday, January 20, 2011

Classical guitar

For a long time, my classical guitar skills were quite limited. I've mostly been dabbling with an electric or (non-classical) acoustic sounds. But in the last 6-8 months or so, I've been drawn to classical guitar. I happened to come across this video of Capricho Arabe (by the legendary Francesco Tarrega) and I was blown away. This degree of expressiveness using a single instrument is basically unheard of in rock/metal circles. imho. No entourage of bassists/drummers/vocalists, no distortion pedals to muddy the waters, just pure unadulterated music.
Soon thereafter, I got my first real classical guitar. The Almansa 434. I just had to do it.
Now I'm learning some of Francesco Tarrega's other songs. Also planning to take proper classical lessons from a well-known local guitarist, Anthony Garcia.
Where is this going to lead me?
PS: Never 'drink and blog'. Or this may happen to you.

Wednesday, January 19, 2011

Unsocialising

Unsocialising (n): Reducing or eliminating your social footprint in the cyber world.
Although I acknowledge the human need to socialise, I just don't find it appealing to have my personal details paraded on some server belonging to some company I don't really know well enough to trust.
So; I have decided to unfacebook and ungoogle myself.
As far as fb is concerned, I don't have much there thats personal, but I will have to remove my name-tags from a few photos. Boy I hate that feature.
UnGoogling will be a more difficult task - their apps actually provide value to me. Maybe a mobileme subscription... I need to think about this. Now is not the best time. I am drinking irish whiskey.