Use Spring classpath Resource as Tuckey UrlRewriteFilter configuration

Use Spring classpath Resource as Tuckey UrlRewriteFilter configuration

Recently I wanted to use the Tuckey UrlRewriteFilter.
It is described as: A Java Web Filter for any compliant web application server, which allows you to rewrite URLs before they get to your code.

I wanted to load my urlrewrite.xml as a Spring (classpath) resource, instead of loading it from the default location provided by the UrlRewriteFilter. The default behavior loads the configuration file from /WEB-INF/ulrewrite.xml. In my case I wanted to load it from the /src/main/resources folder, which is the root of my classpath.

With this small and easy to use decorated UrlRewriteFilter, you can load your configuration from any location. When you write a UnitTest for this decorated Filter, don’t forget to call the init() method before you call the actual doFilter(). For testing your UrlRewriteFilter rules, it is better to write an IntegrationTest, for example with the TestRestTemplate

Spicy Spring: Create your own ResourceLoader

As a Spring developer we know how to load a resource in a Service. The prefixes of classpath: and file: are commonly used. But what if we want to load resources from a different location like a database table RESOURCE, with our own prefix db:?

The Spring Framework provides some default built-in resource implementations, which can be found in the chapter Resources in the Spring Framework Reference Guide. The URL is prefixed and handled by the corresponding ResourceLoader (JavaDoc).

If we want to load a resource from a database table RESOURCE we have to create our own ResourceLoader which triggers on the prefix db:.
Continue reading