michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.cookie; michael@0: michael@0: import java.io.Serializable; michael@0: import java.util.Date; michael@0: import java.util.HashMap; michael@0: import java.util.Locale; michael@0: import java.util.Map; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.cookie.ClientCookie; michael@0: import ch.boye.httpclientandroidlib.cookie.SetCookie; michael@0: michael@0: /** michael@0: * Default implementation of {@link SetCookie}. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe michael@0: public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable { michael@0: michael@0: private static final long serialVersionUID = -3869795591041535538L; michael@0: michael@0: /** michael@0: * Default Constructor taking a name and a value. The value may be null. michael@0: * michael@0: * @param name The name. michael@0: * @param value The value. michael@0: */ michael@0: public BasicClientCookie(final String name, final String value) { michael@0: super(); michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Name may not be null"); michael@0: } michael@0: this.name = name; michael@0: this.attribs = new HashMap(); michael@0: this.value = value; michael@0: } michael@0: michael@0: /** michael@0: * Returns the name. michael@0: * michael@0: * @return String name The name michael@0: */ michael@0: public String getName() { michael@0: return this.name; michael@0: } michael@0: michael@0: /** michael@0: * Returns the value. michael@0: * michael@0: * @return String value The current value. michael@0: */ michael@0: public String getValue() { michael@0: return this.value; michael@0: } michael@0: michael@0: /** michael@0: * Sets the value michael@0: * michael@0: * @param value michael@0: */ michael@0: public void setValue(final String value) { michael@0: this.value = value; michael@0: } michael@0: michael@0: /** michael@0: * Returns the comment describing the purpose of this cookie, or michael@0: * null if no such comment has been defined. michael@0: * michael@0: * @return comment michael@0: * michael@0: * @see #setComment(String) michael@0: */ michael@0: public String getComment() { michael@0: return cookieComment; michael@0: } michael@0: michael@0: /** michael@0: * If a user agent (web browser) presents this cookie to a user, the michael@0: * cookie's purpose will be described using this comment. michael@0: * michael@0: * @param comment michael@0: * michael@0: * @see #getComment() michael@0: */ michael@0: public void setComment(String comment) { michael@0: cookieComment = comment; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns null. Cookies prior to RFC2965 do not set this attribute michael@0: */ michael@0: public String getCommentURL() { michael@0: return null; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns the expiration {@link Date} of the cookie, or null michael@0: * if none exists. michael@0: *

Note: the object returned by this method is michael@0: * considered immutable. Changing it (e.g. using setTime()) could result michael@0: * in undefined behaviour. Do so at your peril.

michael@0: * @return Expiration {@link Date}, or null. michael@0: * michael@0: * @see #setExpiryDate(java.util.Date) michael@0: * michael@0: */ michael@0: public Date getExpiryDate() { michael@0: return cookieExpiryDate; michael@0: } michael@0: michael@0: /** michael@0: * Sets expiration date. michael@0: *

Note: the object returned by this method is considered michael@0: * immutable. Changing it (e.g. using setTime()) could result in undefined michael@0: * behaviour. Do so at your peril.

michael@0: * michael@0: * @param expiryDate the {@link Date} after which this cookie is no longer valid. michael@0: * michael@0: * @see #getExpiryDate michael@0: * michael@0: */ michael@0: public void setExpiryDate (Date expiryDate) { michael@0: cookieExpiryDate = expiryDate; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns false if the cookie should be discarded at the end michael@0: * of the "session"; true otherwise. michael@0: * michael@0: * @return false if the cookie should be discarded at the end michael@0: * of the "session"; true otherwise michael@0: */ michael@0: public boolean isPersistent() { michael@0: return (null != cookieExpiryDate); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns domain attribute of the cookie. michael@0: * michael@0: * @return the value of the domain attribute michael@0: * michael@0: * @see #setDomain(java.lang.String) michael@0: */ michael@0: public String getDomain() { michael@0: return cookieDomain; michael@0: } michael@0: michael@0: /** michael@0: * Sets the domain attribute. michael@0: * michael@0: * @param domain The value of the domain attribute michael@0: * michael@0: * @see #getDomain michael@0: */ michael@0: public void setDomain(String domain) { michael@0: if (domain != null) { michael@0: cookieDomain = domain.toLowerCase(Locale.ENGLISH); michael@0: } else { michael@0: cookieDomain = null; michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns the path attribute of the cookie michael@0: * michael@0: * @return The value of the path attribute. michael@0: * michael@0: * @see #setPath(java.lang.String) michael@0: */ michael@0: public String getPath() { michael@0: return cookiePath; michael@0: } michael@0: michael@0: /** michael@0: * Sets the path attribute. michael@0: * michael@0: * @param path The value of the path attribute michael@0: * michael@0: * @see #getPath michael@0: * michael@0: */ michael@0: public void setPath(String path) { michael@0: cookiePath = path; michael@0: } michael@0: michael@0: /** michael@0: * @return true if this cookie should only be sent over secure connections. michael@0: * @see #setSecure(boolean) michael@0: */ michael@0: public boolean isSecure() { michael@0: return isSecure; michael@0: } michael@0: michael@0: /** michael@0: * Sets the secure attribute of the cookie. michael@0: *

michael@0: * When true the cookie should only be sent michael@0: * using a secure protocol (https). This should only be set when michael@0: * the cookie's originating server used a secure protocol to set the michael@0: * cookie's value. michael@0: * michael@0: * @param secure The value of the secure attribute michael@0: * michael@0: * @see #isSecure() michael@0: */ michael@0: public void setSecure (boolean secure) { michael@0: isSecure = secure; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns null. Cookies prior to RFC2965 do not set this attribute michael@0: */ michael@0: public int[] getPorts() { michael@0: return null; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns the version of the cookie specification to which this michael@0: * cookie conforms. michael@0: * michael@0: * @return the version of the cookie. michael@0: * michael@0: * @see #setVersion(int) michael@0: * michael@0: */ michael@0: public int getVersion() { michael@0: return cookieVersion; michael@0: } michael@0: michael@0: /** michael@0: * Sets the version of the cookie specification to which this michael@0: * cookie conforms. michael@0: * michael@0: * @param version the version of the cookie. michael@0: * michael@0: * @see #getVersion michael@0: */ michael@0: public void setVersion(int version) { michael@0: cookieVersion = version; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if this cookie has expired. michael@0: * @param date Current time michael@0: * michael@0: * @return true if the cookie has expired. michael@0: */ michael@0: public boolean isExpired(final Date date) { michael@0: if (date == null) { michael@0: throw new IllegalArgumentException("Date may not be null"); michael@0: } michael@0: return (cookieExpiryDate != null michael@0: && cookieExpiryDate.getTime() <= date.getTime()); michael@0: } michael@0: michael@0: public void setAttribute(final String name, final String value) { michael@0: this.attribs.put(name, value); michael@0: } michael@0: michael@0: public String getAttribute(final String name) { michael@0: return this.attribs.get(name); michael@0: } michael@0: michael@0: public boolean containsAttribute(final String name) { michael@0: return this.attribs.get(name) != null; michael@0: } michael@0: michael@0: @Override michael@0: public Object clone() throws CloneNotSupportedException { michael@0: BasicClientCookie clone = (BasicClientCookie) super.clone(); michael@0: clone.attribs = new HashMap(this.attribs); michael@0: return clone; michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: StringBuilder buffer = new StringBuilder(); michael@0: buffer.append("[version: "); michael@0: buffer.append(Integer.toString(this.cookieVersion)); michael@0: buffer.append("]"); michael@0: buffer.append("[name: "); michael@0: buffer.append(this.name); michael@0: buffer.append("]"); michael@0: buffer.append("[value: "); michael@0: buffer.append(this.value); michael@0: buffer.append("]"); michael@0: buffer.append("[domain: "); michael@0: buffer.append(this.cookieDomain); michael@0: buffer.append("]"); michael@0: buffer.append("[path: "); michael@0: buffer.append(this.cookiePath); michael@0: buffer.append("]"); michael@0: buffer.append("[expiry: "); michael@0: buffer.append(this.cookieExpiryDate); michael@0: buffer.append("]"); michael@0: return buffer.toString(); michael@0: } michael@0: michael@0: // ----------------------------------------------------- Instance Variables michael@0: michael@0: /** Cookie name */ michael@0: private final String name; michael@0: michael@0: /** Cookie attributes as specified by the origin server */ michael@0: private Map attribs; michael@0: michael@0: /** Cookie value */ michael@0: private String value; michael@0: michael@0: /** Comment attribute. */ michael@0: private String cookieComment; michael@0: michael@0: /** Domain attribute. */ michael@0: private String cookieDomain; michael@0: michael@0: /** Expiration {@link Date}. */ michael@0: private Date cookieExpiryDate; michael@0: michael@0: /** Path attribute. */ michael@0: private String cookiePath; michael@0: michael@0: /** My secure flag. */ michael@0: private boolean isSecure; michael@0: michael@0: /** The version of the cookie specification I was created from. */ michael@0: private int cookieVersion; michael@0: michael@0: } michael@0: