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/

Reply via email to