javakaffee

Just another weblog about (web) development technologies like spring, tapestry, jersey, jsf, hibernate and more

November 15, 2008

How to use spring-aop in jersey - added example resource class to the spring-annotations sample

Filed under: development, java, webdev, REST — martin.grotzke @ 8:40 pm

Yesterday I added another example resource to the spring-annotations sample that shows how to use spring-aop with jersey.


The example contains a resource class SpringAopResource that is created by spring (in this case via autodetection/classpath scanning). There’s an example SecurityAdvice defined with a pointcut matching methods annotated with a custom @Secure annotation. The resource method of the SpringAopResource of course is annotated with this annotation, so that the SecurityAdvice will be applied to this resource method. (The SecurityAdvice in our case does plain stupid logging, this should be changed of course :))


So that spring-aop can come into play our resource class needs to be proxied. This raises the question about subresources. Normally they would be created by the resource class, e.g. some UsersResource.getUserResource(String userId) would return some new UserResource(userId). At this point the UserResource could not be proxied by spring - simply impossible. For this we introduced the ResourceContext (I already blogged about this in the context of the initial jersey-spring integration), so that a resource class can fetch an instance of a subresource class from the IoC container.
This is demonstrated by the subresource locator SpringAopResource.getSubResource() which returns an instance of a SpringAopSubResource fetched from the ResourceContext.


The code is shown below - just too little to believe that this should be all :)


The configuration in applicationContext.xml for aspectj-annotation support, and the configuration of the advice bean.

<beans xmlns="..." etc.>
    ...
    <aop:aspectj-autoproxy/>
    <bean id="securityAdvice" class="com.sun.jersey.samples.springannotations.resources.aop.SecurityAdvice" />
    ...
</beans>


The @Secure annotation and the SecurityAdvice:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Secure {

}

@Aspect
public class SecurityAdvice {
    
    private static final Logger LOGGER = Logger.getLogger( SecurityAdvice.class.getName() );
    
    @Before( "@annotation(com.sun.jersey.samples.springannotations.resources.aop.Secure)" )
    public void check( JoinPoint jp ) {
        /* this might get some authentication/authorization info from the request 
         * and throw a WebApplicationException with a response with status 401 (unauthorized)
         * if the request is not authorized.
         */
        final Signature signature = jp.getSignature();
        LOGGER.info( "Authorized execution of " + signature.getDeclaringTypeName() + "." + signature.getName() );
    }
    
}


The resource and subresource classes:

@Path("/spring-aop")
@Component
@Scope("singleton")
public class SpringAopResource {
    
    @Context
    private ResourceContext _resourceContext;

    @Autowired
    private Item _item;

    @GET
    @Secure
    @Produces("application/xml")
    public Item getItem() {
        return _item;
    }
    
    @Path( "subresource" )
    public SpringAopSubResource getSubResource() {
        return _resourceContext.getResource( SpringAopSubResource.class );
    }
    
}


@Component
@Scope("prototype")
public class SpringAopSubResource {

    @Autowired
    @Qualifier("1")
    private Item2 _item;

    @GET
    @Secure
    @Produces("application/xml")
    public Item2 getItem() {
        return _item;
    }
    
}

That’s all. The Item and Item2 classes are just normal beans injected by spring.


You can pull the full example from subversion or browse it online.

August 31, 2008

Added new jersey sample spring-annotations - how to use jersey and spring/annotations

Filed under: development, java, webdev, REST — martin.grotzke @ 5:00 pm

August 28, 2008

Jersey and WADL generation - documented!

Filed under: development, java, webdev, REST — martin.grotzke @ 3:06 am
Tags: , , ,

May 18, 2008

Jersey DI - Use custom annotations for dependency injection in your resource classes

Filed under: development, java, webdev, REST — martin.grotzke @ 5:10 pm
Tags: , , ,

April 28, 2008

jersey-spring and changes now available in the jersey trunk / namespace com.sun.ws.rest renamed to com.sun.jersey

Filed under: development, java, webdev, REST — martin.grotzke @ 11:57 pm

April 21, 2008

jersey-spring integration mostly complete

Filed under: development, webdev, REST — martin.grotzke @ 1:48 am

March 8, 2008

Jersey is fun - exploring RESTful webservices - subtitle: @Springify your resources

Filed under: development, java, webdev, REST — martin.grotzke @ 6:14 am

Powered by WordPress