cookie.java

来自「Light in the box 抓取程序。 使用HttpClient」· Java 代码 · 共 537 行 · 第 1/2 页

JAVA
537
字号
        return isSecure;    }    /**     * Sets the secure attribute of the cookie.     * <p>     * When <tt>true</tt> the cookie should only be sent     * using a secure protocol (https).  This should only be set when     * the cookie's originating server used a secure protocol to set the     * cookie's value.     *     * @param secure The value of the secure attribute     *      * @see #getSecure()     */    public void setSecure (boolean secure) {        isSecure = secure;    }    /**     * Returns the version of the cookie specification to which this     * cookie conforms.     *     * @return the version of the cookie.     *      * @see #setVersion(int)     *     */    public int getVersion() {        return cookieVersion;    }    /**     * Sets the version of the cookie specification to which this     * cookie conforms.      *     * @param version the version of the cookie.     *      * @see #getVersion     */    public void setVersion(int version) {        cookieVersion = version;    }    /**     * Returns true if this cookie has expired.     *      * @return <tt>true</tt> if the cookie has expired.     */    public boolean isExpired() {        return (cookieExpiryDate != null              && cookieExpiryDate.getTime() <= System.currentTimeMillis());    }    /**     * Returns true if this cookie has expired according to the time passed in.     *      * @param now The current time.     *      * @return <tt>true</tt> if the cookie expired.     */    public boolean isExpired(Date now) {        return (cookieExpiryDate != null              && cookieExpiryDate.getTime() <= now.getTime());    }    /**     * Indicates whether the cookie had a path specified in a      * path attribute of the <tt>Set-Cookie</tt> header. This value     * is important for generating the <tt>Cookie</tt> header because      * some cookie specifications require that the <tt>Cookie</tt> header      * should only include a path attribute if the cookie's path      * was specified in the <tt>Set-Cookie</tt> header.     *     * @param value <tt>true</tt> if the cookie's path was explicitly      * set, <tt>false</tt> otherwise.     *      * @see #isPathAttributeSpecified     */    public void setPathAttributeSpecified(boolean value) {        hasPathAttribute = value;    }    /**     * Returns <tt>true</tt> if cookie's path was set via a path attribute     * in the <tt>Set-Cookie</tt> header.     *     * @return value <tt>true</tt> if the cookie's path was explicitly      * set, <tt>false</tt> otherwise.     *      * @see #setPathAttributeSpecified     */    public boolean isPathAttributeSpecified() {        return hasPathAttribute;    }    /**     * Indicates whether the cookie had a domain specified in a      * domain attribute of the <tt>Set-Cookie</tt> header. This value     * is important for generating the <tt>Cookie</tt> header because      * some cookie specifications require that the <tt>Cookie</tt> header      * should only include a domain attribute if the cookie's domain      * was specified in the <tt>Set-Cookie</tt> header.     *     * @param value <tt>true</tt> if the cookie's domain was explicitly      * set, <tt>false</tt> otherwise.     *     * @see #isDomainAttributeSpecified     */    public void setDomainAttributeSpecified(boolean value) {        hasDomainAttribute = value;    }    /**     * Returns <tt>true</tt> if cookie's domain was set via a domain      * attribute in the <tt>Set-Cookie</tt> header.     *     * @return value <tt>true</tt> if the cookie's domain was explicitly      * set, <tt>false</tt> otherwise.     *     * @see #setDomainAttributeSpecified     */    public boolean isDomainAttributeSpecified() {        return hasDomainAttribute;    }    /**     * Returns a hash code in keeping with the     * {@link Object#hashCode} general hashCode contract.     * @return A hash code     */    public int hashCode() {        int hash = LangUtils.HASH_SEED;        hash = LangUtils.hashCode(hash, this.getName());        hash = LangUtils.hashCode(hash, this.cookieDomain);        hash = LangUtils.hashCode(hash, this.cookiePath);        return hash;    }    /**     * Two cookies are equal if the name, path and domain match.     * @param obj The object to compare against.     * @return true if the two objects are equal.     */    public boolean equals(Object obj) {        if (obj == null) return false;        if (this == obj) return true;        if (obj instanceof Cookie) {            Cookie that = (Cookie) obj;            return LangUtils.equals(this.getName(), that.getName())                  && LangUtils.equals(this.cookieDomain, that.cookieDomain)                  && LangUtils.equals(this.cookiePath, that.cookiePath);        } else {            return false;        }    }    /**     * Return a textual representation of the cookie.     *      * @return string.     */    public String toExternalForm() {        CookieSpec spec = null;        if (getVersion() > 0) {            spec = CookiePolicy.getDefaultSpec();         } else {            spec = CookiePolicy.getCookieSpec(CookiePolicy.NETSCAPE);         }        return spec.formatCookie(this);     }    /**     * <p>Compares two cookies to determine order for cookie header.</p>     * <p>Most specific should be first. </p>     * <p>This method is implemented so a cookie can be used as a comparator for     * a SortedSet of cookies. Specifically it's used above in the      * createCookieHeader method.</p>     * @param o1 The first object to be compared     * @param o2 The second object to be compared     * @return See {@link java.util.Comparator#compare(Object,Object)}     */    public int compare(Object o1, Object o2) {        LOG.trace("enter Cookie.compare(Object, Object)");        if (!(o1 instanceof Cookie)) {            throw new ClassCastException(o1.getClass().getName());        }        if (!(o2 instanceof Cookie)) {            throw new ClassCastException(o2.getClass().getName());        }        Cookie c1 = (Cookie) o1;        Cookie c2 = (Cookie) o2;        if (c1.getPath() == null && c2.getPath() == null) {            return 0;        } else if (c1.getPath() == null) {            // null is assumed to be "/"            if (c2.getPath().equals(CookieSpec.PATH_DELIM)) {                return 0;            } else {                return -1;            }        } else if (c2.getPath() == null) {            // null is assumed to be "/"            if (c1.getPath().equals(CookieSpec.PATH_DELIM)) {                return 0;            } else {                return 1;            }        } else {            return c1.getPath().compareTo(c2.getPath());        }    }    /**     * Return a textual representation of the cookie.     *      * @return string.     *      * @see #toExternalForm     */    public String toString() {        return toExternalForm();    }   // ----------------------------------------------------- Instance Variables   /** Comment attribute. */   private String  cookieComment;   /** Domain attribute. */   private String  cookieDomain;   /** Expiration {@link Date}. */   private Date    cookieExpiryDate;   /** Path attribute. */   private String  cookiePath;   /** My secure flag. */   private boolean isSecure;   /**    * Specifies if the set-cookie header included a Path attribute for this    * cookie    */   private boolean hasPathAttribute = false;   /**    * Specifies if the set-cookie header included a Domain attribute for this    * cookie    */   private boolean hasDomainAttribute = false;   /** The version of the cookie specification I was created from. */   private int     cookieVersion = 0;   // -------------------------------------------------------------- Constants   /** Log object for this class */   private static final Log LOG = LogFactory.getLog(Cookie.class);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?