Wednesday, December 17, 2008

Managing contexts in CXF JAXRS

In JAXRS, a number of classes representing various features of a given request exist, as opposed to a composite WebServiceContext in JAXWS which lets get to individual contexts and message properties.

Imagine this JAXRS resource class which needs to know where the request came from, how secure it is, what the structure of the request URI is, what servlet context parameters corresponding to a given request are, what HTTP headers are involved and if certain request preconditions are met. And if you're writing a composite message body provider then, in addition to the above requirements, you also want to query other available providers as well as to check if a certain context resolver is available.

How unreatistic these requirements are ? It's likely they won't arise in many cases at the same time but likewise it's likely they'll will have to be met in some cases.

So here's a portable JAXRS code showing how you can get an access to all the contexts :

public class CompositeMessageReader
implements MessageBodyReader<Composite> {

@Context UriInfo u;
@Context SecurityContext sc;
@Context HttpHeaders h;
@Context Request r;
@Context ServletContext sc;
@Context HttpServletRequest httpR;
@Context ContextResolver contextR;

// use the injected contexts


And here's a CXF JAXRS specific code on how you can get an access to all the contexts plus the properties of the underlying message.

public class CompositeMessageReader
implements MessageBodyReader<Composite> {

@Context org.apache.cxf.jaxrs.ext.MessageContext mc;
// use the injected contexts

FutureJAXRSContext newContext =



I'm contradicting here to my previous post where I'm whining :-) about the JAXRS portability.
But you know how long would it take you to get back to a portable JAXRS code.

No comments: