Search code examples
jbossresteasytapestrytynamo

Using an interceptor with Tapestry Resteasy


I have a resource class and I'd like to be able to check an authentication token before the resource method is called, thus avoiding having to pass the token directly into the Resource method.

I have added the following to web.xml:

<context-param>
        <param-name>resteasy.providers</param-name>
       <param-value>com.michael.services.interceptors.AuthorisationInterceptorImpl</param-value>
</context-param>

My interceptor is implemented as follows:

@Provider
public class AuthorisationInterceptorImpl implements javax.ws.rs.container.ContainerRequestFilter {

    @Inject
    private ApiAuthenticationService apiAuthenticationService

    @Override
    public void filter(ContainerRequestContext requestContext) {

       //Code to verify token   

    }

}

The filter method is being called before the methods in my resource class; however, the apiAuthenticationService is not being injected and is null when I attempt to call its methods.
I'm using Tapestry 5.3.7, Tapestry-Resteasy 0.3.2 and Resteasy 2.3.4.Final. Can this be done ?


Solution

  • I don't think this will work, based on a quick glance at the tapestry-resteasy code.

    The @Inject annotation is part of tapestry-ioc; if a class is not instantiated by Tapestry, the @Inject annotation is not honored.

    Filters defined in web.xml are instantiated by the servlet container (Jetty, Tomcat, etc.) which do not have any special knowledge of Tapestry and Tapestry annotations.

    I think you will be better off contributing a filter into Tapestry's HttpServletRequestHandler or RequestHandler pipelines (see their JavaDoc). I'm not sure how you can gain access to the ContainerRequestContext, however.