Record Class HTTPBody

java.lang.Object
java.lang.Record
de.cuioss.http.security.data.HTTPBody
Record Components:
content - The body content as a string
contentType - The MIME content type (e.g., "application/json", "text/html")
encoding - The content encoding (e.g., "gzip", "deflate", "" for none)

public record HTTPBody(@Nullable String content, @Nullable String contentType, @Nullable String encoding) extends Record
Immutable record representing an HTTP request or response body with content, content type, and encoding.

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.

Implements: Task B3 from HTTP verification specification
Since:
1.0
See Also:
  • Constructor Details

    • HTTPBody

      public HTTPBody(@Nullable String content, @Nullable String contentType, @Nullable String encoding)
      Creates an instance of a HTTPBody record class.
      Parameters:
      content - the value for the content record component
      contentType - the value for the contentType record component
      encoding - the value for the encoding record component
  • Method Details

    • of

      public static HTTPBody of(String content, String contentType)
    • text

      public static HTTPBody text(String content)
    • json

      public static HTTPBody json(String jsonContent)
    • html

      public static HTTPBody html(String htmlContent)
    • form

      public static HTTPBody form(String formContent)
    • hasContent

      public boolean hasContent()
    • hasContentType

      public boolean hasContentType()
    • hasEncoding

      public boolean hasEncoding()
    • isCompressed

      public boolean isCompressed()
    • isJson

      public boolean isJson()
    • isXml

      public boolean isXml()
      Checks if the content type indicates XML content.
      Returns:
      true if the content type contains "xml"
    • isHtml

      public boolean isHtml()
      Checks if the content type indicates HTML content.
      Returns:
      true if the content type contains "html"
    • isPlainText

      public boolean isPlainText()
      Checks if the content type indicates plain text.
      Returns:
      true if the content type is "text/plain"
    • isFormData

      public boolean isFormData()
      Checks if the content type indicates form data.
      Returns:
      true if the content type is form-encoded
    • isBinary

      public boolean isBinary()
      Checks if the content type indicates binary content.
      Returns:
      true if the content type suggests binary data
    • contentLength

      public int 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

      public String contentOrDefault(String defaultContent)
      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

      public String contentTypeOrDefault(String defaultContentType)
      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

      public String encodingOrDefault(String defaultEncoding)
      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

      public HTTPBody withContent(String newContent)
      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

      public HTTPBody withContentType(String newContentType)
      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

      public HTTPBody withEncoding(String newEncoding)
      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

      public String contentTruncated(int maxLength)
      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

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • content

      public @Nullable String content()
      Returns the value of the content record component.
      Returns:
      the value of the content record component
    • contentType

      public @Nullable String contentType()
      Returns the value of the contentType record component.
      Returns:
      the value of the contentType record component
    • encoding

      public @Nullable String encoding()
      Returns the value of the encoding record component.
      Returns:
      the value of the encoding record component