Module io.jooby
Package io.jooby

Class Cookie

java.lang.Object
io.jooby.Cookie

public class Cookie extends Object
Response cookie implementation. Response are send it back to client using Context.setResponseCookie(Cookie).
Since:
2.0.0
Author:
edgar
  • Field Details

  • Constructor Details

    • Cookie

      public Cookie(@NonNull String name, @Nullable String value)
      Creates a response cookie.
      Parameters:
      name - Cookie's name.
      value - Cookie's value or null.
    • Cookie

      public Cookie(@NonNull String name)
      Creates a response cookie without a value.
      Parameters:
      name - Cookie's name.
  • Method Details

    • clone

      @NonNull public Cookie clone()
      Copy all state from this cookie and creates a new cookie.
      Returns:
      New cookie.
    • getName

      @NonNull public String getName()
      Cookie's name.
      Returns:
      Cookie's name.
    • setName

      @NonNull public Cookie setName(@NonNull String name)
      Set cookie's name.
      Parameters:
      name - Cookie's name.
      Returns:
      This cookie.
    • getValue

      @Nullable public String getValue()
      Cookie's value.
      Returns:
      Cookie's value.
    • setValue

      @NonNull public Cookie setValue(@NonNull String value)
      Set cookie's value.
      Parameters:
      value - Cookie's value.
      Returns:
      This cookie.
    • getDomain

      @Nullable public String getDomain()
      Cookie's domain.
      Returns:
      Cookie's domain.
    • getDomain

      @NonNull public String getDomain(@NonNull String domain)
      Get cookie's domain.
      Parameters:
      domain - Defaults cookie's domain.
      Returns:
      Cookie's domain..
    • setDomain

      @NonNull public Cookie setDomain(@NonNull String domain)
      Set cookie's domain.
      Parameters:
      domain - Cookie's domain.
      Returns:
      This cookie.
    • getPath

      @Nullable public String getPath()
      Cookie's path.
      Returns:
      Cookie's path.
    • getPath

      @NonNull public String getPath(@NonNull String path)
      Cookie's path.
      Parameters:
      path - Defaults path.
      Returns:
      Cookie's path.
    • setPath

      @NonNull public Cookie setPath(@NonNull String path)
      Set cookie's path.
      Parameters:
      path - Cookie's path.
      Returns:
      This cookie.
    • isHttpOnly

      public boolean isHttpOnly()
      Cookie's http-only flag.
      Returns:
      Htto-only flag.
    • setHttpOnly

      public Cookie setHttpOnly(boolean httpOnly)
      Set cookie's http-only.
      Parameters:
      httpOnly - Cookie's http-only.
      Returns:
      This cookie.
    • isSecure

      public boolean isSecure()
      Secure cookie.
      Returns:
      Secure cookie flag.
    • setSecure

      @NonNull public Cookie setSecure(boolean secure)
      Set cookie secure flag.
      Parameters:
      secure - Cookie's secure.
      Returns:
      This cookie.
      Throws:
      IllegalArgumentException - if false is specified and the 'SameSite' attribute value requires a secure cookie.
    • getMaxAge

      public long getMaxAge()
      Max age value:

      - -1: indicates a browser session. It is deleted when user closed the browser. - 0: indicates a cookie has expired and browser must delete the cookie. - positive value: indicates the number of seconds from current date, where browser must expires the cookie.

      Returns:
      Max age, in seconds.
    • setMaxAge

      @NonNull public Cookie setMaxAge(@NonNull Duration maxAge)
      Set max age value:

      - -1: indicates a browser session. It is deleted when user closed the browser. - 0: indicates a cookie has expired and browser must delete the cookie. - positive value: indicates the number of seconds from current date, where browser must expires the cookie.

      Parameters:
      maxAge - Cookie max age.
      Returns:
      This options.
    • setMaxAge

      @NonNull public Cookie setMaxAge(long maxAge)
      Set max age value:

      - -1: indicates a browser session. It is deleted when user closed the browser. - 0: indicates a cookie has expired and browser must delete the cookie. - positive value: indicates the number of seconds from current date, where browser must expires the cookie.

      Parameters:
      maxAge - Cookie max age, in seconds.
      Returns:
      This options.
    • getSameSite

      @Nullable public SameSite getSameSite()
      Returns the value for the 'SameSite' parameter.
      • SameSite.LAX - Cookies are allowed to be sent with top-level navigations and will be sent along with GET request initiated by third party website. This is the default value in modern browsers.
      • SameSite.STRICT - Cookies will only be sent in a first-party context and not be sent along with requests initiated by third party websites.
      • SameSite.NONE - Cookies will be sent in all contexts, i.e sending cross-origin is allowed. Requires the Secure attribute in latest browser versions.
      • null - Not specified.
      Returns:
      the value for 'SameSite' parameter.
      See Also:
    • setSameSite

      public Cookie setSameSite(@Nullable SameSite sameSite)
      Sets the value for the 'SameSite' parameter.
      • SameSite.LAX - Cookies are allowed to be sent with top-level navigations and will be sent along with GET request initiated by third party website. This is the default value in modern browsers.
      • SameSite.STRICT - Cookies will only be sent in a first-party context and not be sent along with requests initiated by third party websites.
      • SameSite.NONE - Cookies will be sent in all contexts, i.e sending cross-origin is allowed. Requires the Secure attribute in latest browser versions.
      • null - Not specified.
      Parameters:
      sameSite - the value for the 'SameSite' parameter.
      Returns:
      this instance.
      Throws:
      IllegalArgumentException - if a value requiring a secure cookie is specified and this cookie is not flagged as secure.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toCookieString

      @NonNull public String toCookieString()
      Generates a cookie string. This is the value we sent to the client as Set-Cookie header.
      Returns:
      Cookie string.
    • sign

      @NonNull public static String sign(@NonNull String value, @NonNull String secret)
      Sign a value using a secret key. A value and secret key are required. Sign is done with HMAC_SHA256. Signed value looks like:
         [signed value] '|' [raw value]
       
      Parameters:
      value - A value to sign.
      secret - A secret key.
      Returns:
      A signed value.
    • unsign

      @Nullable public static String unsign(@NonNull String value, @NonNull String secret)
      Un-sign a value, previously signed with sign(String, String). Produces a nonnull value or null for invalid.
      Parameters:
      value - A signed value.
      secret - A secret key.
      Returns:
      A new signed value or null.
    • encode

      @NonNull public static String encode(@Nullable Map<String,String> attributes)
      Encode a hash into cookie value, like: k1=v1&...&kn=vn. Also, key and value are encoded using URLEncoder.
      Parameters:
      attributes - Map to encode.
      Returns:
      URL encoded from map attributes.
    • decode

      @NonNull public static Map<String,String> decode(@Nullable String value)
      Decode a cookie value using, like: k=v, multiple k=v pair are separated by &. Also, k and v are decoded using URLDecoder.
      Parameters:
      value - URL encoded value.
      Returns:
      Decoded as map.
    • create

      @NonNull public static Optional<Cookie> create(@NonNull String namespace, @NonNull com.typesafe.config.Config conf)
      Attempt to create/parse a cookie from application configuration object. The namespace given must be present and must defined a name property.

      The namespace might optionally defined: value, path, domain, secure, httpOnly and maxAge.

      Parameters:
      namespace - Cookie namespace/prefix.
      conf - Configuration object.
      Returns:
      Parsed cookie or empty.