users@jersey.java.net

Re: Does Jersey set content-length header field automatically ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 16 Oct 2007 15:43:25 +0200

Jakub Podlesak wrote:
> Hi James,
>
> could you please file a RFE [1]?
> I think that "Content-Length" header is not being
> set automatically.
>
> You can use [Response.Builder#header] method
> to set the header manually on the server side.
>

The content header should not be set like that unless the application
really knows the length in bytes of the serialized of the entity, which
it will not know when using JAXB types unless it serializes and then it
might as well return the bytes.

The Jersey runtime relies on the underlying continer to support either
chunked transfer encoding [1]; or buffering to determine the length of
content.

When using the HttpURLConnection a -1 returned from a call to
getContentLength() indicates that there may be (i suspect that should be
an 'is' instead of a 'may be' but the JavaDoc is vague) some content but
the length is not known. So you need to call getInputStream() as in the
case for when getContentLength() > 0 if you are expecting content to be
returned.

Paul.

[1]
http://www.greenbytes.com/tech/webdav/rfc2616.html#chunked.transfer.encoding

> Thanks,
>
> ~Jakub
>
> [1]https://jersey.dev.java.net/issues/
>
>
>
> On Tue, Oct 16, 2007 at 10:11:17AM +0200, James Weir wrote:
>> Hi,
>>
>> Just created some client code using HttpURLConnection. I do the
>> following check on the "content-length" header ....
>>
>> HttpURLConnection uc = null;
>>
>> ....// i do the request here
>>
>> if (uc.getContentLength()<=0) {
>> logger.error("Reply from the the Web Service has " +
>> uc.getContentLength() + " bytes");
>> // Some sort of error has occurred
>> super.setErrorCode(-1);
>> super.setErrorMessage("Unable to get the user "+userName);
>>
>> // Depending upon the Http Response set the correct
>> // response message
>> super.setResponseMsg("User not found");
>> return(null);
>> }
>>
>> The server correctly returns JAXB information in the body, however it
>> seems that the "content-length" header field is not set. Is this
>> something I need to do explicitly when building the message on the
>> server side ?. I was hoping that the REST API would set this for me, or
>> am I being too lazy here :)
>>
>> Kind Regards
>> James
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

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