users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] Re: Re: Re: Response.getEntity vs Response.readEntity

From: Bill Burke <bburke_at_redhat.com>
Date: Fri, 17 Aug 2012 18:58:34 -0400

On 8/17/2012 3:55 PM, Sergey Beryozkin wrote:
> On 17/08/12 20:48, Marek Potociar wrote:
>>
>> On Aug 17, 2012, at 12:36 PM, Sergey Beryozkin <sberyozkin_at_talend.com
>> <mailto:sberyozkin_at_talend.com>> wrote:
>>
>>> On 17/08/12 11:25, Marek Potociar wrote:
>>>>
>>>> On Aug 16, 2012, at 6:37 PM, Sergey Beryozkin<sberyozkin_at_talend.com
>>>> <mailto:sberyozkin_at_talend.com>> wrote:
>>>>
>>>>> On 16/08/12 17:21, Sergey Beryozkin wrote:
>>>>>> Response.readEntity docs say:
>>>>>>
>>>>>> "A non-null message instance returned from this method will be cached
>>>>>> for subsequent retrievals via getEntity(). Unless the supplied entity
>>>>>> type is an input stream, this method automatically closes the
>>>>>> consumed
>>>>>> response entity stream if it is not buffered. In case he entity input
>>>>>> stream has been buffered, it will be reset when the method returns to
>>>>>> enable subsequent invocations of the readEntity(...) methods on this
>>>>>> response. "
>>>>>>
>>>>>> Suppose we have Response containing unconsumed InputStream.
>>>>>>
>>>>>> I expect that initial Response.getEntity() returns a non-null Object
>>>>>> which is the same as the internal unconsumed InputStream:
>>>>>>
>>>>>> assertTrue(Response.getEntity() instanceof InputStream);
>>>>>>
>>>>>> Next, we do
>>>>>>
>>>>>> Response.bufferEntity();
>>>>>> // assuming a buffered entity is kept in ByteArrayInputStream
>>>>>> assertTrue(Response.getEntity() instanceof InputStream);
>>>>>>
>>>>>> Book book = Response.readEntity(Book.class);
>>>>>>
>>>>>> At this stage, what Response.getEntity() is supposed to return ?
>>>>>> Is it Book ? I think this is what the docs say but I'm not sure it is
>>>>>> right...
>>>>>>
>>>>> If it is indeed Book that is cached then we are ending up with two
>>>>> buffered representations (in the above example and according to the
>>>>> docs), the buffered entity stream (so that
>>>>> Response.readEntity(Book.class) works) and Book instance itself
>>>>
>>>> Is that a problem?
>>>
>>> Can you confirm that when we start with Response containing an
>>> unconsumed InputStream we get InputStream:
>>>
>>> assertTrue(response.getEntity() instanceof InputStream)
>>>
>>> Sergey
>>
>> From Response.getEntity() javadoc:
>> /If the entity is represented by an un-consumed input stream the method
>> will return the input stream./
>>
>> Why would you request a special confirmation for the assertion above,
>> with that sentence in the method javadoc, is beyond me. Anyway - yes, I
>> confirm.
>>
> Thank you. As a side note, I'm finding that you are getting annoyed
> every time I'm trying to make sure things are clear to me (I'm working
> on the tests) and that there're no any ambiguities - I'm not trying to
> be negative - but you seem to be seeing the negativity in my every comment.
>
> To answer your question: I thought for whatever reasons that getEntity()
> might have to return 'null' if the underlying stream was not yet consumed.
>

Thanks Sergey, I would have missed that as my implementation currently
returns null.

So,this is true, right?

Response res = target.request().get();
assertNotNull(res.getEntity());
assertTrue(res.getEntity() instanceof InputStream);

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com