views:

56

answers:

2

I have set up UrlRewriterFilter (Tuckey) with many rules and it is working very good for my servlet. But I want to use the same config to rewrite urls outside servlet - in code that generates e-mails with urls.

So, I need to somehow start UrlRewriter (or some kind of wrapper) to process outgoing url i.e. rewrite them with my outbound-rules already defined in config (urlrewrite.xml).

I would like to use it just like this:

String prettyUrl = urlRewriter.rewriteOutgoingUrl(uglyUrl);

Is this possible at all? How to achieve this goal?

A: 

You probably just need to initialize UrlRewriteFilter. Since you have to add this in your web.xml file normally the UrlRewriteFilter config files probably get loaded once. I would try loading this in a static initializer in some wrapper class you define. To define a static initializer all you need to do is within a class have the following:

static { //Your initialization code goes here }

This code will only be initialized once, during runtime. For more information please look here: http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

Michael Bazos
But UrlRewriteFilter works for request/response and whole rest of servlet environment, how to cope with that? What should I pass to filter instead of request/response objects constructed by servlet container?
WildWezyr
A: 

It's open source. You could review it's source code (http://code.google.com/p/urlrewritefilter/source/browse/trunk/src/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java) and see if the logic is available in standalone class(es) which don't rely on servlet request/response objects. If it is, just use it. Otherwise you can build it yourself based on the original source, reusing as much of the library as possible.

Konrad Garus
I've downloaded this code and reviewed it. It has too many references to Request and Response objects and cannot be used in standalone environment just like that - without heavy mocking etc.But I've also managed to make my own UrlRewriter that recognizes rules of Tuckey's (from urlrewrite.xml). My code is about 100 lines long and is totally independent of Servlet API (i.e. request/response objects).
WildWezyr