public final class Cookie
extends java.lang.Object
builder(String, String)
Modifier and Type | Class and Description |
---|---|
static class |
Cookie.SameSite
Specifies whether/when cookies are sent with cross-site requests.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
SECRET_PREFIX |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
build() |
void |
buildAndAddToResponse(com.sun.net.httpserver.HttpExchange htx)
Cakks
build() and adds the Cookie to the
HttpExchange.getResponseHeaders() . |
static Cookie |
builder(java.lang.String name,
java.lang.String value)
Cookie Builder, initialized with required name and value.
|
Cookie |
domain(java.lang.String domain)
Specifies which server can receive the cookie.
|
Cookie |
encrypt(java.lang.String secretKey)
If set (not null), the value will be encrypted and
prefixed with "###~".
|
Cookie |
expires(int year,
java.time.Month month,
int dayOfMonth,
int hour,
int minute)
Sets the absolute timestamp for cookie's end of live.
|
Cookie |
expires(java.lang.Long unixTimestamp)
Expire Cookie at UnixTimeStamp (seconds).
|
Cookie |
expiresToday(int hour,
int minute)
Expire Cookie today at given time.
|
Cookie |
expiresTomorrow(int hour,
int minute)
Expire Cookie tomorrow at given time.
|
static java.util.Map<java.lang.String,java.lang.String> |
fromHeaders(com.sun.net.httpserver.Headers headers,
java.lang.String secretKey)
Returns Cookie's Name-Value-Pairs as Map.
|
static java.util.Map<java.lang.String,java.lang.String> |
fromRequest(com.sun.net.httpserver.HttpExchange htx)
Calls
fromHeaders(Headers, String) with null
secret key for Request. |
static java.util.Map<java.lang.String,java.lang.String> |
fromRequest(com.sun.net.httpserver.HttpExchange htx,
java.lang.String secretKey)
Calls
fromHeaders(Headers, String) for Request. |
Cookie |
httpOnly()
Sets httpOnly, so JavaScript in Browser cannot see this Cookie.
|
Cookie |
maxAge(java.lang.Integer maxAge) |
Cookie |
maxAgeHours(int maxAge) |
Cookie |
maxAgeMinutes(int maxAge) |
Cookie |
maxAgeSeconds(int maxAge) |
Cookie |
path(java.lang.String path)
Specific path, e.g.
|
Cookie |
sameSite(Cookie.SameSite policy) |
Cookie |
secure()
Sets the Secure flag, meaning HTTPS only.
|
static void |
toRequest(java.util.Map<java.lang.String,java.lang.String> nameValueMap,
com.sun.net.httpserver.HttpExchange htx) |
public static final java.lang.String SECRET_PREFIX
public static Cookie builder(java.lang.String name, java.lang.String value)
maxAgeSeconds(int)
or expires(int, Month, int, int, int)
are not set, the lifetime is usually bound to the browser-session.
However, many browsers restore session-cookies after computer crash.
Even maxAge and expire seem to work (or not work) differently under different
browsers. So don't rely on it.name
- NOT NULL. Expects url-friendly attribute name.value
- NOT NULL. Exects already url-Encoded String if necessary.Cookie
public final Cookie maxAge(java.lang.Integer maxAge)
maxAge
- in seconds or NULL in order to ignore this value (=SessionCookie).public final Cookie maxAgeSeconds(int maxAge)
maxAge
- in seconds or 0 in order to delete Cookiepublic final Cookie maxAgeMinutes(int maxAge)
maxAge
- in minutes or 0 in order to delete Cookiepublic final Cookie maxAgeHours(int maxAge)
maxAge
- in hours or 0 in order to delete Cookiepublic final Cookie path(java.lang.String path)
path
- Stringpublic final Cookie domain(java.lang.String domain)
domain
- Stringpublic final Cookie secure()
public final Cookie httpOnly()
public final Cookie sameSite(Cookie.SameSite policy)
policy
- Cookie.SameSite
public final Cookie encrypt(java.lang.String secretKey)
secretKey
- String NULL=no encrpytionfromHeaders(Headers, String)
public final Cookie expires(int year, java.time.Month month, int dayOfMonth, int hour, int minute)
maxAgeSeconds(int)
is set, this attribute
will not be sent, because the age has higher precedence.year
- intmonth
- intdayOfMonth
- Month
hour
- intminute
- intpublic final Cookie expiresToday(int hour, int minute)
hour
- intminute
- intpublic final Cookie expiresTomorrow(int hour, int minute)
hour
- intminute
- intpublic final Cookie expires(java.lang.Long unixTimestamp)
unixTimestamp
- Long NULLABLE (if null then omitted)public void buildAndAddToResponse(com.sun.net.httpserver.HttpExchange htx)
build()
and adds the Cookie to the
HttpExchange.getResponseHeaders()
.htx
- HttpExchange
public java.lang.String build()
public static java.util.Map<java.lang.String,java.lang.String> fromRequest(com.sun.net.httpserver.HttpExchange htx)
fromHeaders(Headers, String)
with null
secret key for Request.htx
- HttpExchange
public static java.util.Map<java.lang.String,java.lang.String> fromRequest(com.sun.net.httpserver.HttpExchange htx, java.lang.String secretKey)
fromHeaders(Headers, String)
for Request.htx
- HttpExchange
secretKey
- String NULLABLEpublic static java.util.Map<java.lang.String,java.lang.String> fromHeaders(com.sun.net.httpserver.Headers headers, java.lang.String secretKey)
headers
- Headers
secretKey
- key if encrypted, NULLABLE (null = no decryption)public static void toRequest(java.util.Map<java.lang.String,java.lang.String> nameValueMap, com.sun.net.httpserver.HttpExchange htx)