⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookie.java

📁 爬虫
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    /**     * 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 STRING_COLLATOR.compare(c1.getPath(), c2.getPath());        }    }    /**     * Return a textual representation of the cookie.     *      * @return string.     *      * @see #toExternalForm     */    public String toString() {        return toExternalForm();    }// BEGIN IA ADDITION    /**     * Create a 'sort key' for this Cookie that will cause it to sort      * alongside other Cookies of the same domain (with or without leading     * '.'). This helps cookie-match checks consider only narrow set of     * possible matches, rather than all cookies.      *      * Only two cookies that are equals() (same domain, path, name) will have     * the same sort key. The '\1' separator character is important in      * conjunction with Cookie.DOMAIN+OVERBOUNDS, allowing keys based on the     * domain plus an extension to define the relevant range in a SortedMap.      * @return String sort key for this cookie     */    public String getSortKey() {        String domain = getDomain();        return (domain.startsWith("."))             ? domain.substring(1) + "\1.\1" + getPath() + "\1" + getName()             : domain + "\1\1" + getPath() + "\1" + getName();    }//  END IA ADDITION          // ----------------------------------------------------- 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   /**     * Collator for Cookie comparisons.  Could be replaced with references to    * specific Locales.    */   private static final RuleBasedCollator STRING_COLLATOR =        (RuleBasedCollator) RuleBasedCollator.getInstance(                                                new Locale("en", "US", ""));   /** Log object for this class */   private static final Log LOG = LogFactory.getLog(Cookie.class);// BEGIN IA ADDITION   /**    * Character which, if appended to end of a domain, will give a     * boundary key that sorts past all Cookie sortKeys for the same    * domain.     */   public static final char DOMAIN_OVERBOUNDS = '\2';// END IA ADDITION}

⌨️ 快捷键说明

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