The following example shows a sample CookieContainer class. You can use this as a model for your own class. Note that:
This class relies on the Axis API. If you are using a different Java client, you will need to use the API for your client.
This class works with both static and dynamic Web Service calls. (See Calling ATG Web Services from a Java Client.) If you always make only one of these call types, your own
CookieContainerclass does not need to handle both cases.
package com.example.webservice;
import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Stub;
import org.apache.axis.transport.http.HTTPConstants;
/**
* A class that can be passed between web service clients that keeps track of
* the cookies received from the server. These cookies are then used by
* subsequent web service client calls in order to ensure that session
* state is maintained.
**/
public class CookieContainer
{
//-------------------------------------
// Member variables
//-------------------------------------
/** Array of cookies from the Set-Cookie HTTP header **/
private String[] mCookies = null;
/** Array of cookies from the Set-Cookie2 HTTP header **/
private String[] mCookies2 = null;
//-------------------------------------
// Methods
//-------------------------------------
/**
* Gets the cookies set by the Set-Cookie HTTP header
* @return the cookies from the Set-Cookie HTTP header, which
* may be null
**/
public String[] getCookies() {
return mCookies;
}
/**
* Gets the cookies set by the Set-Cookie2 HTTP header
* @return the cookies from the Set-Cookie2 HTTP header, which
* may be null
**/
public String[] getCookies2() {
return mCookies2;
}
/**
* Extracts the cookies from the given Axis MessageContext, and
* sets the cookies and cookies2 properties from them.
* @param pContext the Axis message context to examine. This
* cannot be null
**/
public void extractCookies(MessageContext pContext) {
mCookies = (String[])pContext.getProperty
(HTTPConstants.HEADER_COOKIE);
mCookies2 = (String[])pContext.getProperty
(HTTPConstants.HEADER_COOKIE2);
}
/**
* Extracts the cookies from the given Axis Call, and
* sets the cookies and cookies2 properties from them.
* @param pCall the Axis call to examine. This
* cannot be null
**/
public void extractCookies(Call pCall) {
extractCookies(pCall.getMessageContext());
}
/**
* Extracts the cookies from the given Axis Stub, and
* sets the cookies and cookies2 properties from them.
* @param pStub the Axis stub to examine. This
* cannot be null
**/
public void extractCookies(Stub pStub) {
extractCookies(pStub._getCall());
}
/**
* Pushes the cookie values that are set on the instance to
* the given Call
* @param pCall the call to set the cookies on. This cannot be null
**/
public void pushCookies(Call pCall) {
if(mCookies != null)
pCall.setProperty(HTTPConstants.HEADER_COOKIE, mCookies);
if(mCookies2 != null)
pCall.setProperty(HTTPConstants.HEADER_COOKIE2, mCookies2);
}
/**
* Pushes the cookie values that are set on the instance to
* the given Stub
* @param pStub the stub to set the cookies on. This cannot be null
**/
public void pushCookies(Stub pStub) {
if(mCookies != null)
pStub._setProperty(HTTPConstants.HEADER_COOKIE, mCookies);
if(mCookies2 != null)
pStub._setProperty(HTTPConstants.HEADER_COOKIE2, mCookies2);
}
}
