Search code examples
javaplayframework-1.x

How to invoke servlet filter in Play 1.2.x before routing


Overall:

My question is about adding a Servlet Filter to Play framework project to be invoked before routing.

Versions:

  • Java 7
  • Play 1.2.6

More Info:

  • I tried Play interceptors with @Before marker and it works. I would prefer to use servlet filter to be applied before routing.

Issue:

  • The filter is not invoked at all even before or after routing. Could you please share your genuine solutions with me to see if it is doable to have Servlet Filters in Play framework

Solution

  • AFAIK there are no servlet interceptors in Play 1.x You can use the @Before annotation to solve some of your problems. But @Before is not a fully qualified alternative to an interceptor. A manual (and a bit low level) approach can be adding your interceptor to play pipeline. Play has a configuration play.netty.pipeline which defaults to :

    #play.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.PlayHandler
    

    You can modify the pipeline and add your custom handler before the last element (play.server.PlayHandler) in the list.