Wednesday 25 March 2009

Tuckey URL Rewriter

I have been using a url rewriting plugin for Spring, that allows urls to be less cluttered with querystring params. I am using it as an interim step before sorting a proper photo cache.

What I want is to have a url like:

/cache/player/1/picture



Spring 3.0 will allow this kind of thing, but for the moment there is a url rewriting filter from http://tuckey.org

The steps I used were:

1. Add a dependency to the pom file

<dependency>
 <groupid>org.tuckey</groupid>
 <artifactid>urlrewrite</artifactid>
 <version>2.5.2</version>
</dependency>



2. Add a filter to web.xml

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>



3. Add a file called urlwrite.xml to WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<from>/cache/player/([0-9]+)/picture$</from>
<to>/cache/player/picture?accountId=$1</to>
</rule>


Where <from> and <to> are regular expression match and replace values

4. Create a normal Spring controller and configuration to handle requests like

/cache/player/picture?accountId=1



This means that later I can cache the picture as a file and change the controller to being a 404 handler.