users@jersey.java.net

ResourceFilterFactory and Spring _at_Autowired

From: Davide Gurgone <davide.gurgone_at_gmail.com>
Date: Tue, 5 Oct 2010 07:16:40 -0700 (PDT)

Hi,
I know is a little bit a FAQ, but I really can't understand why I cannot
make up and running this "home made" ResourceFilter...

I created the following RequestFilterFactory:
public class SecuredFilterFactory implements ResourceFilterFactory{
        @Override
        public List<ResourceFilter> create(AbstractMethod am) {

                Secured secured = am.getMethod().getAnnotation(Secured.class);
                
                // Method annotation overrides class annotation
                if (secured!=null&&secured.getValue()){
                        return Collections.<ResourceFilter>singletonList(new
SecurityResourceFilter());
                }
                
                else if (secured!=null&&!secured.getValue()){
                        return null;
                }
                
                // Class annotation overrides default
                secured = am.getResource().getAnnotation(Secured.class);
                if (secured!=null&&secured.getValue()){
                        return Collections.<ResourceFilter>singletonList(new
SecurityResourceFilter());
                }
                
                else return null;
    }
    
    private class SecurityResourceFilter implements ResourceFilter {
            
            @Override
            public ContainerRequestFilter getRequestFilter() {

                    // Instantiating Filter using default constructor
                    return new SecurityFilter();
            }

            @Override
            public ContainerResponseFilter getResponseFilter() {
                    return null;
            }
    }
}

It instantiate a ContainerRequestFilter, named SecurityFilter, where I
Autowire some spring fields:
@Component
public class SecurityFilter implements ContainerRequestFilter {

    @Context
    private UriInfo uriInfo;

    @Autowired
    private IHeadingService headingService;

    public ContainerRequest filter(ContainerRequest request) {
        authenticate(request);
        return request;
    }

    private void authenticate(ContainerRequest request) {

        // Some very complicated code here....

        try{
                // Here both, @Autowired field and uriInfo are null!
                Heading heading =
headingService.findByHostName(uriInfo.getRequestUri().getHost());

                // Some other more complicated code here...

        } catch(Exception e) {
                throw new WebApplicationException(400);
        }

    }
}

Spring configuration seems to be OK, it scans the right packages and in
resources, the same operation, runs fine.

In web.xml I declared the init parameter:

        <init-param>
           
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
            <param-value>{packagePath}.SecuredFilterFactory</param-value>
        </init-param>

I'm not a spring expert, but I think this issue exists because I
programmatically build my SecurityFilter.

How to solve it?

I think I'm missing something...

P.S. I really love Jersey, I think is the best framework now...

Best regards, Davide.
-- 
View this message in context: http://jersey.576304.n2.nabble.com/ResourceFilterFactory-and-Spring-Autowired-tp5603279p5603279.html
Sent from the Jersey mailing list archive at Nabble.com.