users@jersey.java.net

Re: [Jersey] How to assign MessageBodyReader to accept another MIME

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 09 Oct 2009 09:19:47 -0400

Another approach would be to write a KML MessageBody[Reader|Writer]
that uses the Providers[1] interface to locate the built in reader/
writer and defer to that to do the actual work:

@Provider
@Consumes("application/vnd.google-earth")
public static final class KmlReader implements
MessageBodyReader<KmlType> {

   @Context Providers providers;

   public boolean isReadable(Class<?> type, Type genericType,
Annotation annotations[],
       MediaType mediaType) {
       return type == KmlType.class;
   }

   KmlType readFrom(java.lang.Class<T> type, java.lang.reflect.Type
genericType,
           java.lang.annotation.Annotation[] annotations, MediaType
mediaType,
           MultivaluedMap<java.lang.String,java.lang.String>
httpHeaders,
           java.io.InputStream entityStream) {
       MessageBodyReader<KmlType> realReader =
providers.getMessageBodyReader(type, genericType, annotations,
MediaType.APPLICATION_XML_TYPE);
       return realReader.readFrom(type, genericType, annotations,
MediaType.APPLICATION_XML_TYPE, httpHeaders, entityStream);
   }

Marc.

[1] https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/ext/Providers.html


On Oct 9, 2009, at 2:12 AM, Paul Sandoz wrote:

> Hi Gene,
>
> The problem is that JAX-RS specifies that JAXB types are supported
> for the following media types: text/xml, application/xml and
> application/⋆+xml.
>
> You can use a client filter to modify the content-type to
> "application/vnd.google-earth+xml" or to "application/xml".
>
> An alternative is to register the following provider, but i do not
> recommend this because it depends on the non-API class
> XMLRootElementProvider (which perhaps indicates we should move this
> to the API?):
>
> @Provider
> @Produces("application/vnd.google-earth")
> @Consumes("application/vnd.google-earth")
> public static final class KmlProvider extends
> XMLRootElementProvider {
>
> public KmlProvider(@Context Injectable<SAXParserFactory> spf,
> @Context Providers ps) {
> super(spf, ps, MediaType.valueOf("application/vnd.google-
> earth"));
> }
>
> public boolean isReadable(Class<?> type, Type genericType,
> Annotation annotations[],
> MediaType mediaType) {
> return type == KmlType.class;
> }
>
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation annotations[],
> MediaType mediaType) {
> return type == KmlType.class;
> }
>
> }
>
> Paul.
>
> On Oct 9, 2009, at 1:45 AM, Turner, George wrote:
>
>> Many times service providers output multiple content types for
>> xml. It is usually plain/text, application/xml, etc, etc.
>> KML is output as application/vnd.google-earth, but to a JAXB
>> unmarshaller, it is really just application/xml.
>> What I am looking for is a way to do this:
>>
>> ClientConfig config = new DefaultClientConfig();
>> Client client = Client.create(config);
>> WebResource webResource = client.resource(url);
>> KmlType kml = webResource.
>> accept("plain/text", "application/xml", "application/
>> vnd.google-earth").get(KmlType.class);
>>
>> And not get the ANNOYING error message that a message body reader
>> has not been registered for KmlType and application/vnd.google-earth.
>>
>> Any help is GREATLY appreciated.
>>
>> Thanks
>>
>> Gene
>>
>> George (Gene) Turner
>> <image001.png>
>> Senior Staff Software Engineer
>> Information Systems & Global Services
>> Work:(719) 277-5244 Cell:(719) 237-0490
>> george.turner_at_lmco.com
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>