5

I'm looking for a mechanism within JAX-RS (JSR-311) to allow me to distill out some of my cross-cutting concerns specific to my app. For example, my app has certain context which is looked up or built for each request. I'd like to be able to have this type of logic be performed in a centralized location and then somehow be attached to the context to be utilized by various resources for the remainder of the request. It would be even better if I could perform these types of actions for only some subsets of URLs.

The default injection that JAX-RS provides for path segments, cookie, header, etc. is great but what about custom interpretation of those parts? I really don't want to have to build that each time I need it. I'd rather have a way to specify how it is built and then just have the context component injected as part of my resource method.

Do any such hooks exist? Can I manipulate the providers model to do this? BTW, I want to stay implementation independent (Jersey, RESTEasy, etc.) as long as possible.

Thanks in advance for any insight.

mckamey
  • 17,359
  • 16
  • 83
  • 116

2 Answers2

0

In some sort of bizaro twist, standards are working together (JAX-RS & CDI) in a wonderfully composable manner, and IBM wrote a tutorial which which may cover my specific question. Part of a larger tutorial on combining CDI and JAX-RS, this article specifically addresses using CDI (Java Contexts and Dependency Injection) decorators and method interceptors to implement cross-cutting concerns in JAX-RS resources:

Update: I was just able to get this to work in GlassFish 3.1. The key (which none of the examples I've found showed) is that you have to make sure CDI manages the lifespan of your resource instances (so it can wrap with the interceptors). Instantiating yourself and then returning in the Application.getSingletons() method does not work.

I'm going to go back and see if I can get it to go in Jetty too.

Update 2: Jetty (and by extension probably any other non-J2EE servlet containers like Tomcat) are kind of a pain to get set up with CDI. I think GlassFish is a much easier integration. Here is one blog that outlines some of the steps needed for Jetty:

Frank Wong
  • 1,718
  • 2
  • 13
  • 12
mckamey
  • 17,359
  • 16
  • 83
  • 116
0

You can use ContextResolver provider to provide any context to the resource or to another provider. Basically you need to implement javax.ws.rs.ext.ContextResolver<T> for any context you want to get injected. Don't forget to annotate it with @Provider and register.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • I'm not sure this is what I'm looking for, or at least I'm unclear how this solves it. Ideally I'd like to add an annotation to my resource and have that trigger additional functionality (logging, security checks, building up context objects). When I looked through Providers I kept running into `MessageBodyReader`/`Writer`. Can you elaborate how I could use Providers to provide cross-cutting aspects? – mckamey Mar 19 '11 at 01:26
  • Sorry, but it seems I didn't understand the original question correctly (should not get here at 12am). The correct answer - it's impossible to make cross-cutting aspects using only JAX-RS spec. But if you combine JAX-RS with Spring, it may become possible. Spring provides a neat support for AOP. Basically you should make sure that your resources are also registered as spring beans. Apache Wink provides a nice integration with Spring, so you can try use it. – Tarlog Mar 19 '11 at 09:57