- Record Components:
content- The body content as a stringcontentType- The MIME content type (e.g., "application/json", "text/html")encoding- The content encoding (e.g., "gzip", "deflate", "" for none)
This record encapsulates the structure of HTTP message bodies, providing a type-safe way to handle body data in HTTP security validation. It supports various content types and encoding schemes commonly used in HTTP communications.
Design Principles
- Immutability - All fields are final and the record cannot be modified
- Type Safety - Strongly typed representation of HTTP body data
- Encoding Awareness - Explicit handling of content encoding
- Content Type Support - Supports MIME type specification
Usage Examples
// JSON body
HTTPBody jsonBody = new HTTPBody(
"{\"userId\": 123, \"name\": \"John\"}",
"application/json",
""
);
// Form data
HTTPBody formBody = new HTTPBody(
"username=admin&password=secret",
"application/x-www-form-urlencoded",
""
);
// Compressed content
HTTPBody compressedBody = new HTTPBody(
"...", // compressed content
"text/html",
"gzip"
);
// Access components
String content = body.content(); // The actual content
String contentType = body.contentType(); // "application/json"
String encoding = body.encoding(); // "gzip"
// Check content characteristics
boolean isJson = body.isJson(); // true for JSON content
boolean hasContent = body.hasContent(); // true if content is not empty
boolean isCompressed = body.isCompressed(); // true if encoding is specified
// Use in validation
validator.validate(body.content(), ValidationType.BODY);
Content Types
The contentType field should contain a valid MIME type (e.g., "application/json", "text/html", "multipart/form-data"). An empty string indicates no content type is specified.
Encoding
The encoding field specifies content encoding such as "gzip", "deflate", "br" (Brotli), or "" for no encoding. This is distinct from character encoding, which is typically specified in the Content-Type header.
Security Considerations
This record is a simple data container. Security validation should be applied to
the content using appropriate validators for ValidationType.BODY, taking into
account the content type and encoding when determining validation strategies.
- Since:
- 1.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@Nullable Stringcontent()Returns the value of thecontentrecord component.intReturns the content length in characters.contentOrDefault(String defaultContent) Returns the content or a default value if content is null.contentTruncated(int maxLength) Returns a truncated version of the content for safe logging.@Nullable StringReturns the value of thecontentTyperecord component.contentTypeOrDefault(String defaultContentType) Returns the content type or a default value if content type is null.@Nullable Stringencoding()Returns the value of theencodingrecord component.encodingOrDefault(String defaultEncoding) Returns the encoding or a default value if encoding is null.final booleanIndicates whether some other object is "equal to" this one.static HTTPBodyExtracts the charset from the content type if specified.booleanbooleanbooleanfinal inthashCode()Returns a hash code value for this object.static HTTPBodybooleanisBinary()Checks if the content type indicates binary content.booleanbooleanChecks if the content type indicates form data.booleanisHtml()Checks if the content type indicates HTML content.booleanisJson()booleanChecks if the content type indicates plain text.booleanisXml()Checks if the content type indicates XML content.static HTTPBodystatic HTTPBodystatic HTTPBodyfinal StringtoString()Returns a string representation of this record class.withContent(String newContent) Returns a copy of this body with new content.withContentType(String newContentType) Returns a copy of this body with a new content type.withEncoding(String newEncoding) Returns a copy of this body with a new encoding.
-
Constructor Details
-
HTTPBody
Creates an instance of aHTTPBodyrecord class.- Parameters:
content- the value for thecontentrecord componentcontentType- the value for thecontentTyperecord componentencoding- the value for theencodingrecord component
-
-
Method Details
-
of
-
text
-
json
-
html
-
form
-
hasContent
-
hasContentType
-
hasEncoding
-
isCompressed
-
isJson
-
isXml
Checks if the content type indicates XML content.- Returns:
- true if the content type contains "xml"
-
isHtml
Checks if the content type indicates HTML content.- Returns:
- true if the content type contains "html"
-
isPlainText
Checks if the content type indicates plain text.- Returns:
- true if the content type is "text/plain"
-
isFormData
Checks if the content type indicates form data.- Returns:
- true if the content type is form-encoded
-
isBinary
Checks if the content type indicates binary content.- Returns:
- true if the content type suggests binary data
-
contentLength
Returns the content length in characters.- Returns:
- The length of the content string, or 0 if content is null
-
getCharset
Extracts the charset from the content type if specified.- Returns:
- The charset name wrapped in Optional, or empty if not specified
-
contentOrDefault
Returns the content or a default value if content is null.- Parameters:
defaultContent- The default content to return if content is null- Returns:
- The content or the default
-
contentTypeOrDefault
Returns the content type or a default value if content type is null.- Parameters:
defaultContentType- The default content type to return if contentType is null- Returns:
- The content type or the default
-
encodingOrDefault
Returns the encoding or a default value if encoding is null.- Parameters:
defaultEncoding- The default encoding to return if encoding is null- Returns:
- The encoding or the default
-
withContent
Returns a copy of this body with new content.- Parameters:
newContent- The new content- Returns:
- A new HTTPBody with the specified content and same contentType/encoding
-
withContentType
Returns a copy of this body with a new content type.- Parameters:
newContentType- The new content type- Returns:
- A new HTTPBody with the same content/encoding and specified content type
-
withEncoding
Returns a copy of this body with a new encoding.- Parameters:
newEncoding- The new encoding- Returns:
- A new HTTPBody with the same content/contentType and specified encoding
-
contentTruncated
Returns a truncated version of the content for safe logging.- Parameters:
maxLength- The maximum length for the truncated content- Returns:
- The content truncated to the specified length with "..." if truncated
-
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). -
content
Returns the value of thecontentrecord component.- Returns:
- the value of the
contentrecord component
-
contentType
Returns the value of thecontentTyperecord component.- Returns:
- the value of the
contentTyperecord component
-
encoding
Returns the value of theencodingrecord component.- Returns:
- the value of the
encodingrecord component
-