public void addCookie(Cookie cookie) {
// Do we already have a cookie stored with the same
// name, path and domain values as the cookie we are trying
// to add?
Cookie storedCookieWithMatchingNamePathAndDomain =
getCookieWithMatchingNamePathAndDomain(cookie);
if (storedCookieWithMatchingNamePathAndDomain != null) {
// Handle the requirement of RFC 2109, Section 4.3.3, i.e
// If a user agent receives a Set-Cookie response header
// whose NAME is the same as a pre-existing cookie, and whose
// Domain and Path attribute values exactly (string) match those
// of a pre-existing cookie, the new cookie supersedes the old.
// However, if the Set-Cookie has a value for Max-Age of zero,
// the (old and new) cookie is discarded.
cookieJar.remove(
storedCookieWithMatchingNamePathAndDomain.getIdentity());
addCookieImpl(cookie);
} else {
// we dont have any existing cookies with the same name so
// simply add the supplied cookie to the jar if the max age
// is greater than zero.