users@jersey.java.net

Re: [Jersey] MessageBodyWriter for multiple media types

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Fri, 6 Aug 2010 11:21:49 +0200

Hi Christian,

A message body writer can support any set of media types. If you do
not declare a @Produces then it defaults to @Produces("*/*"). Multiple
media types can be present in the @Produces just like then can when
that is used on a resource method.

It seems like in this case you want to support content negotiation at
the level of the message body writer (MBW) and not at the level of
@Produces.

In your message body writer you can inject HttpHeaders from which you
can get the acceptable media types and utilize those media types to
select the most appropriate template if it exists or return a 406 by
throwing a WebApplicationException.

In the MBW you can set the response headers like Content-Type but make
sure you do that before you write any bytes to the OutputStream as
once the first byte is written the response headers will be committed.


Perhaps a simpler approach is to allow the @Produces/_at_ImpliciProduces
to be declared on the resource methods and then utilize the content-
type header (the most acceptable media type selected by Jersey) to
look up the appropriate template using the configuration you proposed.
If such a template cannot be found then you can return a 406. This is
appealing because the resources still describe what they produce and
the developer does not have to look as suffixes of template files to
work this out.

Ideally i would like to support this type of concept at the level of
the ViewProcessor with an appropriate convention for prefixes. I think
this may be possible with the above proposed simple approach.

Paul.


On Aug 4, 2010, at 10:33 PM, Christian Helmbold wrote:

> I want to write a MessageBodyWriter to render Viewable instances
> with FreeMarker
> templates. Usually the media type would be html, but FreeMarker
> templates could
> be used to render almost any media type. My idea is to use file
> extensions to
> specify what media type will be rendered by a template, for example
> "template.html", "template.txt" and so on. In principle this could
> be done with
> a single MessageBodyWriter that processes the templates. But ...
>
> * Is it possible that a MessageBodyWriter contains multiple media
> types in the
> @Produces annotation?
>
> * Since it would be a configuration of the actual environment, the
> @Produces
> annotation couldn't be placed on a general MessageBodyWriter class.
> Do I have to
> subclass my FreeMarker MessageBodyWriter for each concrete
> configuration only to
> add a @Produces annotation? Is there another way to tell what media
> types could
> be produced?
>
> * Would it be appropriate to throw a WebApplicationException(406) (Not
> Acceptable) if the FreeMarkerMessageBodyWriter tries to render an
> entity but
> there is no matching template? The FreeMarkerMessageBodyWriter could
> for example
> say that it is able to produce "text/plain", but then there is no
> template for
> the given entity. Would it be possible to resume the lookup for an
> appropriate
> MessageBodyWriter then?
>
>
> Regards
> Christian
>
> --
> http://scala-forum.org/
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>