Class CookieBuilder
- java.lang.Object
-
- io.muserver.CookieBuilder
-
public class CookieBuilder extends java.lang.Object
A builder to create cookies that will be sent on a Response withMuResponse.addCookie(Cookie)
-
-
Constructor Summary
Constructors Constructor Description CookieBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 cookiestatic CookieBuilder
newSecureCookie()
Creates a new session cookie that is only sent over HTTPS and cannot be accessed with JavaScript with a Strict samesite policy applied.CookieBuilder
secure(boolean secure)
Instructs clients on whether or not cookies can be sent over non-secure (HTTP) connections.CookieBuilder
withDomain(java.lang.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(java.lang.String name)
Sets the name of the cookie.CookieBuilder
withPath(java.lang.String path)
Sets the path prefix that the cookie will be sent with, or null to send to all paths.CookieBuilder
withSameSite(java.lang.String sameSiteValue)
Sets theSameSite
property of the cookie.CookieBuilder
withUrlEncodedValue(java.lang.String value)
Sets the value of the cookie by URL Encoding the given value.CookieBuilder
withValue(java.lang.String value)
Sets the value of the cookie.
-
-
-
Method Detail
-
withName
public CookieBuilder withName(java.lang.String name)
Sets the name of the cookie.- Parameters:
name
- The name of the cookie- Returns:
- This builder
-
withValue
public CookieBuilder withValue(java.lang.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.- Parameters:
value
- The value to use for the cookie, which can be any US-ASCII characters excluding CTLs, whitespace, double quotes, comma, semicolon, and backslash.- Returns:
- This builder
- Throws:
java.lang.IllegalArgumentException
- If the value contains illegal characters
-
withUrlEncodedValue
public CookieBuilder withUrlEncodedValue(java.lang.String value)
Sets the value of the cookie by URL Encoding the given value.
- Parameters:
value
- A value containing any characters that will be URL Encoded.- Returns:
- This builder
-
withDomain
public CookieBuilder withDomain(java.lang.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()
).- Parameters:
domain
- The host part of a URL (without scheme or port), e.g.example.org
- Returns:
- This builder
-
withPath
public CookieBuilder withPath(java.lang.String path)
Sets the path prefix that the cookie will be sent with, or null to send to all paths.
- Parameters:
path
- A path such as/order
which would cause cookies to be sent with requests to paths such as/order
and/order/checkout
etc.- Returns:
- This builder
-
withMaxAgeInSeconds
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.- Parameters:
maxAge
- The age to live in seconds.- Returns:
- This builder
-
makeSessionCookie
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)
- Returns:
- This builder
-
secure
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.
- Parameters:
secure
-true
to make this cookie HTTPS-only, orfalse
to allow HTTPS and HTTP.- Returns:
- This builder
-
httpOnly
public CookieBuilder httpOnly(boolean httpOnly)
Instructs browsers on whether or not the cookie can be accessed via JavaScript. This is a secure feature that should be used unless JavaScript access is required.- Parameters:
httpOnly
-true
to make it so JavaScript cannot access this cookie; otherwisefalse
- Returns:
- This builder
-
withSameSite
public CookieBuilder withSameSite(java.lang.String sameSiteValue)
Sets theSameSite
property of the cookie. If not set, thenNone
is used.- Parameters:
sameSiteValue
- One ofStrict
,Lax
orNone
- Returns:
- This builder
-
newCookie
public static CookieBuilder newCookie()
Creates a new session cookie- Returns:
- A builder to create a new cookie;
-
newSecureCookie
public static CookieBuilder newSecureCookie()
Creates a new session cookie that is only sent over HTTPS and cannot be accessed with JavaScript with a Strict samesite policy applied.- Returns:
- A builder to create a new cookie;
-
build
public Cookie build()
- Returns:
- Returns a newly created cookie.
-
-