Package ej.hoka.http

Class Cookie.Builder

  • Enclosing class:
    Cookie

    public static final class Cookie.Builder
    extends java.lang.Object
    Cookie builder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Cookie build()
      Builds the cookie instance.
      Cookie.Builder domain​(java.lang.String domain)
      Domain=<domain-value> Optional Host to which the cookie will be sent.
      Cookie.Builder expires​(java.util.Date expires)
      Expires=<date> Optional The maximum lifetime of the cookie as an HTTP-date timestamp.
      Cookie.Builder httpOnly()
      HttpOnly Optional Forbids JavaScript from accessing the cookie, for example, through the Document.cookie property.
      Cookie.Builder maxAge​(int maxAge)
      Max-Age=<number> Optional Number of seconds until the cookie expires.
      Cookie.Builder name​(java.lang.String name)
      A <cookie-name> can be any US-ASCII characters, except control characters, spaces, or tabs.
      Cookie.Builder path​(java.lang.String path)
      Path=<path-value> Optional A path that must exist in the requested URL, or the browser won't send the Cookie header.
      Cookie.Builder sameSite​(Cookie.SameSite sameSite)
      SameSite=<samesite-value> Optional Controls whether a cookie is sent with cross-origin requests, providing some protection against cross-site request forgery attacks (CSRF).
      Cookie.Builder secure()
      Secure Optional Cookie is only sent to the server when a request is made with the https: scheme (except on localhost), and therefore is more resistent to man-in-the-middle attacks.
      Cookie.Builder value​(java.lang.String value)
      A <cookie-value> can optionally be wrapped in double quotes and include any US-ASCII characters excluding control characters, Whitespace, double quotes, comma, semicolon, and backslash.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • name

        public Cookie.Builder name​(@Nullable
                                   java.lang.String name)
        A <cookie-name> can be any US-ASCII characters, except control characters, spaces, or tabs. It also must not contain a separator character like the following: ( ) < > @ , ; : \ " / [ ] ? = { } .
        Parameters:
        name - cookie name
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • value

        public Cookie.Builder value​(@Nullable
                                    java.lang.String value)
        A <cookie-value> can optionally be wrapped in double quotes and include any US-ASCII characters excluding control characters, Whitespace, double quotes, comma, semicolon, and backslash. Encoding: Many implementations perform URL encoding on cookie values, however it is not required per the RFC specification. It does help satisfying the requirements about which characters are allowed for <cookie-value> though.
        Parameters:
        value - cookie value
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • expires

        public Cookie.Builder expires​(@Nullable
                                      java.util.Date expires)
        Expires=<date> Optional The maximum lifetime of the cookie as an HTTP-date timestamp. If unspecified, the cookie becomes a session cookie. A session finishes when the client shuts down, and session cookies will be removed. Warning: Many web browsers have a session restore feature that will save all tabs and restore them next time the browser is used. Session cookies will also be restored, as if the browser was never closed. When an Expires date is set, the deadline is relative to the client the cookie is being set on, not the server.
        Parameters:
        expires - the maximum lifetime of the cookie as an HTTP-date timestamp
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • domain

        public Cookie.Builder domain​(@Nullable
                                     java.lang.String domain)
        Domain=<domain-value> Optional Host to which the cookie will be sent. If omitted, defaults to the host of the current document URL, not including subdomains. Contrary to earlier specifications, leading dots in domain names (.example.com) are ignored. Multiple host/domain values are not allowed, but if a domain is specified, then subdomains are always included.
        Parameters:
        domain - Host to which the cookie will be sent.
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • path

        public Cookie.Builder path​(@Nullable
                                   java.lang.String path)
        Path=<path-value> Optional A path that must exist in the requested URL, or the browser won't send the Cookie header. The forward slash (/) character is interpreted as a directory separator, and subdirectories will be matched as well: for Path=/docs, /docs, /docs/Web/, and /docs/Web/HTTP will all match.
        Parameters:
        path - A path that must exist in the requested URL, or the browser won't send the Cookie header
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • maxAge

        public Cookie.Builder maxAge​(int maxAge)
        Max-Age=<number> Optional Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence.
        Parameters:
        maxAge - seconds until the cookie expires
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • secure

        public Cookie.Builder secure()
        Secure Optional Cookie is only sent to the server when a request is made with the https: scheme (except on localhost), and therefore is more resistent to man-in-the-middle attacks. Note: Do not assume that Secure prevents all access to sensitive information in cookies (session keys, login details, etc.). Cookies with this attribute can still be read/modified with access to the client's hard disk, or from JavaScript if the HttpOnly cookie attribute is not set. Note: Insecure sites (http:) can't set cookies with the Secure attribute (since Chrome 52 and Firefox 52). For Firefox, the https: requirements are ignored when the Secure attribute is set by localhost (since Firefox 75).
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • httpOnly

        public Cookie.Builder httpOnly()
        HttpOnly Optional Forbids JavaScript from accessing the cookie, for example, through the Document.cookie property. Note that a cookie that has been created with HttpOnly will still be sent with JavaScript-initiated requests, e.g. when calling XMLHttpRequest.send() or fetch(). This mitigates attacks against cross-site scripting (XSS).
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • sameSite

        public Cookie.Builder sameSite​(@Nullable
                                       Cookie.SameSite sameSite)
        SameSite=<samesite-value> Optional Controls whether a cookie is sent with cross-origin requests, providing some protection against cross-site request forgery attacks (CSRF). Inline options are: Strict: The browser sends the cookie only for same-site requests (that is, requests originating from the same site that set the cookie). If the request originated from a different URL than the current one, no cookies with the SameSite=Strict attribute are sent. Lax: The cookie is not sent on cross-site requests, such as calls to load images or frames, but is sent when a user is navigating to the origin site from an external site (e.g. if following a link). This is the default behavior if the SameSite attribute is not specified. None: The browser sends the cookie with both cross-site and same-site requests. The Secure attribute must also be set when SameSite=None!
        Parameters:
        sameSite - Controls whether a cookie is sent with cross-origin requests, providing some protection against cross-site request forgery attacks (CSRF)
        Returns:
        builder instance to continue configuring the cookie or building the instance by calling build()
      • build

        public Cookie build()
        Builds the cookie instance.
        Returns:
        Cookie instance