users@jersey.java.net

Re: [Jersey] modifying jersey client requests

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 13 Oct 2009 19:53:26 +0200

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.


> 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".



> currently I'm trying this approach, and server side logging
> indicates that the added header never arrives.

Perhaps you can send the complete code?

Paul.


> If not this, then what's the right way to do it?
>
> Thanks,
> Zoltan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>