Interface Cookie

All Superinterfaces:
Comparable<Cookie>

public interface Cookie extends Comparable<Cookie>
An interface defining an HTTP cookie.
  • Field Details

    • UNDEFINED_MAX_AGE

      static final long UNDEFINED_MAX_AGE
      Constant for undefined MaxAge attribute value.
      See Also:
  • Method Details

    • of

      @Deprecated static Cookie of(String name, String value)
      Deprecated.
      Use ofSecure(String, String) instead to create a secure Cookie.
      Returns a newly created Cookie.
      Parameters:
      name - the name of the Cookie
      value - the value of the Cookie
    • ofSecure

      static Cookie ofSecure(String name, String value)
      Returns a newly created Cookie with secure settings. "Secure", "HttpOnly", and "SameSite=Strict" are enabled.
      Parameters:
      name - the name of the Cookie
      value - the value of the Cookie
    • builder

      @Deprecated static CookieBuilder builder(String name, String value)
      Deprecated.
      Use secureBuilder(String, String) instead to create a secure Cookie.
      Returns a newly created CookieBuilder which builds a Cookie.
      Parameters:
      name - the name of the Cookie
      value - the value of the Cookie
    • secureBuilder

      static CookieBuilder secureBuilder(String name, String value)
      Returns a newly created CookieBuilder which builds a Cookie with secure settings. "Secure", "HttpOnly", and "SameSite=Strict" are enabled by default.
      Parameters:
      name - the name of the Cookie
      value - the value of the Cookie
    • fromCookieHeader

      static Cookies fromCookieHeader(String cookieHeader)
      Decodes the specified "Cookie" header value into a set of Cookies.
      Parameters:
      cookieHeader - the "Cookie" header value.
      Returns:
      the decoded Cookies.
    • fromCookieHeader

      static Cookies fromCookieHeader(boolean strict, String cookieHeader)
      Decodes the specified "Cookie" header value into a set of Cookies.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookieHeader - the "Cookie" header value.
      Returns:
      the decoded Cookies.
    • fromCookieHeaders

      static Cookies fromCookieHeaders(String... cookieHeaders)
      Decodes the specified "Cookie" header values into a set of Cookies.
      Parameters:
      cookieHeaders - the "Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromCookieHeaders

      static Cookies fromCookieHeaders(Iterable<String> cookieHeaders)
      Decodes the specified "Cookie" header values into a set of Cookies.
      Parameters:
      cookieHeaders - the "Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromCookieHeaders

      static Cookies fromCookieHeaders(boolean strict, String... cookieHeaders)
      Decodes the specified "Cookie" header values into a set of Cookies.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookieHeaders - the "Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromCookieHeaders

      static Cookies fromCookieHeaders(boolean strict, Iterable<String> cookieHeaders)
      Decodes the specified "Cookie" header values into a set of Cookies.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookieHeaders - the "Cookie" header values.
      Returns:
      the decoded Cookies.
    • toCookieHeader

      static String toCookieHeader(Cookie... cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
    • toCookieHeader

      static String toCookieHeader(Iterable<? extends Cookie> cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
      Throws:
      IllegalArgumentException - if cookies is empty.
    • toCookieHeader

      static String toCookieHeader(Collection<? extends Cookie> cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
      Throws:
      IllegalArgumentException - if cookies is empty.
    • toCookieHeader

      static String toCookieHeader(boolean strict, Cookie... cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      strict - whether to validate that cookie names and values are in the valid scope defined in RFC 6265 and to sort the Cookies into order of decreasing path length, as specified in RFC 6265. If false, the Cookies are encoded in the order in which they are given.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
    • toCookieHeader

      static String toCookieHeader(boolean strict, Iterable<? extends Cookie> cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      strict - whether to validate that cookie names and values are in the valid scope defined in RFC 6265 and to sort the Cookies into order of decreasing path length, as specified in RFC 6265. If false, the Cookies are encoded in the order in which they are given.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
      Throws:
      IllegalArgumentException - if cookies is empty.
    • toCookieHeader

      static String toCookieHeader(boolean strict, Collection<? extends Cookie> cookies)
      Encodes the specified Cookies into a "Cookie" header value.
      Parameters:
      strict - whether to validate that cookie names and values are in the valid scope defined in RFC 6265 and to sort the Cookies into order of decreasing path length, as specified in RFC 6265. If false, the Cookies are encoded in the order in which they are given.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Cookie" header value.
      Throws:
      IllegalArgumentException - if cookies is empty.
    • fromSetCookieHeader

      @Nullable static @Nullable Cookie fromSetCookieHeader(String setCookieHeader)
      Decodes the specified "Set-Cookie" header value into a Cookie.
      Parameters:
      setCookieHeader - the "Set-Cookie" header value.
      Returns:
      the decoded Cookie if decoded successfully, or null otherwise.
    • fromSetCookieHeader

      @Nullable static @Nullable Cookie fromSetCookieHeader(boolean strict, String setCookieHeader)
      Decodes the specified "Set-Cookie" header value into a Cookie.
      Parameters:
      strict - whether to validate the cookie names and values are in the valid scope defined in RFC 6265.
      setCookieHeader - the "Set-Cookie" header value.
      Returns:
      the decoded Cookie if decoded successfully, or null otherwise.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(String... setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(Iterable<String> setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(Collection<String> setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(boolean strict, String... setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      strict - whether to validate the cookie names and values are in the valid scope defined in RFC 6265.
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(boolean strict, Iterable<String> setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      strict - whether to validate the cookie names and values are in the valid scope defined in RFC 6265.
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • fromSetCookieHeaders

      static Cookies fromSetCookieHeaders(boolean strict, Collection<String> setCookieHeaders)
      Decodes the specified "Set-Cookie" header values into Cookies.
      Parameters:
      strict - whether to validate the cookie names and values are in the valid scope defined in RFC 6265.
      setCookieHeaders - the "Set-Cookie" header values.
      Returns:
      the decoded Cookies.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(Cookie... cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(Iterable<? extends Cookie> cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(Collection<? extends Cookie> cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(boolean strict, Cookie... cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(boolean strict, Iterable<? extends Cookie> cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • toSetCookieHeaders

      static List<String> toSetCookieHeaders(boolean strict, Collection<? extends Cookie> cookies)
      Encodes the specified Cookies into "Set-Cookie" header values.
      Parameters:
      strict - whether to validate that the cookie names and values are in the valid scope defined in RFC 6265.
      cookies - the Cookies to encode.
      Returns:
      the encoded "Set-Cookie" header values.
    • name

      String name()
      Returns the name of this Cookie.
    • value

      String value()
      Returns the value of this Cookie.
    • isValueQuoted

      boolean isValueQuoted()
      Returns whether the raw value of this Cookie was wrapped with double quotes in the original "Set-Cookie" header.
    • domain

      Returns the domain of this Cookie.
      Returns:
      the domain, or null.
    • path

      Returns the path of this Cookie.
      Returns:
      the path, or null.
    • maxAge

      long maxAge()
      Returns the maximum age of this Cookie in seconds.
      Returns:
      the maximum age, or UNDEFINED_MAX_AGE if unspecified.
    • isSecure

      boolean isSecure()
      Returns whether this Cookie is secure.
    • isHttpOnly

      boolean isHttpOnly()
      Returns whether this Cookie can only be accessed via HTTP. If this returns true, the Cookie cannot be accessed through client side script. However, it works only if the browser supports it. Read here for more information.
    • sameSite

      Returns the "SameSite" attribute of this Cookie.
      Returns:
      the "SameSite" attribute, or null.
    • isHostOnly

      boolean isHostOnly()
      Returns whether this Cookie should only match its original host in domain matching. If this returns true, should domain() returns a non-null value, it's going to be the original request host.
    • toCookieHeader

      default String toCookieHeader()
      Encodes this Cookie into a single "Cookie" header value. Note that you must use toCookieHeader(Collection) when encoding more than one Cookie, because it is prohibited to send multiple "Cookie" headers in an HTTP request, according to RFC 6265.
      Returns:
      a single RFC 6265-style "Cookie" header value.
    • toCookieHeader

      default String toCookieHeader(boolean strict)
      Encodes this Cookie into a single "Cookie" header value. Note that you must use toCookieHeader(boolean, Collection) when encoding more than one Cookie, because it is prohibited to send multiple "Cookie" headers in an HTTP request, according to RFC 6265.
      Parameters:
      strict - whether to validate that the cookie name and value are in the valid scope defined in RFC 6265.
      Returns:
      a single RFC 6265-style "Cookie" header value.
    • toSetCookieHeader

      default String toSetCookieHeader()
      Encodes this Cookie into a single "Set-Cookie" header value.
      Returns:
      a single "Set-Cookie" header value.
    • toSetCookieHeader

      default String toSetCookieHeader(boolean strict)
      Encodes this Cookie into a single "Set-Cookie" header value.
      Parameters:
      strict - whether to validate that the cookie name and value are in the valid scope defined in RFC 6265.
      Returns:
      a single "Set-Cookie" header value.
    • toBuilder

      default CookieBuilder toBuilder()
      Returns a new CookieBuilder created from this Cookie.
      See Also:
    • withMutations

      default Cookie withMutations(Consumer<CookieBuilder> mutator)
      Returns a new Cookie which is the result from the mutation by the specified Consumer. This method is a shortcut for:
      
       builder = toBuilder();
       mutator.accept(builder);
       return builder.build();
       
      See Also:
    • compareTo

      default int compareTo(Cookie c)
      Specified by:
      compareTo in interface Comparable<Cookie>