You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by co...@cyberdeck.org on 2010/03/12 16:46:37 UTC

Setting cookies from a Handler

I'm able to retrieve cookies by over-riding doPost() in my
XmlRpcServlet and reading them from the HttpServletRequest, but I'm unable to
figure out the best method for adding cookies to the outgoing response.

I particularly want to be able to add them inside my Handler.

The only way I can think of is to setup a ThreadLocal in my servlet that
houses the HttpServletResponse passed to doPost, and then accessing that
in my Handler, but that seems extremely kludgy.  I'd rather be able to
override some "processResponse()" type method in my servlet and muck about
with the response there.

Any best practices for this?

Thank you,

-Steve

Re: Setting cookies from a Handler

Posted by Count Zero <co...@cyberdeck.org>.
FYI: the cookie documentation at 
http://ws.apache.org/xmlrpc/advanced.html could use some modification.

Specifically, cookie handling in the client is significantly easier than 
what is described if one can make use of the 
XmlRpcSun15HttpTransportFactory (and associated transport), since both 
will take advantage of the default CookieHandler.

Sample code (similar to the existing example):

final XmlRpcClient client = new XmlRpcClient();

CookieManager cookiemanager = new CookieManager();

// set a permissive cookiepolicy for testing, real implementations will 
likely prefer
// CookiePolicy.ACCEPT_ORIGINAL_SERVER
cookiemanager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookiemanager);

XmlRpcTransportFactory tFactory = new 
XmlRpcSun15HttpTransportFactory(client);
client.setTransportFactory(tFactory);

This will store cookies set by the server, and return them in requests 
back to that server (following normal cookie handling rules).

-Steve

On 3/12/2010 10:32 PM, Count Zero wrote:
> I've implemented it via the ThreadLocal method I describe below, and 
> it works well enough.
>
> I still feel like this is a bit of a kludge though...  If anyone has a 
> more elegant solution, I'd love to hear it.
>
> Thank you,
>
> -Steve
>
> On 3/12/2010 10:46 AM, countzero@cyberdeck.org wrote:
>> I'm able to retrieve cookies by over-riding doPost() in my
>> XmlRpcServlet and reading them from the HttpServletRequest, but I'm 
>> unable to
>> figure out the best method for adding cookies to the outgoing response.
>>
>> I particularly want to be able to add them inside my Handler.
>>
>> The only way I can think of is to setup a ThreadLocal in my servlet that
>> houses the HttpServletResponse passed to doPost, and then accessing that
>> in my Handler, but that seems extremely kludgy.  I'd rather be able to
>> override some "processResponse()" type method in my servlet and muck 
>> about
>> with the response there.
>>
>> Any best practices for this?
>>
>> Thank you,
>>
>> -Steve
>>
>

Re: Setting cookies from a Handler

Posted by Count Zero <co...@cyberdeck.org>.
I've implemented it via the ThreadLocal method I describe below, and it 
works well enough.

I still feel like this is a bit of a kludge though...  If anyone has a 
more elegant solution, I'd love to hear it.

Thank you,

-Steve

On 3/12/2010 10:46 AM, countzero@cyberdeck.org wrote:
> I'm able to retrieve cookies by over-riding doPost() in my
> XmlRpcServlet and reading them from the HttpServletRequest, but I'm unable to
> figure out the best method for adding cookies to the outgoing response.
>
> I particularly want to be able to add them inside my Handler.
>
> The only way I can think of is to setup a ThreadLocal in my servlet that
> houses the HttpServletResponse passed to doPost, and then accessing that
> in my Handler, but that seems extremely kludgy.  I'd rather be able to
> override some "processResponse()" type method in my servlet and muck about
> with the response there.
>
> Any best practices for this?
>
> Thank you,
>
> -Steve
>
>