users@jersey.java.net

Re: [Jersey] modifying jersey client requests

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 14 Oct 2009 10:58:40 +0200

On Oct 13, 2009, at 11:08 PM, Zoltan Arnold NAGY wrote:
>
>>
>>
>>> and in HasingOutputStream: // baos holds the copy of the stream
>>>
>>> @Override
>>> public void close() throws IOException {
>>> String auth = "xy";
>>> List<Object> x = new ArrayList<Object>();
>>> x.add(auth);
>>> request.getHeaders().put("Authorization", x);
>>>
>>> baos.close();
>>> out.close();
>>> }
>>>
>>> right?
>>>
>>
>> Did you verify that the close method gets called?
>>
>> In the write methods of HasingOutputStream are you also writing to
>> "out"? If so then the request headers will have already been
>> written by the time close has been called so the addition of
>> headers at that point will not make any difference.
>>
>> You are also not writing out the buffered copy, "baos", on close.
>> The implementation should adapt the writes to write to the buffered
>> copy and then on close calculate new headers and write out the
>> buffered copy to "out".
>>
> the code snippet above was a simplified version, not a correct one.
> check the link below for a correct snippet.
>
> but let me say that this is very confusing. If I understand
> correctly, I can only alter the bytestream representation of the
> entity in the adapt() method, after serialization, but when that
> gets called, the headers are already sent, but it seems that's the
> only place where I can actually inpsect the generated bytecode.
>
> I don't see how I could possibly alter the headers from the adapt()
> method.

As long as you do not write to the to the OutputStream that is passed
in to the adapt method you can add headers. Once one or more bytes are
written to that stream the request is committed (headers are written).


>>
>>
>>> currently I'm trying this approach, and server side logging
>>> indicates that the added header never arrives.
>>
>> Perhaps you can send the complete code?
> Sure. Code is here: http://nagyz.pastebin.com/m1e237267
>

That looks good. At line 42 it should be possible to add headers. If
you cannot it is a bug. Can you verify when the client performs a POST
request that the AuthFilter.close() is called?

Paul.