Hi Sergey, Thank you for the quick response. It would be greatly appreciated if you could fix the problem.
There is another minor problem related to this one. The list returned from UriInfo.getMatchedResources() is not in the correct order according to the JAX-RS spec. >From the Java Doc for UriInfo.getMatchedResources(): "Get a read-only list of the currently matched resource class instances. Each entry is a resource class instance that matched the request URI either directly or via a sub-resource method or a sub-resource locator. Entries are ordered according to reverse request URI matching order, with the current resource first." In my testing, the entries are NOT ordered according to reverse request URI matching order. The current resource is actually at the end. Thanks, Tao -----Original Message----- From: Sergey Beryozkin [mailto:[email protected]] Sent: Friday, November 25, 2011 1:20 PM To: [email protected] Subject: Re: Cannot obtain UriInfo in isWriteable() for custom providers Hi On 25/11/11 18:21, Lin, Tao wrote: > Hi, > > I would like to write a custom provider, which extends a default provider in > CXF. I would also like to make the custom provider only handles the resources > my application exposes. It should be possible since the requested resource > can be obtained through UriInfo object (UriInfo.getMatchedResources()), which > is injected into the provider directly or indirectly . > > However, UriInfo is only available in writeTo() but not in isWriteable(). > Here is the sample code to demonstrate the problem: > > ~~ > @Produces("application/json") > @Consumes("application/json") > @Provider > public class MyJSONProvider extends JSONProvider > { > @Context MessageContext context; > > @Override > public boolean isWriteable(Class<?> type, Type genericType, > Annotation[] annotations, MediaType mt) > { > UriInfo uriInfo = context.getUriInfo(); > List<Object> resources = uriInfo.getMatchedResources(); // throw > null pointer at this line > > return Customer.class.isAssignableFrom(type); > } > > @Override > public void writeTo(Object obj, Class<?> cls, Type genericType, > Annotation[] anns, MediaType m, MultivaluedMap<String, Object> headers, > OutputStream os) > throws IOException > { > UriInfo uriInfo = context.getUriInfo(); > List<Object> resources = uriInfo.getMatchedResources(); > > super.writeTo(obj, cls, genericType, anns, m, headers, os); > } > } > ~~ > > In the writeTo() method, uriInfo contains the information I need while in the > isWriteable(), uriInfo is null. But what I really want is the uriInfo > available in isWriteable(). > > Here is my question: > > is this by design or simply a bug? > > To me, this is a bug. > I'll probably agree; I've been aware of it, the reason I've never done it that all the the injection is kind of not cheap so I thought I'd only get the code injecting contexts only for the chosen provider. I'll investigate a bit more, and get back on this issue Cheers, Sergey > Thanks, > > Tao > > > > > > -- Sergey Beryozkin http://sberyozkin.blogspot.com Talend Community Coders http://coders.talend.com/
