comments (not for humans)
You may sometimes need to set or retrieve cookies on axis connections to transfer authentication tokens or similar.

Setting a cookie on an axis client request
To set a cookie on an axis client, you can add a handler. Below is source code for a handler, that fetches the currently incoming HttpServletRequest (say to a web site), extracts a cookie and puts the cookie on the axis request (to the web service).
public class CookieHandler extends BasicHandler {
public void invoke(MessageContext context) throws AxisFault {
HttpServletRequest request = someMethodReturningCurrentRequest();
Cookie cookies[] = request.getCookies();
Cookie cookie = null;
for (int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
if (name.equals("cookiename")) {
cookie = cookies[i];
}
}
if (cookie != null) {
context.setProperty(HTTPConstants.HEADER_COOKIE,
cookie.getName() + "=" + cookie.getValue());
}
}
}

To make the axis client use this cookie, we have to add it (or create) a client-config.wsdd. This file should reside in the classes directory in you war-archive.
client-config.wsdd:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<requestFlow>
<handler type="java:com.abb.commons.contentcockpit.servlet.CookieHandler"/>
</requestFlow>
</globalConfiguration>
<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>


To make axis actually use cookies, MaintainSession has to be set to true (I discovered this reading the source code of HttpHandler - search for "don't forget the cookies....mmm..cookies" :-) ):
((MyServiceStub) myService).setMaintainSession(true);
Retrieving a cookie on a web service call
The following code retrieves a cookie from an incoming call to web service (SOAP/HTTP):
MessageContext context = MessageContext.getCurrentContext();
if (context != null) {
HttpServletRequest req = (HttpServletRequest) context
.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
if (req != null) {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("cookiename")) {
//do something with cookie
}
}
}
}
}

Joe Solinsky

Thank you for writing this

I had to address this exact challenge. Thank you for breaking new ground. Ik zou zonder u scheel zijn!(Dutch)
Lao Kaa

Nice web site

Very many thanks for a good work. Nice and useful. Like it!
Lapsang Klu

Thank you very much for such a good work

Very many thanks for a good work. Nice and useful. Like it!
Lukasz

I don't understand

I don't understand how in method:
<code>someMethodReturningCurrentRequest()</code>
i can get HttpServletRequest?
Class CookieHandler is not connected with any Servlet or Filter - how to pass request?
Erlend

Re: I don't understand

How to get hold of the current HttpServletRequest depends on the framework. Usually there is a context object with a static getter which also contains a reference to the HttpServletRequest.
For WebWork this line should be:
HttpServletRequest request = ServletActionContext.getRequest()
Marchant

Setting a cookie in HTTP response

I observer the following behaviour. If I enabled cookies in Axis (setMaintainSession set to true) I was still unable to set my own cookie using MessageContext.setProperty(HTTPConstants.HEADER_COOKIE,"name=value") - it was not passed to the underlying HttpServletResponse.

However, it worked when I accessed the HttpServletResponse directly (via MessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE)) and used addCookie() directly.

When I set session maintenance to false I was unable to use cookies again.

This looks a bit weird to me but as the client worked I leave this issue. However, such a response(with multiple Set-Cookie lines) may be mistreated by some less robust HTTP clients.
venu

know how

Hi Marchant,
I am also facing the same problem. I am not able to set cookies in axis2. could you please elaborate the solution? What should i do after calling getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE) ? How to add cookie to it ?
Cedric Boudet
Easy and efficient ...

Thanks mate
anonymous
HttpServletRequest) context
.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST) always returns me null.pls help me
anon
this info might help some people who cannot retrieve the cookie from the web service response: http://wso2.org/library/3464
Comments closed for this post