Using UrlRewriteFilter with Dropwizard

529 views
Skip to first unread message

Nick Lothian

unread,
Aug 19, 2014, 2:54:06 AM8/19/14
to dropwiz...@googlegroups.com
Hi,

I'd like to use UrlRewriteFilter (http://www.tuckey.org/urlrewrite/) with Dropwizard.

I have the following code:

final FilterRegistration.Dynamic rewrite = environment.servlets().addFilter("UrlRewrite",UrlRewriteFilter.class);
rewrite.setInitParameter("confPath", "/urlrewrite.xml");
rewrite.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

That seems to successfully create the filter. However, it can't find the configuraiton file:

org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: unable to find urlrewrite conf file at /urlrewrite.xml

Has anyone successfully got this to work? Any tips for where to put it, or how to debug where it is looking?

Nick

Jochen Schalanda

unread,
Aug 19, 2014, 3:02:23 AM8/19/14
to dropwiz...@googlegroups.com
Hi Nick,

On 19.08.2014 08:54, Nick Lothian wrote:
> Has anyone successfully got this to work? Any tips for where to put it,
> or how to debug where it is looking?

According to UrlRewriteFilter line 265 and following
(https://code.google.com/p/urlrewritefilter/source/browse/trunk/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java#265)
the filter tries to read the configuration file from your classpath.

You could simply set a breakpoint at that line and debug your
application in the IDE of your choice.


Cheers,
Jochen

Nick Lothian

unread,
Aug 20, 2014, 1:00:04 AM8/20/14
to dropwiz...@googlegroups.com
Thanks.

I debugged it, with a result that I found surprising.

In my version, UrlRewriteFilter is doing the following:

  InputStream inputStream = context.getResourceAsStream(confPath);

So I built a filter to wrap UrlRewriteFilter and added some logging:

  System.out.println(filterConfig.getServletContext().getResource("/").toString());

Which prints:
  
  file:/

It took me a while to work out that yes, that really does mean "/" - the root directory of the whole machine. Sure enough, once I put urlrewrite.xml at /urlrewrite.xml it worked.

Obviously this is less than ideal! I was expecting it to be the base of the application classpath perhaps. 

Does anyone have further tips to debug this? The obvious filterConfig.getServletContext().getResource("urlrewrite.xml") gives java.net.MalformedURLException.

Nick

Jochen Schalanda

unread,
Aug 20, 2014, 3:42:40 AM8/20/14
to dropwiz...@googlegroups.com
Hi,

On 20.08.2014 07:00, Nick Lothian wrote:
> In my version, UrlRewriteFilter is doing the following:
>
> InputStream inputStream = context.getResourceAsStream(confPath);
>
> So I built a filter to wrap UrlRewriteFilter and added some logging:
>
> System.out.println(filterConfig.getServletContext().getResource("/").toString());
>
> Which prints:
>
> file:/

That's probably the fallback in line 272
(https://code.google.com/p/urlrewritefilter/source/browse/trunk/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java#272).

Just put your urlrewrite.xml into ./src/main/resources/WEB-INF/ (which
is the default path, see
https://code.google.com/p/urlrewritefilter/source/browse/trunk/src/main/java/org/tuckey/web/filters/urlrewrite/UrlRewriteFilter.java#88)
and try again.


Cheers,
Jochen

Nick Lothian

unread,
Aug 20, 2014, 8:06:48 PM8/20/14
to dropwiz...@googlegroups.com
Thanks. 

That didn't quite work. It thinks /WEB-INF is in the root of the file system (!) which just doesn't work. But it prompted me to dig deeper into what the classpath actually is, and I eventually got it working with this:

rewrite.setInitParameter("confPath", "urlrewrite.xml"); // Note, NOT /urlrewrite.xml

with the urlrewrite.xml file in ./src/main/resources/

It's odd - I always thought "/" meant the root of the classloader - but I have to admit I haven't looked up the spec to see what it says.

Nick

 
Reply all
Reply to author
Forward
0 new messages