javaguicesitebricks

How to exclude a URL when registering filters in ServletModule.configureServlets?


I use sitebricks to build a RESTful API on Google App Engine. I register two filters for all /rest/* URLs in my GuiceCreator. How can I use the filter("/rest/*) syntax but exclude one specific URL? I want that everything under /rest/* gets filtered except for /rest/1/foo.

I could enumerate all URLs that actually need to be filtered. But the obvious disadvantage of this is that it will be hard to maintain if I decide to add or remove endpoints.

new ServletModule() {
    @Override
    protected void configureServlets() {
        filter("/rest/*").through(ObjectifyFilter.class);
        filter("/rest/*").through(SomeOtherFilter.class);
    }
}

I am looking for a construct like

filter("/rest/*").exclude("/rest/1/foo").through(ObjectifyFilter.class).

Solution

  • Thanks to dhanji, I fixed this by using filterRegex() instead of filter(). In my regular expression I am using a negative lookbehind assertion. This filters all /rest/.* URLs except the ones ending with /[0-9]/foo.

    new ServletModule() {
      @Override
      protected void configureServlets() {
        filter("^/rest/.*(?<!/\\d/foo)$").through(ObjectifyFilter.class);
        filter("^/rest/.*(?<!/\\d/foo)$").through(SomeOtherFilter.class);
      }
    }