users@jersey.java.net

Re: [Jersey] modifying jersey client requests

From: Zoltan Arnold NAGY <Zoltan.Nagy_at_Sun.COM>
Date: Tue, 13 Oct 2009 23:08:41 +0200

Paul Sandoz wrote:
>
> On Oct 13, 2009, at 4:31 PM, Zoltan Arnold NAGY wrote:
>>>>
>>>> Is there a way to catch the output stream before it gets written at
>>>> all to the server?
>>>>
>>>
>>> Yes, that is what a filter is for, you can modify the request headers.
>>> You just need to work out if there is an entity present or not to
>>> decide where the creation of the signed string and header addition
>>> occurs. See ClientRequest.getEntity(). If it returns null then
>>> adaption will not occur, unless another filter further along in the
>>> filter chain adds one (a security filter should really be the last
>>> one in the chain).
>> hmm, so in my own filter I should be able to do sometihng like:
>>
>> public OutputStream adapt(ClientRequest request, OutputStream
>> out) throws IOException {
>> return new HashingOutputStream(request,
>> getAdapter().adapt(request, out));
>> }
>>
>
> + you should only adapt if there is a request entity.
yes.
>
>
>> 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.
>
>
>> 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

Thanks,
Zoltan