Sounds like not reporting the size isn't the end of the world so we'll
leave it at -1.
Thanks to all who replied,
Gary
On Wed, Jan 5, 2011 at 5:49 AM, Paul Sandoz <Paul.Sandoz_at_oracle.com> wrote:
> I forgot to say if you really want the size to be declared to the HTTP
> container you can write a ContainerResponseFilter and adapt the
> ContainerResponseWriter to buffer the bytes. You can choose to do this
> per-resource method using a ResourceFilterFactory if you want to be
> selective.
>
> See the source code to com.sun.jersey.api.container.filter.LoggingFilter for
> more details of how to buffer.
>
> Paul.
>
> On Jan 5, 2011, at 11:22 AM, Paul Sandoz wrote:
>
>> Hi Gary,
>>
>> The best practice i would advise is if you know the size before you do any
>> serialization then this method can be used, otherwise return -1 and let the
>> HTTP container do its thing (e.g. buffer or use chunked transfer encoding).
>> A good example where getSize can return a non-negative integer is the
>> serializing of byte[] or File.
>>
>> I would not recommend doing work in the getSize method and caching.
>> Although the runtime will call getSize before writeTo it is not specified
>> that getSize should be called just once or that writeTo will be called after
>> getSize is called (and if so it is not stated that it will be on the same
>> thread). For example, the application developer can inject Providers and
>> from that obtain a MessageBodyWriter and do what ever it wants with it.
>>
>> The following is likely to be implementation specific to the HTTP
>> container that is utilized. In the writeTo method you could buffer the
>> serialization in byte[] then set the Content-Length header. You need to set
>> this header before any bytes are written to the output stream, since when
>> the first byte is written the headers are committed.
>>
>> Hth,
>> Paul.
>>
>>
>> On Jan 5, 2011, at 12:17 AM, Gary Moore wrote:
>>
>>> Right now, we're doing a lot of this in our MessageBodyWriter(s):
>>>
>>> @Override
>>> public long getSize(PropValue value, Class<?> type, Type type1,
>>> Annotation[] annotations, MediaType mediaType) {
>>> //TODO: Do this
>>> return -1L;
>>> }
>>>
>>> The problem is that we're doing some objects->byte conversions in the
>>> MessageBodyWriter.writeTo method. The docs for MessageBodyWriter say
>>> getSize is called before writeTo, so to respond properly to getSize,
>>> we have to do the objects->byte conversions twice, once to get the
>>> size and another time to actually write it out.
>>>
>>> What is the Jersey lifecycle of MessageBodyWriters? Is it safe to do
>>> the converting in the getSize method, then save the result for use in
>>> the writeTo method?
>>>
>>> Thanks,
>>> Gary
>>>
>>> --
>>> Gary Moore
>>> http://athena.fracturedatlas.org
>>> http://www.gmoore.net
>>
>
>
--
Gary Moore
http://www.gmoore.net