Record Class URLParameter

java.lang.Object
java.lang.Record
de.cuioss.http.security.data.URLParameter
Record Components:
name - The parameter name (e.g., "userId", "page", "filter")
value - The parameter value (e.g., "12345", "admin", "active")

public record URLParameter(@Nullable String name, @Nullable String value) extends Record
Immutable record representing a URL query parameter with name and value.

This record encapsulates the key-value pair structure of URL query parameters, providing a type-safe way to handle parameter data in HTTP security validation.

Design Principles

  • Immutability - All fields are final and the record cannot be modified
  • Type Safety - Strongly typed representation of parameter data
  • Null Safety - Explicit handling of null values with clear semantics
  • Value Semantics - Records provide automatic equals/hashCode/toString

Usage Examples

 // Create a parameter
 URLParameter param = new URLParameter("userId", "12345");

 // Access components
 String name = param.name();     // "userId"
 String value = param.value();   // "12345"

 // Use in validation
 validator.validate(param.name(), ValidationType.PARAMETER_NAME);
 validator.validate(param.value(), ValidationType.PARAMETER_VALUE);

 // Parameters are value objects
 URLParameter param2 = new URLParameter("userId", "12345");
 assert param.equals(param2);  // true
 

Null Handling

Both name and value can be null to represent edge cases in HTTP parsing, though null parameter names are typically invalid in well-formed URLs.

Security Considerations

This record is a simple data container. Security validation should be applied to the name and value components separately using appropriate validators for ValidationType.PARAMETER_NAME and ValidationType.PARAMETER_VALUE.

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

    • URLParameter

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

    • withEmptyValue

      public static URLParameter withEmptyValue(String name)
      Creates a URLParameter with empty value. Useful for parameters that appear without values (e.g., "?flag" instead of "?flag=value").
      Parameters:
      name - The parameter name, should not be null
      Returns:
      A URLParameter with the specified name and empty string value
    • hasName

      public boolean hasName()
      Checks if this parameter has a non-null, non-empty name.
      Returns:
      true if the name is not null and not empty
    • hasValue

      public boolean hasValue()
      Checks if this parameter has a non-null, non-empty value.
      Returns:
      true if the value is not null and not empty
    • isFlag

      public boolean isFlag()
      Checks if this parameter represents a flag (has name but no meaningful value). A parameter is considered a flag if it has a name but the value is null or empty.
      Returns:
      true if this appears to be a flag parameter
    • nameOrDefault

      public String nameOrDefault(String defaultName)
      Returns the parameter name, or a default value if the name is null.
      Parameters:
      defaultName - The default name to return if name is null
      Returns:
      The parameter name or the default
    • valueOrDefault

      public String valueOrDefault(String defaultValue)
      Returns the parameter value, or a default value if the value is null.
      Parameters:
      defaultValue - The default value to return if value is null
      Returns:
      The parameter value or the default
    • toParameterString

      Returns a string representation suitable for URL encoding. Note: This does not perform actual URL encoding - use appropriate encoding utilities for that purpose.
      Returns:
      A string in the format "name=value" or "name" for flag parameters
    • withName

      public URLParameter withName(String newName)
    • withValue

      public URLParameter withValue(String newValue)
    • 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.
    • name

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

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