public class CookieBuilder extends Object
MuResponse.addCookie(Cookie)
Constructor and Description |
---|
CookieBuilder() |
Modifier and Type | Method and Description |
---|---|
Cookie |
build() |
CookieBuilder |
httpOnly(boolean httpOnly)
Instructs browsers on whether or not the cookie can be accessed via JavaScript.
|
CookieBuilder |
makeSessionCookie()
Makes this cookie expire when the browser session expires (which may be when the user closes their
browser).
|
static CookieBuilder |
newCookie()
Creates a new session cookie
|
static CookieBuilder |
newSecureCookie()
Creates a new session cookie that is only sent over HTTPS and cannot be accessed with JavaScript.
|
CookieBuilder |
secure(boolean secure)
Instructs clients on whether or not cookies can be sent over non-secure (HTTP) connections.
|
CookieBuilder |
withDomain(String domain)
Sets the host that clients should send the cookie over.
|
CookieBuilder |
withMaxAgeInSeconds(long maxAge)
Specifies how long the cookie will last for.
|
CookieBuilder |
withName(String name)
Sets the name of the cookie.
|
CookieBuilder |
withPath(String path)
Sets the path prefix that the cookie will be sent with, or null to send to all paths.
|
CookieBuilder |
withUrlEncodedValue(String value)
Sets the value of the cookie by URL Encoding the given value.
|
CookieBuilder |
withValue(String value)
Sets the value of the cookie.
|
public CookieBuilder withName(String name)
name
- The name of the cookiepublic CookieBuilder withValue(String value)
Sets the value of the cookie.
Note that only a subset of ASCII characters are allowed (any other characters must be encoded).
Consider using withUrlEncodedValue(String)
instead if you want to use arbitrary values.
value
- The value to use for the cookie, which can be any US-ASCII characters excluding CTLs, whitespace,
double quotes, comma, semicolon, and backslash.IllegalArgumentException
- If the value contains illegal characterspublic CookieBuilder withUrlEncodedValue(String value)
Sets the value of the cookie by URL Encoding the given value.
value
- A value containing any characters that will be URL Encoded.public CookieBuilder withDomain(String domain)
Sets the host that clients should send the cookie over. By default it is the current domain (but not subdomains).
To allow sub-domains, specify the current domain (e.g. with request.uri().getHost()
).
domain
- The host part of a URL (without scheme or port), e.g. example.org
public CookieBuilder withPath(String path)
Sets the path prefix that the cookie will be sent with, or null to send to all paths.
path
- A path such as /order
which would cause cookies to be sent with requests
to paths such as /order
and /order/checkout
etc.public CookieBuilder withMaxAgeInSeconds(long maxAge)
Specifies how long the cookie will last for.
A value of 0 will cause most browsers to delete the cookie immediately.
Use makeSessionCookie()
to make this a session cookie that will cause most browsers to delete
the cookie when they close their browser.
maxAge
- The age to live in seconds.public CookieBuilder makeSessionCookie()
Makes this cookie expire when the browser session expires (which may be when the user closes their browser).
This overwrites any value set with withMaxAgeInSeconds(long)
public CookieBuilder secure(boolean secure)
Instructs clients on whether or not cookies can be sent over non-secure (HTTP) connections.
It is strongly recommended to use this if your site is HTTPS only as it may prevent private information being sent over HTTP.
secure
- true
to make this cookie HTTPS-only, or false
to allow HTTPS and HTTP.public CookieBuilder httpOnly(boolean httpOnly)
httpOnly
- true
to make it so JavaScript cannot access this cookie; otherwise false
public static CookieBuilder newCookie()
public static CookieBuilder newSecureCookie()
public Cookie build()
Copyright © 2017–2019. All rights reserved.