Class Body

java.lang.Object
com.renomad.minum.web.Body

public final class Body extends Object
This class represents the body of an HTML message. See Message Body on Wikipedia

 This could be a response from the web server:

 HTTP/1.1 200 OK
 Date: Sun, 10 Oct 2010 23:26:07 GMT
 Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8 OpenSSL/0.9.8g
 Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT
 ETag: "45b6-834-49130cc1182c0"
 Accept-Ranges: bytes
 Content-Length: 12
 Connection: close
 Content-Type: text/html

 Hello world!
 

The message body (or content) in this example is the text

Hello world!
.

  • Field Details

    • EMPTY

      public static final Body EMPTY
      An empty body instance, useful when you need an instantiated body.
  • Constructor Details

    • Body

      public Body(Map<String,byte[]> bodyMap, byte[] raw, List<Partition> partitions, BodyType bodyType)
      Build a body for an HTTP message
      Parameters:
      bodyMap - a map of key-value pairs, presumably extracted from form data. Empty if our body isn't one of the form data protocols we understand.
      raw - the raw bytes of this body
      partitions - if the body is of type form/multipart, these will be the list of partitions
  • Method Details

    • asString

      public String asString(String key)
      Return the value for a key, as a string. This method presumes the data was sent URL-encoded.

      If there is no value found for the provided key, an empty string will be returned.

      Otherwise, the value found will be converted to a string, and trimmed.

      Note: if the request is a multipart/form-data, this method will throw a helpful exception to indicate that.

    • asString

      public String asString()
      Return the entire raw contents of the body of this request, as a string. No processing involved other than converting the bytes to a string.
    • asBytes

      public byte[] asBytes(String key)
      Return the bytes of this request body by its key. This method presumes the data was sent URL-encoded.
    • asBytes

      public byte[] asBytes()
      Returns the raw bytes of this HTTP message's body. This method presumes the data was sent URL-encoded.
    • getPartitionHeaders

      public List<Partition> getPartitionHeaders()
      If the body is of type form/multipart, return the partitions

      For example:

       --i_am_a_boundary
        Content-Type: text/plain
        Content-Disposition: form-data; name="text1"
      
        I am a value that is text
        --i_am_a_boundary
        Content-Type: application/octet-stream
        Content-Disposition: form-data; name="image_uploads"; filename="photo_preview.jpg"
       
    • getPartitionByName

      public List<Partition> getPartitionByName(String name)
      A helper method for getting the partitions with a particular name set in its content-disposition. This returns a list of partitions because there is nothing preventing the browser doing this, and in fact it will typically send partitions with the same name when sending multiple files from one input. (HTML5 provides the ability to select multiple files on the input with type=file)
    • getBodyType

      public BodyType getBodyType()
      Returns the BodyType, which is necessary to distinguish which methods to run for accessing data. For instance, if the body is of type FORM_URL_ENCODED, you may use methods like getKeys(), asBytes(String), or asString(String)
      On the other hand, if the type is MULTIPART, you will need to use getPartitionHeaders() to get a list of the partitions.
      If the body type is UNRECOGNIZED, you can use asBytes() to get the body.
      Don't forget, there is also an option to obtain the body's InputStream by using Request.getSocketWrapper(), but that needs to be done before running Request.getBody()
    • getKeys

      public Set<String> getKeys()
      Get all the keys for the key-value pairs in the body
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object