Context.setResponseCookie(Cookie)
.- Since:
- 2.0.0
- Author:
- edgar
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionclone()
Copy all state from this cookie and creates a new cookie.Attempt to create/parse a cookie from application configuration object.Decode a cookie value using, like:k=v
, multiplek=v
pair are separated by&
.static String
Encode a hash into cookie value, like:k1=v1&...&kn=vn
.Cookie's domain.Get cookie's domain.long
Max age value:getName()
Cookie's name.getPath()
Cookie's path.Cookie's path.Returns the value for the 'SameSite' parameter.getValue()
Cookie's value.boolean
Cookie's http-only flag.boolean
isSecure()
Secure cookie.Set cookie's domain.setHttpOnly
(boolean httpOnly) Set cookie's http-only.setMaxAge
(long maxAge) Set max age value:Set max age value:Set cookie's name.Set cookie's path.setSameSite
(SameSite sameSite) Sets the value for the 'SameSite' parameter.setSecure
(boolean secure) Set cookie secure flag.Set cookie's value.static String
Sign a value using a secret key.Generates a cookie string.toString()
static String
Un-sign a value, previously signed withsign(String, String)
.
-
Field Details
-
HMAC_SHA256
Algorithm name.- See Also:
-
-
Constructor Details
-
Cookie
Creates a response cookie.- Parameters:
name
- Cookie's name.value
- Cookie's value ornull
.
-
Cookie
Creates a response cookie without a value.- Parameters:
name
- Cookie's name.
-
-
Method Details
-
clone
Copy all state from this cookie and creates a new cookie.- Returns:
- New cookie.
-
getName
Cookie's name.- Returns:
- Cookie's name.
-
setName
Set cookie's name.- Parameters:
name
- Cookie's name.- Returns:
- This cookie.
-
getValue
Cookie's value.- Returns:
- Cookie's value.
-
setValue
Set cookie's value.- Parameters:
value
- Cookie's value.- Returns:
- This cookie.
-
getDomain
Cookie's domain.- Returns:
- Cookie's domain.
-
getDomain
Get cookie's domain.- Parameters:
domain
- Defaults cookie's domain.- Returns:
- Cookie's domain..
-
setDomain
Set cookie's domain.- Parameters:
domain
- Cookie's domain.- Returns:
- This cookie.
-
getPath
Cookie's path.- Returns:
- Cookie's path.
-
getPath
Cookie's path.- Parameters:
path
- Defaults path.- Returns:
- Cookie's path.
-
setPath
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
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
Set cookie secure flag.- Parameters:
secure
- Cookie's secure.- Returns:
- This cookie.
- Throws:
IllegalArgumentException
- iffalse
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
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
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
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 theSecure
attribute in latest browser versions.null
- Not specified.
- Returns:
- the value for 'SameSite' parameter.
- See Also:
-
setSameSite
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 theSecure
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
-
toCookieString
Generates a cookie string. This is the value we sent to the client asSet-Cookie
header.- Returns:
- Cookie string.
-
sign
Sign a value using a secret key. A value and secret key are required. Sign is done withHMAC_SHA256
. Signed value looks like:[signed value] '|' [raw value]
- Parameters:
value
- A value to sign.secret
- A secret key.- Returns:
- A signed value.
-
unsign
Un-sign a value, previously signed withsign(String, String)
. Produces a nonnull value ornull
for invalid.- Parameters:
value
- A signed value.secret
- A secret key.- Returns:
- A new signed value or null.
-
encode
Encode a hash into cookie value, like:k1=v1&...&kn=vn
. Also,key
andvalue
are encoded usingURLEncoder
.- Parameters:
attributes
- Map to encode.- Returns:
- URL encoded from map attributes.
-
decode
Decode a cookie value using, like:k=v
, multiplek=v
pair are separated by&
. Also,k
andv
are decoded usingURLDecoder
.- 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 aname
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.
-