Package jodd.http

Class Cookie


  • public class Cookie
    extends java.lang.Object
    Cookie object. Simple cookie data holder, cookie header parser and generator.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String comment  
      private java.lang.String domain  
      private java.lang.String expires  
      private boolean httpOnly  
      private java.lang.Integer maxAge  
      private java.lang.String name  
      private java.lang.String path  
      private boolean secure  
      private java.lang.String value  
      private java.lang.Integer version  
    • Constructor Summary

      Constructors 
      Constructor Description
      Cookie​(java.lang.String cookie)
      Parses cookie data from given user-agent string.
      Cookie​(java.lang.String name, java.lang.String value)
      Creates cookie with specified name and value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getComment()
      Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
      java.lang.String getDomain()
      Returns the domain name set for this cookie.
      java.lang.String getExpires()  
      java.lang.Integer getMaxAge()
      Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until session shutdown.
      java.lang.String getName()
      Returns the name of the cookie.
      java.lang.String getPath()
      Returns the path on the server to which the session returns this cookie.
      java.lang.String getValue()
      Returns the value of the cookie.
      java.lang.Integer getVersion()
      Returns the version of the protocol this cookie complies with.
      boolean isHttpOnly()  
      boolean isSecure()
      Returns true if the session is sending cookies only over a secure protocol, or false if the session can send cookies using any protocol.
      Cookie setComment​(java.lang.String purpose)
      Specifies a comment that describes a cookie's purpose.
      Cookie setDomain​(java.lang.String pattern)
      Specifies the domain within which this cookie should be presented.
      Cookie setExpires​(java.lang.String expires)  
      Cookie setHttpOnly​(boolean httpOnly)  
      Cookie setMaxAge​(int expiry)
      Sets the maximum age of the cookie in seconds.
      private void setName​(java.lang.String name)
      Sets the cookie name and checks for validity.
      Cookie setPath​(java.lang.String uri)
      Specifies a path for the cookie to which the client should return the cookie.
      Cookie setSecure​(boolean flag)
      Indicates to the session whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
      Cookie setValue​(java.lang.String newValue)
      Assigns a new value to a cookie after the cookie is created.
      Cookie setVersion​(int version)
      Sets the version of the cookie protocol this cookie complies with.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • name

        private java.lang.String name
      • value

        private java.lang.String value
      • comment

        private java.lang.String comment
      • domain

        private java.lang.String domain
      • maxAge

        private java.lang.Integer maxAge
      • expires

        private java.lang.String expires
      • path

        private java.lang.String path
      • secure

        private boolean secure
      • version

        private java.lang.Integer version
      • httpOnly

        private boolean httpOnly
    • Constructor Detail

      • Cookie

        public Cookie​(java.lang.String name,
                      java.lang.String value)
        Creates cookie with specified name and value.

        The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character.

        The value can be anything the server chooses to send.

      • Cookie

        public Cookie​(java.lang.String cookie)
        Parses cookie data from given user-agent string.
    • Method Detail

      • setName

        private void setName​(java.lang.String name)
        Sets the cookie name and checks for validity.
      • getComment

        public java.lang.String getComment()
        Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
      • setComment

        public Cookie setComment​(java.lang.String purpose)
        Specifies a comment that describes a cookie's purpose. The comment is useful if the session presents the cookie to the user.
      • getDomain

        public java.lang.String getDomain()
        Returns the domain name set for this cookie. The form of the domain name is set by RFC 2109.
      • setDomain

        public Cookie setDomain​(java.lang.String pattern)
        Specifies the domain within which this cookie should be presented.

        The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to the server that sent them.

      • getMaxAge

        public java.lang.Integer getMaxAge()
        Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until session shutdown.
      • setMaxAge

        public Cookie setMaxAge​(int expiry)
        Sets the maximum age of the cookie in seconds.

        A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.

        A negative value means that the cookie is not stored persistently and will be deleted when the Web session exits. A zero value causes the cookie to be deleted.

      • getPath

        public java.lang.String getPath()
        Returns the path on the server to which the session returns this cookie. The cookie is visible to all subpaths on the server.
      • setPath

        public Cookie setPath​(java.lang.String uri)
        Specifies a path for the cookie to which the client should return the cookie.

        The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog.

        Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.

      • isSecure

        public boolean isSecure()
        Returns true if the session is sending cookies only over a secure protocol, or false if the session can send cookies using any protocol.
      • setSecure

        public Cookie setSecure​(boolean flag)
        Indicates to the session whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
      • getName

        public java.lang.String getName()
        Returns the name of the cookie. The name cannot be changed after creation.
      • getValue

        public java.lang.String getValue()
        Returns the value of the cookie.
      • setValue

        public Cookie setValue​(java.lang.String newValue)
        Assigns a new value to a cookie after the cookie is created. If you use a binary value, you may want to use BASE64 encoding.
      • getVersion

        public java.lang.Integer getVersion()
        Returns the version of the protocol this cookie complies with. Version 1 complies with RFC 2109, and version 0 complies with the original cookie specification drafted by Netscape. Cookies provided by a session use and identify the session's cookie version.
      • setVersion

        public Cookie setVersion​(int version)
        Sets the version of the cookie protocol this cookie complies with. Version 0 complies with the original Netscape cookie specification. Version 1 complies with RFC 2109.
      • isHttpOnly

        public boolean isHttpOnly()
      • setHttpOnly

        public Cookie setHttpOnly​(boolean httpOnly)
      • getExpires

        public java.lang.String getExpires()
      • setExpires

        public Cookie setExpires​(java.lang.String expires)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object