java.lang.Object
com.renomad.minum.web.Body
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 Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]
asBytes()
Returns the raw bytes of this HTTP message's body.byte[]
Return the bytes of this request body by its key.asString()
Return the entire raw contents of the body of this request, as a string.Return the value for a key, as a string.boolean
Returns theBodyType
, which is necessary to distinguish which methods to run for accessing data.getKeys()
Get all the keys for the key-value pairs in the bodygetPartitionByName
(String name) A helper method for getting the partitions with a particular name set in its content-disposition.If the body is of type form/multipart, return the partitionsint
hashCode()
toString()
-
Field Details
-
EMPTY
An empty body instance, useful when you need an instantiated body.
-
-
Constructor Details
-
Body
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 bodypartitions
- if the body is of type form/multipart, these will be the list of partitions
-
-
Method Details
-
asString
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
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
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
If the body is of type form/multipart, return the partitionsFor 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
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
Returns theBodyType
, 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 likegetKeys()
,asBytes(String)
, orasString(String)
On the other hand, if the type is MULTIPART, you will need to usegetPartitionHeaders()
to get a list of the partitions.
If the body type is UNRECOGNIZED, you can useasBytes()
to get the body.
Don't forget, there is also an option to obtain the body'sInputStream
by usingRequest.getSocketWrapper()
, but that needs to be done before runningRequest.getBody()
-
getKeys
Get all the keys for the key-value pairs in the body -
equals
-
hashCode
public int hashCode() -
toString
-