Re: JSR311: Re: ResponseBuilder

From: Stephan Koops <>
Date: Fri, 13 Jun 2008 15:32:39 +0200


Marc Hadley schrieb:
> Bill Burke wrote:
>> type(MediaType)
>> type(String)
>> language(Locale)
>> language(String)
> +1 to having both variants.
also +1
> The issue, if I understood correctly, is what type to expect in a
> message body writer if you get the value of a header from the
> httpHeaders parameter.
yes Marc, exactly.
A problem is, that one resource method could give a String, but another
method (perhaps in another class) could give a MediaType. So the message
body writer must check if it is not null, must check the type and
perhaps do a conversion. If the message body writer ever uses
toString(), than you get a NPE, if the value isn't set.

So you have to use the following code in the message body writer:

MediaType mediaType;
Object mt = httpHeaders.getFirst("Content-Type");
if(mt == null)
    mediaType = null;
else if (mt instanceof MediaType)
    mediaType = (MediaType)mt;
    mediaType = MediaType.valueOf(mt.toString());

This is a silly.
It is nice, if I ever get the higher level object, but for pratical
reasons, it could be good to return ever a String (or null). ->
MultivaluedMap<String, String> httpHeaders. The back conversion could be
done by the valueOf methods.
BTW: What should a HeaderDelegate do with a given null (in
valueOf(String) and in toString(T) )? It's not explicit defined in the
javadoc. For this case it is useful to allow null and return null than,
and not throw an IllegalArgumentException.

best regards