dev@jsr311.java.net

EntityProvider Syntax Clarifcation

From: Ryan J. McDonough <ryan_at_damnhandy.com>
Date: Fri, 2 Nov 2007 09:56:52 -0400

As I read through some the spec and some of the Jersey code, I am not
clear on how the proper syntax on how to create a custom
EntityProvider. To help clarify my understanding, I have some code
below for a theoretical ImageEntityProvider which would be able to
render an Image representation. Based on what I have read so far,
this is my understanding so far on how to implement it:

@Provider
public class ImageEntityProvider implements EntityProvider<IIOImage> {
        @ConsumeMime("application/xml,image/jpg,image/png,image/gif")
        public IIOImage readFrom(Class<IIOImage> type,
                                                         MediaType mediaType,
                                                         MultivaluedMap<String, String> httpHeaders,
                                                         InputStream entityStream)
                        throws IOException {...}
        @ProduceMime("image/jpg,image/png,image/gif")
        public void writeTo(IIOImage image,
                                            MediaType mediaTypes,
                                            MultivaluedMap<String, Object> httpHeaders,
                                            OutputStream entityStream)
                        throws IOException {...}

The spec also mentions the use of the @Contract annotation, but I'm
not clear on the proper usage or the reason for it's existence. In
regards to the annotations, it also possible to be able to annotate
the provider this way:

@Provider
@ProduceMime("image/jpg,image/png,image/gif")
@ConsumeMime("application/xml,image/jpg,image/png,image/gif")
public class ImageEntityProvider implements EntityProvider<IIOImage> {
        
        public IIOImage readFrom(Class<IIOImage> type,
                                                         MediaType mediaType,
                                                         MultivaluedMap<String, String> httpHeaders,
                                                         InputStream entityStream)
                        throws IOException {...}
        
        public void writeTo(IIOImage image,
                                            MediaType mediaTypes,
                                            MultivaluedMap<String, Object> httpHeaders,
                                            OutputStream entityStream)
                        throws IOException {...}


Also, the JAX-RS implementation should be able to locate this class
and add it to the runtime. I'm just trying to clarify how this all
should work in practice as it seems a bit vague at the moment. Is
this syntax correct?

Ryan-