You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by jarmod <ja...@hotmail.com> on 2007/03/15 18:06:19 UTC

Axis 1.4 client get cookies

I've read about 14 million internet posts (OK, it was just 13 million) on the
topic of how an Axis (1.4 in this case) client might retrieve cookies and,
quite surprisingly, I have yet to find a solution that actually works.

My Axis client uses the highest-level abstraction so looks something like
this:

MyServiceLocator locator = new MyServiceLocator();
locator.setMaintainSession(true);
MyService service = locator.getMyService();

service.login("fred", "fredpass"); // server sends me some cookies here
// I want to get those cookies here
service.dosomethingelse();
service.logout();

The code works fine and uses JSESSIONID cookie-based session management
automatically.  However, I want more thanthat -- I want to be able to
retrieve the cookies in the HTTP response associated with the login.

I've tried to retrieve the cookies using
stub._getProperty(HTTPConstants.HEADER_COOKIE) but that returns NULL.  I've
also enumerated all properties of the stub *after* the login call and there
are none.  Can you even do what I want at this interface level?

Appreciate any help.
-- 
View this message in context: http://www.nabble.com/Axis-1.4-client-get-cookies-tf3409630.html#a9499083
Sent from the Axis - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org


Re: Axis 1.4 client get cookies

Posted by dreamryder <Je...@fmr.com>.
Hi,
I am using Axis 1.3, but I got one cookie to work well. (Multiple cookies is
difficult, I will explain why). On the service side, I wrote a handler class
that extends JAXRPCHandler. In the invoke() method I have this:
public void invoke(MessageContext context) throws AxisFault
{    
    if (context.getPastPivot()) { // returning response handler
        context.setMaintainSession(true);
        HttpServletResponse resp =
(HttpServletResponse)context.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE);
    		
        Cookie cookie1 = new Cookie("JSESSIONID", "12345blah");
        cookie1.setComment("Just a comment");
        cookie1.setMaxAge(10000);
        cookie1.setDomain("PaG");
        resp.addCookie(cookie1);
        
        // 2nd cookie fails, because addCookie() overwrites the last entry
in a hashtable somewhere
        // Cookie cookie2 = new Cookie("GROUPID", "09876aaz");
        //resp.addCookie(cookie2);
    }
}

In the client I read the cookie like this:
...
org.apache.axis.client.Call call = stub._getCall();
org.apache.axis.MessageContext msgContext = call.getMessageContext();
String cookieStr =
(String)msgContext.getProperty(HTTPConstants.HEADER_COOKIE);
if (cookieStr != null && !cookieStr.equals(""))
    System.out.println("cookie = " + cookieStr);
...
Notes:
1. The string cookieStr looks like this "JSESSIONID=12345blah" that is, it
contains the cookie name and value appended together with an "=" as a
separator.
2. Use org.apache.axis.utils.tcpmon to watch the response coming back from
the service, it will reveal the cookie line as a HTTP header, like this:
  Set-Cookie: JSESSIONID=1234blah; Domain=pag; Expires=Tue, 05-Jun-2007
18:01:12 GMT
3. Multiple cookies set using addCookie() in the service-side handler merely
causes the entry in the hashtable of properties to be overwritten, thereby
making it extraordinarily difficult to get two or more cookies to work. Most
examples tell you to use setProperty() on the service-side and getProperty()
on the client-side, but I have found the properties do not carry well across
to the client.
4. If you call setMaintainSession(true) when you first get the stub in the
client, then the cookie will be reflected back to the service on the next
web service call.
5. You're not supposed to use cookies for web services anyway. (Cookies
belong at the protocol level). Web services is about stateless SOAP
envelopes, not HTTP header setting and getting, which is what your doing
with cookies) Hence, I doubt Axis will ever improve the cookie handling in
the Axis.1 x code base. What are you supposed to use instead? SOAP headers,
aka WS-Addressing. But, intermediate processing agents (like WebShere or
Weblogic) can't open SOAP headers to route them to a particular server.
-dreamryder





jarmod wrote:
> 
> I believe that I've finally cracked this though I am highly suspicious of
> its complexity -- you'd think that it would be trivial retrieving a
> cookie, and maybe it is but how you do it is not at all obvious.
> 
> Here's what I did:
> 
> MyServiceLocator locator = new MyServiceLocator();
> locator.setMaintainSession(true);
> MyService service = locator.getMyService();
> 
> service.login("fred", "fredpass"); // server sends me some cookies here
> 
> // I want to get those cookies here
> MessageContext ctx = locator.getCall().getMessageContext();
> Message rsp = ctx.getResponseMessage();
> MimeHeaders mhd = rsp.getMimeHeaders();
> String [] cookies = mhd.getHeader("set-cookie");
> if (cookies != null)
> {
>  // parse value of JSESSIONID from cookies[0]
> }
> 
> service.dosomethingelse();
> service.logout();
> 
> 
> 
> jarmod wrote:
>> 
>> I've read about 14 million internet posts (OK, it was just 13 million) on
>> the topic of how an Axis (1.4 in this case) client might retrieve cookies
>> and, quite surprisingly, I have yet to find a solution that actually
>> works.
>> 
>> My Axis client uses the highest-level abstraction so looks something like
>> this:
>> 
>> MyServiceLocator locator = new MyServiceLocator();
>> locator.setMaintainSession(true);
>> MyService service = locator.getMyService();
>> 
>> service.login("fred", "fredpass"); // server sends me some cookies here
>> // I want to get those cookies here
>> service.dosomethingelse();
>> service.logout();
>> 
>> The code works fine and uses JSESSIONID cookie-based session management
>> automatically.  However, I want more thanthat -- I want to be able to
>> retrieve the cookies in the HTTP response associated with the login.
>> 
>> I've tried to retrieve the cookies using
>> stub._getProperty(HTTPConstants.HEADER_COOKIE) but that returns NULL. 
>> I've also enumerated all properties of the stub *after* the login call
>> and there are none.  Can you even do what I want at this interface level?
>> 
>> Appreciate any help.
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Axis-1.4-client-get-cookies-tf3409630.html#a10972133
Sent from the Axis - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org


Re: Axis 1.4 client get cookies

Posted by jarmod <ja...@hotmail.com>.
I believe that I've finally cracked this though I am highly suspicious of its
complexity -- you'd think that it would be trivial retrieving a cookie, and
maybe it is but how you do it is not at all obvious.

Here's what I did:

MyServiceLocator locator = new MyServiceLocator();
locator.setMaintainSession(true);
MyService service = locator.getMyService();

service.login("fred", "fredpass"); // server sends me some cookies here

// I want to get those cookies here
MessageContext ctx = locator.getCall().getMessageContext();
Message rsp = ctx.getResponseMessage();
MimeHeaders mhd = rsp.getMimeHeaders();
String [] cookies = mhd.getHeader("set-cookie");
if (cookies != null)
{
 // parse value of JSESSIONID from cookies[0]
}

service.dosomethingelse();
service.logout();



jarmod wrote:
> 
> I've read about 14 million internet posts (OK, it was just 13 million) on
> the topic of how an Axis (1.4 in this case) client might retrieve cookies
> and, quite surprisingly, I have yet to find a solution that actually
> works.
> 
> My Axis client uses the highest-level abstraction so looks something like
> this:
> 
> MyServiceLocator locator = new MyServiceLocator();
> locator.setMaintainSession(true);
> MyService service = locator.getMyService();
> 
> service.login("fred", "fredpass"); // server sends me some cookies here
> // I want to get those cookies here
> service.dosomethingelse();
> service.logout();
> 
> The code works fine and uses JSESSIONID cookie-based session management
> automatically.  However, I want more thanthat -- I want to be able to
> retrieve the cookies in the HTTP response associated with the login.
> 
> I've tried to retrieve the cookies using
> stub._getProperty(HTTPConstants.HEADER_COOKIE) but that returns NULL. 
> I've also enumerated all properties of the stub *after* the login call and
> there are none.  Can you even do what I want at this interface level?
> 
> Appreciate any help.
> 

-- 
View this message in context: http://www.nabble.com/Axis-1.4-client-get-cookies-tf3409630.html#a9519062
Sent from the Axis - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org