java.lang.Object
java.lang.Record
de.cuioss.http.security.data.Cookie
- Record Components:
name- The cookie name (e.g., "JSESSIONID", "auth_token")value- The cookie value (e.g., session ID, authentication token)attributes- Cookie attributes string (e.g., "Domain=example.com; Secure; HttpOnly")
public record Cookie(@Nullable String name, @Nullable String value, @Nullable String attributes)
extends Record
Immutable record representing an HTTP cookie with name, value, and attributes.
This record encapsulates the structure of HTTP cookies as defined in RFC 6265, providing a type-safe way to handle cookie data in HTTP security validation.
Design Principles
- Immutability - All fields are final and the record cannot be modified
- RFC Compliance - Follows HTTP cookie specifications
- Security Focus - Designed with security validation in mind
- Flexibility - Supports various cookie attribute formats
Usage Examples
// Simple cookie
Cookie sessionCookie = new Cookie("JSESSIONID", "ABC123", "");
// Cookie with attributes
Cookie secureCookie = new Cookie(
"auth_token",
"xyz789",
"Domain=example.com; Path=/; Secure; HttpOnly"
);
// Access components
String name = cookie.name(); // "JSESSIONID"
String value = cookie.value(); // "ABC123"
String attrs = cookie.attributes(); // "Domain=..."
// Check for security attributes
boolean isSecure = cookie.isSecure(); // Check for Secure attribute
boolean isHttpOnly = cookie.isHttpOnly(); // Check for HttpOnly attribute
// Use in validation
validator.validate(cookie.name(), ValidationType.COOKIE_NAME);
validator.validate(cookie.value(), ValidationType.COOKIE_VALUE);
Cookie Attributes
The attributes field contains the semicolon-separated list of cookie attributes such as Domain, Path, Secure, HttpOnly, SameSite, and Max-Age. This field can be an empty string if no attributes are present.
Security Considerations
This record is a simple data container. Security validation should be applied to the name, value, and attributes components separately using appropriate validators.
Implements: Task B3 from HTTP verification specification- Since:
- 1.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@Nullable StringReturns the value of theattributesrecord component.final booleanIndicates whether some other object is "equal to" this one.Returns all attribute names present in this cookie.Extracts the Domain attribute value if present.Extracts the Max-Age attribute value if present.getPath()Extracts the Path attribute value if present.Extracts the SameSite attribute value if present.booleanChecks if this cookie has any attributes.final inthashCode()Returns a hash code value for this object.booleanhasName()Checks if this cookie has a non-null, non-empty name.booleanhasValue()Checks if this cookie has a non-null, non-empty value.booleanChecks if the cookie has the HttpOnly attribute.booleanisSecure()Checks if the cookie has the Secure attribute.@Nullable Stringname()Returns the value of thenamerecord component.nameOrDefault(String defaultName) static CookieCreates a simple cookie with no attributes.Returns a string representation suitable for HTTP Set-Cookie headers.final StringtoString()Returns a string representation of this record class.@Nullable Stringvalue()Returns the value of thevaluerecord component.valueOrDefault(String defaultValue) withAttributes(String newAttributes) Returns a copy of this cookie with new attributes.Returns a copy of this cookie with a new name.Returns a copy of this cookie with a new value.
-
Constructor Details
-
Cookie
Creates an instance of aCookierecord class.- Parameters:
name- the value for thenamerecord componentvalue- the value for thevaluerecord componentattributes- the value for theattributesrecord component
-
-
Method Details
-
simple
Creates a simple cookie with no attributes.- Parameters:
name- The cookie namevalue- The cookie value- Returns:
- A Cookie with no attributes
-
hasName
Checks if this cookie has a non-null, non-empty name.- Returns:
- true if the name is not null and not empty
-
hasValue
Checks if this cookie has a non-null, non-empty value.- Returns:
- true if the value is not null and not empty
-
hasAttributes
Checks if this cookie has any attributes.- Returns:
- true if the attributes string is not null and not empty
-
isSecure
Checks if the cookie has the Secure attribute.- Returns:
- true if the attributes contain "Secure"
-
isHttpOnly
Checks if the cookie has the HttpOnly attribute.- Returns:
- true if the attributes contain "HttpOnly"
-
getDomain
Extracts the Domain attribute value if present.- Returns:
- The domain value wrapped in Optional, or empty if not specified
-
getPath
Extracts the Path attribute value if present.- Returns:
- The path value wrapped in Optional, or empty if not specified
-
getSameSite
Extracts the SameSite attribute value if present.- Returns:
- The SameSite value (e.g., "Strict", "Lax", "None") wrapped in Optional, or empty if not specified
-
getMaxAge
Extracts the Max-Age attribute value if present.- Returns:
- The Max-Age value as a string wrapped in Optional, or empty if not specified
-
getAttributeNames
Returns all attribute names present in this cookie.- Returns:
- A list of attribute names (may be empty)
-
nameOrDefault
-
valueOrDefault
-
toCookieString
Returns a string representation suitable for HTTP Set-Cookie headers. Note: This does not perform proper HTTP encoding - use appropriate encoding utilities for actual HTTP header generation.- Returns:
- A string in the format "name=value; attributes"
-
withName
Returns a copy of this cookie with a new name.- Parameters:
newName- The new cookie name- Returns:
- A new Cookie with the specified name and same value/attributes
-
withValue
Returns a copy of this cookie with a new value.- Parameters:
newValue- The new cookie value- Returns:
- A new Cookie with the same name/attributes and specified value
-
withAttributes
Returns a copy of this cookie with new attributes.- Parameters:
newAttributes- The new attributes string- Returns:
- A new Cookie with the same name/value and specified attributes
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
name
Returns the value of thenamerecord component.- Returns:
- the value of the
namerecord component
-
value
Returns the value of thevaluerecord component.- Returns:
- the value of the
valuerecord component
-
attributes
Returns the value of theattributesrecord component.- Returns:
- the value of the
attributesrecord component
-