users@jsr311.java.net

Re: MediaType.isComaptible

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 14 Aug 2008 12:46:31 +0200

Reto Bachmann-Gmür wrote:
> Paul Sandoz wrote:
>>
>> Or the Accept header could be:
>> Accept: image/jpeg
>>
>> and i could have a resource method:
>>
>> @Produces("image/*")
>> public Response getGeneralImage() { ... }
>>
>> Changing this method to be asymmetric will essentially render it useless.
>
> With the accept header image/jpeg a producer of image/jpeg is to be
> preferred over image/jpeg, so you could first iterate over the candidate
> producer (sorting them by q-value) checking if there is a subtype of
> image/jpeg if no match look for exactly image/* and then */*. Returning
> a subtype (where a type is a subtype of itself) is best, using a generic
> producer is a fall-back.
>

The spec says that media types for resource methods or readers/writers
are sorted before comparison. For readers/writers see section 4.3.2:

   When choosing an entity provider an implementation sorts the available
   providers according to the media types they declare support for.
   Sorting of media types follows the general rule: x/y < x/* < */*, i.e.
   a provider that explicitly lists a media types is sorted before a
   provider that lists */*. Quality parameter values are also used such
   that x/y;q=1.0 < x/y;q=0.7.


> Looking at this, I realize that it's no clear to me, what
> wildchar-producer are supposed to do if they cannot produce the
> requested concrete type, say we have a BodyWriter with
> Produces("image/*") able to produce many image formats and one with
> Produces("images/png") and a request with
>
> Accept: image/x-weird-format, image/png;q=.9
>
> the BodyWriter for image/* matches image/x-weird-format but will fail to
> produce that format and throw an exception. Had the mediatype been
> passed to isWriteable the BodyWriter could have detected that it doesn't
> support image/x-weird-format allowing the producer for next accepted
> format to be used.
>

The wild card reader can get access to the ordered list of acceptable
media types (via HttpHeaders) so could choose another media type if the
most acceptable media type is not supported. This presumes that such a
reader would support common formats, like image/png in your example.

If we want the isReadable/isWriteable methods to have more information
then they probably require all parameters of the readFrom/writeTo
methods, minus those for the Input/OutputStream.

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109