public class EncoderRegistry extends Object
This class handles creation of the request body (i.e. for a
PUT or POST operation) based on content-type. When a
body
is set from the builder, it is
processed based on the request content-type
. For instance, the encodeForm(Map)
method
will be invoked if the request content-type is form-urlencoded, which will
cause the following:body=[a:1, b:'two']
to be encoded as
the equivalent a=1&b=two
in the request body.
Most default encoders can handle a closure as a request body. In this
case, the closure is executed and a suitable 'builder' passed to the
closure that is used for constructing the content. In the case of
binary encoding this would be an OutputStream; for TEXT encoding it would
be a PrintWriter, and for XML it would be an already-bound
StreamingMarkupBuilder
. See each encode...
method
for details for each particular content-type.
Contrary to its name, this class does not have anything to do with the
content-encoding
HTTP header.
Constructor and Description |
---|
EncoderRegistry() |
Modifier and Type | Method and Description |
---|---|
protected Map<String,groovy.lang.Closure> |
buildDefaultEncoderMap()
Returns a map of default encoders.
|
protected org.apache.http.HttpEntity |
createEntity(String ct,
byte[] byteArray) |
protected org.apache.http.HttpEntity |
createEntity(String ct,
String data)
Helper method used by encoder methods to create an
HttpEntity
instance that encapsulates the request data. |
org.apache.http.client.entity.UrlEncodedFormEntity |
encodeForm(Map<?,?> params)
Set the request body as a url-encoded list of parameters.
|
org.apache.http.HttpEntity |
encodeForm(Object contentType,
String formData)
Accepts a String as a url-encoded form post.
|
org.apache.http.HttpEntity |
encodeJSON(Object contentType,
Object model)
Accepts a Collection or a JavaBean object which is converted to JSON.
|
org.apache.http.entity.InputStreamEntity |
encodeStream(Object contentType,
Object data)
Default request encoder for a binary stream.
|
org.apache.http.HttpEntity |
encodeText(Object contentType,
Object data)
Default handler used for a plain text content-type.
|
org.apache.http.HttpEntity |
encodeXML(Object contentType,
Object xml)
Encode the content as XML.
|
groovy.lang.Closure |
getAt(Object contentType)
Retrieve a encoder for the given content-type.
|
Iterator<Map.Entry<String,groovy.lang.Closure>> |
iterator()
Iterate over the entire parser map
|
groovy.lang.Closure |
propertyMissing(Object key)
Alias for
getAt(Object) to allow property-style access. |
void |
propertyMissing(Object key,
groovy.lang.Closure value)
Alias for
putAt(Object, Closure) to allow property-style access. |
void |
putAt(Object contentType,
groovy.lang.Closure value)
Register a new encoder for the given content type.
|
void |
setEncoderConfig(EncoderConfig encoderConfig)
Set the encoder config
|
public void setEncoderConfig(EncoderConfig encoderConfig)
public org.apache.http.entity.InputStreamEntity encodeStream(Object contentType, Object data) throws UnsupportedEncodingException
data
- HttpEntity
encapsulating this request dataUnsupportedEncodingException
public org.apache.http.HttpEntity encodeText(Object contentType, Object data) throws IOException
PrintWriter
is passed as the single
argument to the closure. Any data sent to the writer from the
closure will be sent to the request content body.data
- HttpEntity
encapsulating this request dataIOException
public org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(Map<?,?> params) throws UnsupportedEncodingException
[ key1 : ['val1', 'val2'], key2 : 'etc.' ]
params
- HttpEntity
encapsulating this request dataUnsupportedEncodingException
public org.apache.http.HttpEntity encodeForm(Object contentType, String formData) throws UnsupportedEncodingException
formData
- a url-encoded form POST string. See
The W3C spec for more info.HttpEntity
encapsulating this request dataUnsupportedEncodingException
public org.apache.http.HttpEntity encodeXML(Object contentType, Object xml) throws UnsupportedEncodingException
toString
produces valid markup, or a Closure which will be
interpreted as a builder definition.xml
- data that defines the XML structureHttpEntity
encapsulating this request dataUnsupportedEncodingException
public org.apache.http.HttpEntity encodeJSON(Object contentType, Object model) throws UnsupportedEncodingException
Accepts a Collection or a JavaBean object which is converted to JSON.
A Map or Collection will be converted to a JsonBuilder
.. A
String or GString will be interpreted as valid JSON and passed directly
as the request body (with charset conversion if necessary.)
If a Closure is passed as the model, it will be executed as if it were
a JSON object definition passed to a JsonBuilder
. In order
for the closure to be interpreted correctly, there must be a 'root'
element immediately inside the closure. For example:
builder.post( JSON ) { body = { root { first { one = 1 two = '2' } second = 'some string' } } }
will return the following JSON string:
{"root":{"first":{"one":1,"two":"2"},"second":"some string"}}
model
- data to be converted to JSON, as specified above.HttpEntity
encapsulating this request dataUnsupportedEncodingException
protected org.apache.http.HttpEntity createEntity(String ct, byte[] byteArray)
protected org.apache.http.HttpEntity createEntity(String ct, String data) throws UnsupportedEncodingException
HttpEntity
instance that encapsulates the request data. This may be used by any
non-streaming encoder that needs to send textual data.ct
- content-type of the datadata
- textual request data to be encodedrequest content
UnsupportedEncodingException
protected Map<String,groovy.lang.Closure> buildDefaultEncoderMap()
super.buildDefaultEncoderMap()
and then add or remove
from that result as well.public groovy.lang.Closure getAt(Object contentType)
contentType
- public void putAt(Object contentType, groovy.lang.Closure value)
HttpEntity
. It will also usually
accept a single argument, which will be whatever is set in the request
configuration closure via HTTPBuilder.RequestConfigDelegate.setBody(Object)
.contentType
- public groovy.lang.Closure propertyMissing(Object key)
getAt(Object)
to allow property-style access.key
- public void propertyMissing(Object key, groovy.lang.Closure value)
putAt(Object, Closure)
to allow property-style access.key
- value
- Copyright © 2010–2017. All rights reserved.