Class Cookie
java.lang.Object
de.craftsblock.craftsnet.api.http.cookies.Cookie
Represents a http cookie with various attributes such as name, value, path, domain, expiry date,
same-site policy, security, and HttpOnly flag.
- Since:
- 3.0.6
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares this cookie to the specified object for equality.Returns the domain of the cookie.Returns the expiry date of the cookie.longReturns the maximum age of the cookie.@NotNull StringgetName()Returns the name of the cookie.getPath()Returns the path of the cookie.Returns the SameSite policy of the cookie.<T> TgetValue()Retrieves the value of the cookie, cast to the specified type.inthashCode()Returns the hash code value for this cookie.booleanReturns whether the cookie is HttpOnly.booleanisSecure()Returns whether the cookie is secure.Marks the cookie as deleted by setting its expiry date to a date in the past.Overrides the current cookie's attributes with those of another cookie.Sets the domain of the cookie.setExpiresAt(@Nullable String expiresAt) Sets the expiry date of the cookie using a string representation.setExpiresAt(@Nullable OffsetDateTime expiresAt) Sets the expiry date of the cookie using a TemporalAccessor.Sets a specified attribute of the cookie based on the flag provided.setHttpOnly(boolean httpOnly) Sets whether the cookie is HttpOnly.setMaxAge(long maxAge) Sets the maximum age of this cookie.Sets the path of the cookie.setSameSite(@Nullable SameSite sameSite) Sets the SameSite policy of the cookie.setSameSite(@Nullable String sameSite) Sets the SameSite policy of the cookie using a string representation.setSecure(boolean secure) Sets whether the cookie is secure.Sets the value of the cookie.toString()Returns a string representation of the cookie.
-
Constructor Details
-
Cookie
Constructs a new Cookie with the specified name and no value.- Parameters:
name- The name of the cookie, cannot be null
-
Cookie
Constructs a new Cookie with the specified name and value.- Parameters:
name- The name of the cookie, cannot be nullvalue- The value of the cookie, can be null
-
-
Method Details
-
setFlag
Sets a specified attribute of the cookie based on the flag provided.This method allows setting various cookie attributes using a flag and its corresponding argument. Recognized flags are "Path", "Domain", "Expires", "SameSite", "Secure", and "HttpOnly".
- Parameters:
flag- The attribute to be set, cannot be nullarg- The value for the attribute, can be null for attributes that accept null values- Returns:
- The current Cookie object, for method chaining
-
getName
Returns the name of the cookie.- Returns:
- The name of the cookie
-
getValue
@Nullable public <T> T getValue()Retrieves the value of the cookie, cast to the specified type.This method returns the value of the cookie if it is set, casting it to the type specified by the caller. If the value is null, it returns null. The caller must ensure that the type cast is correct.
- Type Parameters:
T- The type to which the cookie value should be cast- Returns:
- The value of the cookie cast to the specified type, or null if the value is not set
- Throws:
ClassCastException- If the value cannot be cast to the specified type
-
setValue
Sets the value of the cookie.- Parameters:
value- The new value of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
getPath
Returns the path of the cookie.- Returns:
- The path of the cookie, or null if not set
-
setPath
Sets the path of the cookie.- Parameters:
path- The new path of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
getDomain
Returns the domain of the cookie.- Returns:
- The domain of the cookie, or null if not set
-
setDomain
Sets the domain of the cookie.- Parameters:
domain- The new domain of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
getExpiresAt
Returns the expiry date of the cookie.- Returns:
- The expiry date of the cookie, or null if not set
-
setExpiresAt
Sets the expiry date of the cookie using a string representation.- Parameters:
expiresAt- The new expiry date of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
setExpiresAt
Sets the expiry date of the cookie using a TemporalAccessor.- Parameters:
expiresAt- The new expiry date of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
getMaxAge
public long getMaxAge()Returns the maximum age of the cookie.- Returns:
- The maximum age of the cookie, if disabled -1 will be returned.
-
setMaxAge
Sets the maximum age of this cookie. Set to -2 to disable this flag.- Parameters:
maxAge- The number in seconds the cookie is valid.- Returns:
- The current Cookie object, for method chaining
-
getSameSite
Returns the SameSite policy of the cookie.- Returns:
- The SameSite policy of the cookie, or null if not set
-
setSameSite
Sets the SameSite policy of the cookie using a string representation.- Parameters:
sameSite- The new SameSite policy of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
setSameSite
Sets the SameSite policy of the cookie.- Parameters:
sameSite- The new SameSite policy of the cookie, can be null- Returns:
- The current Cookie object, for method chaining
-
isSecure
public boolean isSecure()Returns whether the cookie is secure.- Returns:
- true if the cookie is secure, false otherwise
-
setSecure
Sets whether the cookie is secure.- Parameters:
secure- true to make the cookie secure, false otherwise- Returns:
- The current Cookie object, for method chaining
-
isHttpOnly
public boolean isHttpOnly()Returns whether the cookie is HttpOnly.- Returns:
- true if the cookie is HttpOnly, false otherwise
-
setHttpOnly
Sets whether the cookie is HttpOnly.- Parameters:
httpOnly- true to make the cookie HttpOnly, false otherwise- Returns:
- The current Cookie object, for method chaining
-
override
Overrides the current cookie's attributes with those of another cookie.- Parameters:
cookie- The cookie to copy attributes from- Returns:
- The current Cookie object, for method chaining
-
markDeleted
Marks the cookie as deleted by setting its expiry date to a date in the past.- Returns:
- The current Cookie object, for method chaining
-
toString
Returns a string representation of the cookie. -
equals
Compares this cookie to the specified object for equality. -
hashCode
public int hashCode()Returns the hash code value for this cookie.
-