users@jersey.java.net

Re: [Jersey] ResourceFilterFactory and Spring _at_Autowired

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 5 Oct 2010 16:57:58 +0200

Hi Davide,

You are right that the issue is because you instantiate SecurityFilter
as follows:

   new SecurityFilter();

You need to first of all make the SecuredFilterFactory spring managed
and then either:

1) Inject the stuff onto SecuredFilterFactory and pass those injected
references to the constructor of SecurityFilter; or

2) Use the Spring API to programatically obtain a reference to
SecurityFilter. To do that you will need to inject something onto
      SecuredFilterFactory to look up references, but i dunno what
that is.

Paul.

On Oct 5, 2010, at 4:16 PM, Davide Gurgone wrote:

>
> 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.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>