Package nl.colorize.util.http
Class PostData
java.lang.Object
nl.colorize.util.http.PostData
Represents data that has been sent as part of an HTTP request and is
encoded using the
application/x-www-form-urlencoded
content type.
This is commonly used in URL query strings, and in "classic" HTML forms
as documented in sections 4.10.21.7 and 4.10.21.8 of the
HTML specification.
Post data parameters consist of name/value pairs. This class provides two
ways to retrieve parameter values: getRequiredParameter(String)
(which will throw an exception if the parameter is not present), and
getOptionalParameter(String, String)
(which will return a default
value for missing parameters). Note that the parameter is considered
"missing" if it is either not present at all, or if the parameter name is
specified but its value is empty.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns true if thisPostData
contains at least one parameter with the specified name.static PostData
Creates aPostData
instance from a number of key/value pairs.static PostData
Creates aPostData
instance from existing key/value pairs.static PostData
empty()
Utility method that returns aPostData
instance that indicates no parameters have been specified.encode()
Encodes thisPostData
into a string that follows theapplication/x-www-form-urlencoded
notation, using the UTF-8 character encoding.Encodes thisPostData
into a string that follows theapplication/x-www-form-urlencoded
notation.boolean
getOptionalParameter
(String name, String defaultValue) Returns either the value of the parameter with the specified name, ordefaultValue
if no parameter with that name exists.getParameterValues
(String name) Returns all values for the parameter with the specified name.getRequiredParameter
(String name) Returns the value of the parameter with the specified name.int
hashCode()
boolean
isEmpty()
Creates a newPostData
instance by merging all parameters from this instance with another one.static PostData
ParsesPostData
from the specified string using the UTF-8 character encoding.static PostData
ParsesPostData
from the specified string.toMap()
Deprecated.Using a map does not allow for an explicit iteration order, and it also does not allow multiple parameters to use the same name.toString()
-
Method Details
-
getRequiredParameter
Returns the value of the parameter with the specified name. If multiple parameters match the requested name, the first occurrence will be returned. UsegetParameterValues(String)
if you want to retrieve all matching parameters.- Throws:
IllegalStateException
- if no parameter with that name exists.
-
getOptionalParameter
Returns either the value of the parameter with the specified name, ordefaultValue
if no parameter with that name exists. -
getParameterValues
Returns all values for the parameter with the specified name. Returns an empty list if no matching parameters exist. This can also return multiple values, as it is allowed for multiple parameters to have the same name. -
contains
Returns true if thisPostData
contains at least one parameter with the specified name. -
isEmpty
public boolean isEmpty() -
toMap
Deprecated.Using a map does not allow for an explicit iteration order, and it also does not allow multiple parameters to use the same name.Creates a map that includes all parameters in thisPostData
. If multiple parameters share the same name, the map will include the first occurrence. -
merge
Creates a newPostData
instance by merging all parameters from this instance with another one.- Throws:
IllegalArgumentException
- if both instances contain a parameter with the same name.
-
encode
-
encode
-
equals
-
hashCode
public int hashCode() -
toString
-
create
Creates aPostData
instance from a number of key/value pairs. When specifying multiple key/value pairs, therest
parameter expects the orderkey1, value1, key2, value2, ...
.- Throws:
IllegalArgumentException
- if the number of parameters is not a multiple of two.
-
create
Creates aPostData
instance from existing key/value pairs. The parameter order will be based on the map's iteration order. -
parse
ParsesPostData
from the specified string. The input is expected to follow theapplication/x-www-form-urlencoded
notation, for exampleb=2&c=3
. If the encoded input string is empty, this will returnempty()
. -
parse
ParsesPostData
from the specified string using the UTF-8 character encoding. The input is expected to follow theapplication/x-www-form-urlencoded
notation, for exampleb=2&c=3
. If the encoded input string is empty, this will returnempty()
. -
empty
Utility method that returns aPostData
instance that indicates no parameters have been specified. Encoding this instance will produce an empty string.
-