Class SetCookie
Set-Cookie header with secure defaults, validated when it is created.
var cookie = SetCookie.of("__Host-theme", "dark"); // Secure; HttpOnly; SameSite=Lax; Path=/
return response.withHeader("Set-Cookie", cookie.header());
Defaults. Secure, HttpOnly, SameSite=Lax, Path=/, no
Domain (so the cookie is host-only and no sub-domain receives it), no Max-Age (a
browser-session cookie).
Rules enforced. The name is an HTTP token and the value consists of cookie octets; name
plus value is at most 4,096 bytes. A name starting with __Host- (compared ignoring case,
as browsers do) requires Secure, Path=/ and no Domain; __Secure-
requires Secure. SameSite=None requires Secure. A Domain is a
lower-case DNS name (no leading dot, not an IP address); a Path starts with / and
has no control characters or semicolons; Max-Age is 0 (delete) to 400 days. Violations
throw IllegalArgumentException.
One response can carry one Set-Cookie in Axiom's header model; addTo(com.jsgalactic.axiom.http.Response) refuses
to replace another.
Immutable and thread-safe. toString() omits the value.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionReturns the response with this cookie as itsSet-Cookie.Returns a copy with aDomain, which also sends the cookie to every sub-domain.expire()Returns a copy that tells the browser to delete the cookie (empty value,Max-Age=0); name, path and domain must match the cookie being deleted.header()Returns the header value, for examplesid=abc; Path=/; Secure; HttpOnly; SameSite=Lax.httpOnly(boolean httpOnly) Returns a copy withHttpOnlyset or cleared.booleanReturns whether the cookie isHttpOnly.booleanisSecure()Returns whether the cookie isSecure.Returns a copy with aMax-Age, in whole seconds (rounded down).name()Returns the cookie name.static SetCookieCreates a cookie with the secure defaults.Returns a copy with anotherPath.sameSite(SetCookie.SameSite sameSite) Returns a copy with anotherSameSiteattribute.secure(boolean secure) Returns a copy with theSecureattribute set or cleared.toString()value()Returns the cookie value.Returns a copy with another value.
-
Method Details
-
of
Creates a cookie with the secure defaults.- Parameters:
name- cookie namevalue- cookie value, possibly empty- Returns:
- the cookie
- Throws:
IllegalArgumentException- if a rule above is violated
-
withValue
Returns a copy with another value.- Parameters:
value- cookie value- Returns:
- the copy
-
secure
Returns a copy with theSecureattribute set or cleared. Browsers drop Secure cookies received over plain HTTP (except on localhost), so clear it only for local development.- Parameters:
secure- whether the cookie is Secure- Returns:
- the copy
-
httpOnly
Returns a copy withHttpOnlyset or cleared.- Parameters:
httpOnly- whether scripts are denied access- Returns:
- the copy
-
sameSite
Returns a copy with anotherSameSiteattribute.- Parameters:
sameSite- the attribute- Returns:
- the copy
-
path
Returns a copy with anotherPath.- Parameters:
path- path starting with/- Returns:
- the copy
-
domain
Returns a copy with aDomain, which also sends the cookie to every sub-domain.- Parameters:
domain- lower-case DNS name without a leading dot, or null for a host-only cookie- Returns:
- the copy
-
maxAge
Returns a copy with aMax-Age, in whole seconds (rounded down).- Parameters:
maxAge- lifetime from 0 to 400 days, or null for a browser-session cookie- Returns:
- the copy
-
expire
Returns a copy that tells the browser to delete the cookie (empty value,Max-Age=0); name, path and domain must match the cookie being deleted.- Returns:
- the deleting cookie
-
name
Returns the cookie name.- Returns:
- the name
-
value
Returns the cookie value.- Returns:
- the value, a credential for session and token cookies
-
isSecure
public boolean isSecure()Returns whether the cookie isSecure.- Returns:
- the attribute
-
isHttpOnly
public boolean isHttpOnly()Returns whether the cookie isHttpOnly.- Returns:
- the attribute
-
header
Returns the header value, for examplesid=abc; Path=/; Secure; HttpOnly; SameSite=Lax.- Returns:
- the
Set-Cookievalue
-
addTo
Returns the response with this cookie as itsSet-Cookie.- Parameters:
response- the response- Returns:
- a copy carrying the header
- Throws:
IllegalStateException- if the response already has aSet-Cookie
-
toString
-