Content-Length is missing after calling setSize() in OutputRepresentation reponse

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Content-Length is missing after calling setSize() in OutputRepresentation reponse

Jakkrit Vongsraluang
Here is my ServerResource response implementation:

return new OutputRepresentation(mediaType) {
                        @Override
                        public void write(OutputStream outputStream) throws IOException {
                                byte[] buffer = new byte[1024];
                               
                                long totalLength = 0;
                                int length;
                                while ((length = inputStream.read(buffer)) > 0) {
                                        outputStream.write(buffer, 0, length);
                                        totalLength += length;
                                }
                                setSize(totalLength);
                        }
                };

The HTTP response does not include Content-Length. But when I construct OutputRepresentation to include expectedSize argument in the constructor, as below, the Content-Length is included in the HTTP response properly.

return new OutputRepresentation(mediaType, length) { ... }

Is this a bug or intended behavior?

Thanks!
Jakkrit Vongsraluang (PaePae)

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2891563
Reply | Threaded
Open this post in threaded view
|

RE: Content-Length is missing after calling setSize() in OutputRepresentation reponse

Christian Wuertz
I've the same problem. If I set the size in the constructor, it will be used as content-length parameter in the HTTP header. But if I use setSize() in the write() method it has no effect and no content-length parameter is added to the HTTP header.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2903401
Reply | Threaded
Open this post in threaded view
|

RE: Content-Length is missing after calling setSize() in OutputRepresentation reponse

jlouvel
Administrator
Hi Jakkrit and Christian,

This is the expected behavior, due to the fact that HTTP requires us to
write the "Content-Length" header before actually writing the representation
content.

If you want to delay the size computation after construction of the object,
you can try to either override the OutputRepresentation#getSize() method or
to leverage the ConnectorService#beforeSend(Representation) method.

Again, this isn't necessary when leveraging chunked encoding.

Best regards,
Jerome
--
http://www.restlet.org
http://twitter.com/#!/jlouvel


-----Message d'origine-----
De : Christian Wuertz [mailto:[hidden email]]
Envoyé : dimanche 1 janvier 2012 10:53
À : [hidden email]
Objet : RE: Content-Length is missing after calling setSize() in
OutputRepresentation reponse

I've the same problem. If I set the size in the constructor, it will be used
as content-length parameter in the HTTP header. But if I use setSize() in
the write() method it has no effect and no content-length parameter is added
to the HTTP header.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=29034
01

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2903571