0

I'm using Tomcat, but i'd like to go for a generic way (servlet 3.0)

I have some configured filters in the web.xml and one of these is the GuiceFilter. GuiceFilter is a a little bully...once it get executed it will swallow the request and never call proceeding filters. This way i cant get executed any filter after the Guice one. I could define all filters in guice insetead of web.xml, but this will not work for some filters: "Tomcat WebSocket (JSR356) Filter" This is a filter Tomcat will register after all other to handle WebSockets...it should be the first one, not the last one, but i just need it gets executed before GuiceFilter or inside it.

I've found a way to get all registered filters and add them to guice:

for (Entry<String, ? extends FilterRegistration> entry : context.getFilterRegistrations().entrySet()) {
        FilterRegistration filterRegistration = entry.getValue();
        System.out.println(entry.getKey() + " = " + filterRegistration);
        Class<? extends Filter> clazz = (Class<? extends Filter>) Class.forName(filterRegistration.getClassName());
        if (GuiceFilter.class.isAssignableFrom(clazz)) {
            continue;
        }

        bind(clazz).in(Singleton.class);
        filter(filterRegistration.getUrlPatternMappings()).through(clazz, filterRegistration.getInitParameters());
    }

but this way i'll get some filters (the one before Guice one) executed twice (the normal filter chain and the guice one)

Is there a way to get the filters order? (so i can add only the ones after guice)

Or, another way: is there a way to remove the filter from the normal chain leaving only the guice one (moving all other filters to guice itself)?

lelmarir
  • 593
  • 2
  • 7
  • 24
  • Wait, your original problem is filter ordering, right? Would knowing they are [executed in their declared order](http://stackoverflow.com/a/17086794/785663) help? – mabi Aug 26 '16 at 13:15
  • I know the order of filters, but i need a way to figure it out at runtime. My "original problem" is to execute all other filters before guice one, because GuiceFilter will not call any filters after it. I can solve the problem knowing, at runtime, which filter actually is after GuiceFilter (and will not get executed) so i can redeclare it inside GuiceFilter or be able to add all filters but remove them from the chain (leaving only GuiceFilter that will call all of them) – lelmarir Aug 26 '16 at 22:02
  • The point of the answer I linked was that you can declare guicefilter last in your `web.xml` and it will be loaded after all others. Need to test myself, though. – mabi Aug 26 '16 at 23:26
  • It's true, but some filters (Tomcat WebSocket (JSR356) Filter) are registered directly from Tomcat (and are not declared in web.xml) thus i have no control on their order (and Tomcat always register it after any web.xml ones) – lelmarir Aug 27 '16 at 09:27

0 Answers0