Let’s say that you are a JAVA web applications developer and you want to rewrite some sets of URL’s to some other. Nothing is easier than that! Simply use the Tuckey Url Rewriter Filter (http://tuckey.org/urlrewrite/). This tool is just like Apache’s mod_rewrite and permits you to manage the url rewriting rules with a very simple XML files to be put in your WEB-INF folder. URL’s to be redirected can be defined via regular expressions, which makes the tool quite flexible and will permit you to handle of the rewriting situations you will have to face.
I would suggest you to refert to http://tuckey.org/urlrewrite to see how to use the tool, but in short you simply have to define the URLRewriteFilter in your web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
At this point download the sample urlrewrite.xml and put in your WEB-INF folder and the trick is done. Every time an HTTP request is performed in the domain of your web application, if the URL matches one of the rule, it will be rewritten.
Make sure of course that the tuckey jar is available in your project libraries. If yours is a Maven project just add the dependency:
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>
A constraint I found a little annoying is the fact that the filter is always expecting to find the urlrewrite.xml in the WEB-INF folder or your project. This is not very flexible, because it means that file must be in the war you deploy and therefore in order to change one of your redirect rules you will have to redeploy the whole application, or change the file inside the decompressed war in your web server. This does not seem very clear to me.
With the version 3.1.0 the org.tuckey.web.filters.urlrewrite.UrlRewriteFilter is not marked as final anymore and you can therefore extend it and you can therefore extend it and define another location where to load the configuration file from, or even load it from a remote machine. This will give you the possibility to define new rules and make them effective simply restarting the web application. Here below a simple example when you want to load the configuration file from the fixed absolute path /home/myuser/myurlrewrite.xml . In your new filter extending the org.tuckey.web.filters.urlrewrite.UrlRewriteFilter simply override the method loadUrlRewriter:
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
InputStream inputStream = new FileInputStream("/home/myuser/myurlrewrite.xml");
Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "myurlrewrite.xml", "systemId");
urlrewriters.add(new UrlRewriter(conf1));
} catch (Exception e) {
throw new ServletException(e);
}
}
0 Responses to “Tuckey Url Rewriter Filter”