java.lang.Object
de.craftsblock.craftsnet.api.http.body.Body
de.craftsblock.craftsnet.api.http.body.bodies.FormBody<T>
Type Parameters:
T - The type of data that each field in the form body holds.
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
MultipartFormBody, StandardFormBody

public abstract class FormBody<T> extends Body
The FormBody class is an abstract base class for representing HTTP request bodies that contain form data. It provides common functionality for handling form data, such as deserialization and field retrieval.
Since:
2.2.0-SNAPSHOT
See Also:
  • Field Details

  • Constructor Details

    • FormBody

      public FormBody(Request request, InputStream body) throws IOException
      Constructs a new FormBody with the given input stream and deserializes the form data if it is of the standard form body type.
      Parameters:
      request - The representation of the http request.
      body - The input stream containing the form data.
      Throws:
      IOException - If an error occurs while reading or parsing the form data.
  • Method Details

    • deserialize

      protected abstract void deserialize() throws IOException
      This method should be implemented by concrete subclasses to handle the deserialization of form data from the input stream and populate the internal data map.
      Throws:
      IOException - If an error occurs while reading or parsing the form data.
    • close

      public void close()
      Clears the internal data map when the form body is closed. This is important to release resources and ensure that the form body is in a clean state after processing.
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close in class Body
    • hasField

      public abstract boolean hasField(String name)
      Checks if a specific field exists in the form data.
      Parameters:
      name - The name of the field to check.
      Returns:
      true if the field exists, otherwise false.
    • getField

      public abstract T getField(String name)
      Retrieves the value of a specific field from the form data.
      Parameters:
      name - The name of the field to retrieve.
      Returns:
      The value of the field, or null if the field does not exist.
    • getBody

      public final InputStream getBody()
      Returns the raw input stream containing the form data.
      Returns:
      The input stream.
    • getData

      public final ConcurrentHashMap<String,T> getData()
      Returns the concurrent hash map containing the form fields and their values.
      Returns:
      The data map.