Class NewCookie


  • public class NewCookie
    extends Cookie
    Used to create a new HTTP cookie, transferred in a response.
    Since:
    1.0
    Author:
    Paul Sandoz, Marc Hadley
    See Also:
    IETF RFC 2109
    • Field Detail

      • DEFAULT_MAX_AGE

        public static final int DEFAULT_MAX_AGE
        Specifies that the cookie expires with the current application/browser session.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie.
        value - the value of the cookie.
        Throws:
        IllegalArgumentException - if name is null.
      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value,
                         String path,
                         String domain,
                         String comment,
                         int maxAge,
                         boolean secure)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie.
        value - the value of the cookie.
        path - the URI path for which the cookie is valid.
        domain - the host domain for which the cookie is valid.
        comment - the comment.
        maxAge - the maximum age of the cookie in seconds.
        secure - specifies whether the cookie will only be sent over a secure connection.
        Throws:
        IllegalArgumentException - if name is null.
      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value,
                         String path,
                         String domain,
                         String comment,
                         int maxAge,
                         boolean secure,
                         boolean httpOnly)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie.
        value - the value of the cookie.
        path - the URI path for which the cookie is valid.
        domain - the host domain for which the cookie is valid.
        comment - the comment.
        maxAge - the maximum age of the cookie in seconds.
        secure - specifies whether the cookie will only be sent over a secure connection.
        httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
        Throws:
        IllegalArgumentException - if name is null.
        Since:
        2.0
      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value,
                         String path,
                         String domain,
                         int version,
                         String comment,
                         int maxAge,
                         boolean secure)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie
        value - the value of the cookie
        path - the URI path for which the cookie is valid
        domain - the host domain for which the cookie is valid
        version - the version of the specification to which the cookie complies
        comment - the comment
        maxAge - the maximum age of the cookie in seconds
        secure - specifies whether the cookie will only be sent over a secure connection
        Throws:
        IllegalArgumentException - if name is null.
      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value,
                         String path,
                         String domain,
                         int version,
                         String comment,
                         int maxAge,
                         Date expiry,
                         boolean secure,
                         boolean httpOnly)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie
        value - the value of the cookie
        path - the URI path for which the cookie is valid
        domain - the host domain for which the cookie is valid
        version - the version of the specification to which the cookie complies
        comment - the comment
        maxAge - the maximum age of the cookie in seconds
        expiry - the cookie expiry date.
        secure - specifies whether the cookie will only be sent over a secure connection
        httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
        Throws:
        IllegalArgumentException - if name is null.
        Since:
        2.0
      • NewCookie

        @Deprecated
        public NewCookie​(String name,
                         String value,
                         String path,
                         String domain,
                         int version,
                         String comment,
                         int maxAge,
                         Date expiry,
                         boolean secure,
                         boolean httpOnly,
                         NewCookie.SameSite sameSite)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance.
        Parameters:
        name - the name of the cookie
        value - the value of the cookie
        path - the URI path for which the cookie is valid
        domain - the host domain for which the cookie is valid
        version - the version of the specification to which the cookie complies
        comment - the comment
        maxAge - the maximum age of the cookie in seconds
        expiry - the cookie expiry date.
        secure - specifies whether the cookie will only be sent over a secure connection
        httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
        sameSite - specifies the value of the SameSite cookie attribute
        Throws:
        IllegalArgumentException - if name is null.
        Since:
        3.1
      • NewCookie

        @Deprecated
        public NewCookie​(Cookie cookie)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance copying the information in the supplied cookie.
        Parameters:
        cookie - the cookie to clone.
        Throws:
        IllegalArgumentException - if cookie is null.
      • NewCookie

        @Deprecated
        public NewCookie​(Cookie cookie,
                         String comment,
                         int maxAge,
                         boolean secure)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance supplementing the information in the supplied cookie.
        Parameters:
        cookie - the cookie to clone.
        comment - the comment.
        maxAge - the maximum age of the cookie in seconds.
        secure - specifies whether the cookie will only be sent over a secure connection.
        Throws:
        IllegalArgumentException - if cookie is null.
      • NewCookie

        @Deprecated
        public NewCookie​(Cookie cookie,
                         String comment,
                         int maxAge,
                         Date expiry,
                         boolean secure,
                         boolean httpOnly)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance supplementing the information in the supplied cookie.
        Parameters:
        cookie - the cookie to clone.
        comment - the comment.
        maxAge - the maximum age of the cookie in seconds.
        expiry - the cookie expiry date.
        secure - specifies whether the cookie will only be sent over a secure connection.
        httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
        Throws:
        IllegalArgumentException - if cookie is null.
        Since:
        2.0
      • NewCookie

        @Deprecated
        public NewCookie​(Cookie cookie,
                         String comment,
                         int maxAge,
                         Date expiry,
                         boolean secure,
                         boolean httpOnly,
                         NewCookie.SameSite sameSite)
        Deprecated.
        This constructor will be removed in a future version. Please use NewCookie.Builder instead.
        Create a new instance supplementing the information in the supplied cookie.
        Parameters:
        cookie - the cookie to clone.
        comment - the comment.
        maxAge - the maximum age of the cookie in seconds.
        expiry - the cookie expiry date.
        secure - specifies whether the cookie will only be sent over a secure connection.
        httpOnly - if true make the cookie HTTP only, i.e. only visible as part of an HTTP request.
        sameSite - specifies the value of the SameSite cookie attribute
        Throws:
        IllegalArgumentException - if cookie is null.
        Since:
        3.1
    • Method Detail

      • valueOf

        @Deprecated
        public static NewCookie valueOf​(String value)
        Deprecated.
        This method will be removed in a future version. Please use RuntimeDelegate.getInstance().createHeaderDelegate(NewCookie.class).fromString(value) instead.
        Creates a new instance of NewCookie by parsing the supplied string.
        Parameters:
        value - the cookie string.
        Returns:
        the newly created NewCookie.
        Throws:
        IllegalArgumentException - if the supplied string cannot be parsed or is null.
      • getComment

        public String getComment()
        Get the comment associated with the cookie.
        Returns:
        the comment or null if none set
      • getMaxAge

        public int getMaxAge()
        Get the maximum age of the the cookie in seconds. Cookies older than the maximum age are discarded. A cookie can be unset by sending a new cookie with maximum age of 0 since it will overwrite any existing cookie and then be immediately discarded. The default value of -1 indicates that the cookie will be discarded at the end of the browser/application session.

        Note that it is recommended to use Max-Age to control cookie expiration, however some browsers do not understand Max-Age, in which case setting getExpiry() Expires} parameter may be necessary.

        Returns:
        the maximum age in seconds.
        See Also:
        getExpiry()
      • getExpiry

        public Date getExpiry()
        Get the cookie expiry date. Cookies whose expiry date has passed are discarded. A cookie can be unset by setting a new cookie with an expiry date in the past, typically the lowest possible date that can be set.

        Note that it is recommended to use Max-Age to control cookie expiration, however some browsers do not understand Max-Age, in which case setting Expires parameter may be necessary.

        Returns:
        cookie expiry date or null if no expiry date was set.
        Since:
        2.0
        See Also:
        getMaxAge()
      • isSecure

        public boolean isSecure()
        Whether the cookie will only be sent over a secure connection. Defaults to false.
        Returns:
        true if the cookie will only be sent over a secure connection, false otherwise.
      • isHttpOnly

        public boolean isHttpOnly()
        Returns true if this cookie contains the HttpOnly attribute. This means that the cookie should not be accessible to scripting engines, like javascript.
        Returns:
        true if this cookie should be considered http only, false otherwise.
        Since:
        2.0
      • getSameSite

        public NewCookie.SameSite getSameSite()
        Returns the value of the SameSite attribute for this cookie or null if the attribute is not set. This attributes controls whether the cookie is sent with cross-origin requests, providing protection against cross-site request forgery.
        Returns:
        the value of the SameSite cookie attribute or null.
        Since:
        3.1
      • toCookie

        public Cookie toCookie()
        Obtain a new instance of a Cookie with the same name, value, path, domain and version as this NewCookie. This method can be used to obtain an object that can be compared for equality with another Cookie; since a Cookie will never compare equal to a NewCookie.
        Returns:
        a Cookie
      • toString

        @Deprecated
        public String toString()
        Deprecated.
        The format of the toString() method is subject to change in a future version. Please use RuntimeDelegate.getInstance().createHeaderDelegate(NewCookie.class).toString(value) instead if you rely on the format of this method.
        Convert the cookie to a string suitable for use as the value of the corresponding HTTP header.
        Overrides:
        toString in class Cookie
        Returns:
        a stringified cookie.
      • hashCode

        public int hashCode()
        Generate a hash code by hashing all of the properties.
        Overrides:
        hashCode in class Cookie
        Returns:
        the hash code.
      • equals

        public boolean equals​(Object obj)
        Compare for equality. Use toCookie() to compare a NewCookie to a Cookie considering only the common properties.
        Overrides:
        equals in class Cookie
        Parameters:
        obj - the object to compare to
        Returns:
        true if the object is a NewCookie with the same value for all properties, false otherwise.