public class Cookie extends Object
Context.setResponseCookie(Cookie)
.Modifier and Type | Field and Description |
---|---|
static String |
HMAC_SHA256
Algorithm name.
|
Constructor and Description |
---|
Cookie(String name)
Creates a response cookie without a value.
|
Cookie(String name,
String value)
Creates a response cookie.
|
Modifier and Type | Method and Description |
---|---|
Cookie |
clone()
Copy all state from this cookie and creates a new cookie.
|
static Optional<Cookie> |
create(String namespace,
com.typesafe.config.Config conf)
Attempt to create/parse a cookie from application configuration object.
|
static Map<String,String> |
decode(String value)
Decode a cookie value using, like:
k=v , multiple k=v pair are
separated by & . |
static String |
encode(Map<String,String> attributes)
Encode a hash into cookie value, like:
k1=v1&...&kn=vn . |
String |
getDomain()
Cookie's domain.
|
String |
getDomain(String domain)
Get cookie's domain.
|
long |
getMaxAge()
Max age value:
-
-1 : indicates a browser session. |
String |
getName()
Cookie's name.
|
String |
getPath()
Cookie's path.
|
String |
getPath(String path)
Cookie's path.
|
String |
getValue()
Cookie's value.
|
boolean |
isHttpOnly()
Cookie's http-only flag.
|
boolean |
isSecure()
Secure cookie.
|
Cookie |
setDomain(String domain)
Set cookie's domain.
|
Cookie |
setHttpOnly(boolean httpOnly)
Set cookie's http-only.
|
Cookie |
setMaxAge(Duration maxAge)
Set max age value:
-
-1 : indicates a browser session. |
Cookie |
setMaxAge(long maxAge)
Set max age value:
-
-1 : indicates a browser session. |
Cookie |
setName(String name)
Set cookie's name.
|
Cookie |
setPath(String path)
Set cookie's path.
|
Cookie |
setSecure(boolean secure)
Set cookie secure flag..
|
Cookie |
setValue(String value)
Set cookie's value.
|
static String |
sign(String value,
String secret)
Sign a value using a secret key.
|
String |
toCookieString()
Generates a cookie string.
|
String |
toString() |
static String |
unsign(String value,
String secret)
Un-sign a value, previously signed with
sign(String, String) . |
public static final String HMAC_SHA256
public Cookie(@Nonnull String name, @Nullable String value)
name
- Cookie's name.value
- Cookie's value or null
.@Nonnull public Cookie setName(@Nonnull String name)
name
- Cookie's name.@Nonnull public Cookie setValue(@Nonnull String value)
value
- Cookie's value.@Nonnull public String getDomain(@Nonnull String domain)
domain
- Defaults cookie's domain.@Nonnull public Cookie setDomain(@Nonnull String domain)
domain
- Cookie's domain.@Nonnull public String getPath(@Nonnull String path)
path
- Defaults path.@Nonnull public Cookie setPath(@Nonnull String path)
path
- Cookie's path.public boolean isHttpOnly()
public Cookie setHttpOnly(boolean httpOnly)
httpOnly
- Cookie's http-only.public boolean isSecure()
@Nonnull public Cookie setSecure(boolean secure)
secure
- Cookie's secure.public long getMaxAge()
-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.@Nonnull public Cookie setMaxAge(@Nonnull Duration maxAge)
-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.maxAge
- Cookie max age.@Nonnull public Cookie setMaxAge(long maxAge)
-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.maxAge
- Cookie max age, in seconds.@Nonnull public String toCookieString()
Set-Cookie
header.@Nonnull public static String sign(@Nonnull String value, @Nonnull String secret)
HMAC_SHA256
.
Signed value looks like:
[signed value] '|' [raw value]
value
- A value to sign.secret
- A secret key.@Nullable public static String unsign(@Nonnull String value, @Nonnull String secret)
sign(String, String)
.
Produces a nonnull value or null
for invalid.value
- A signed value.secret
- A secret key.@Nonnull public static String encode(@Nullable Map<String,String> attributes)
k1=v1&...&kn=vn
. Also,
key
and value
are encoded using URLEncoder
.attributes
- Map to encode.@Nonnull public static Map<String,String> decode(@Nullable String value)
k=v
, multiple k=v
pair are
separated by &
. Also, k
and v
are decoded using
URLDecoder
.value
- URL encoded value.@Nonnull public static Optional<Cookie> create(@Nonnull String namespace, @Nonnull com.typesafe.config.Config conf)
name
property.
The namespace might optionally defined: value, path, domain, secure, httpOnly and maxAge.namespace
- Cookie namespace/prefix.conf
- Configuration object.Copyright © 2020. All rights reserved.