|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectgroovyx.net.http.EncoderRegistry
public class EncoderRegistry
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.
HTTPBuilder.RequestConfigDelegate.setBody(Object),
HTTPBuilder.RequestConfigDelegate.send(Object, Object)| Constructor Summary | |
|---|---|
EncoderRegistry()
|
|
| Method Summary | |
|---|---|
protected Map<String,Closure> |
buildDefaultEncoderMap()
Returns a map of default encoders. |
protected StringEntity |
createEntity(Object ct,
String data)
Helper method used by encoder methods to create an HttpEntity
instance that encapsulates the request data. |
UrlEncodedFormEntity |
encodeForm(Map<?,?> params)
Set the request body as a url-encoded list of parameters. |
UrlEncodedFormEntity |
encodeForm(Map<?,?> params,
Object contentType)
|
HttpEntity |
encodeForm(String formData,
Object contentType)
Accepts a String as a url-encoded form post. |
HttpEntity |
encodeJSON(Object model,
Object contentType)
Accepts a Collection or a JavaBean object which is converted to JSON. |
InputStreamEntity |
encodeStream(Object data,
Object contentType)
Default request encoder for a binary stream. |
HttpEntity |
encodeText(Object data,
Object contentType)
Default handler used for a plain text content-type. |
HttpEntity |
encodeXML(Object xml,
Object contentType)
Encode the content as XML. |
Closure |
getAt(Object contentType)
Retrieve a encoder for the given content-type. |
Iterator<Map.Entry<String,Closure>> |
iterator()
Iterate over the entire parser map |
Closure |
propertyMissing(Object key)
Alias for getAt(Object) to allow property-style access. |
void |
propertyMissing(Object key,
Closure value)
Alias for putAt(Object, Closure) to allow property-style access. |
void |
putAt(Object contentType,
Closure value)
Register a new encoder for the given content type. |
void |
setCharset(String charset)
Set the charset used in the content-type header of all requests that send textual data. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public EncoderRegistry()
| Method Detail |
|---|
public void setCharset(String charset)
charset - Charset.forName(String)
public InputStreamEntity encodeStream(Object data,
Object contentType)
throws UnsupportedEncodingException
data -
HttpEntity encapsulating this request data
UnsupportedEncodingException
public HttpEntity encodeText(Object data,
Object contentType)
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 data
IOException
public UrlEncodedFormEntity encodeForm(Map<?,?> params)
throws UnsupportedEncodingException
[ key1 : ['val1', 'val2'], key2 : 'etc.' ]
params -
HttpEntity encapsulating this request data
UnsupportedEncodingException
public UrlEncodedFormEntity encodeForm(Map<?,?> params,
Object contentType)
throws UnsupportedEncodingException
UnsupportedEncodingException
public HttpEntity encodeForm(String formData,
Object contentType)
throws UnsupportedEncodingException
formData - a url-encoded form POST string. See
The W3C spec for more info.
HttpEntity encapsulating this request data
UnsupportedEncodingException
public HttpEntity encodeXML(Object xml,
Object contentType)
throws UnsupportedEncodingException
toString produces valid markup, or a Closure which will be
interpreted as a builder definition. A closure argument is
passed to StreamingMarkupBuilder#bind(groovy.lang.Closure).
xml - data that defines the XML structure
HttpEntity encapsulating this request data
UnsupportedEncodingException
public HttpEntity encodeJSON(Object model,
Object contentType)
throws UnsupportedEncodingException
Accepts a Collection or a JavaBean object which is converted to JSON.
A Map or POJO/POGO will be converted to a JSONObject, and any
other collection type will be converted to a JSONArray. 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 JsonGroovyBuilder. 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 data
UnsupportedEncodingException
protected StringEntity createEntity(Object 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. It also sets the
charset portion of the content-type header.
ct - content-type of the datadata - textual request data to be encoded
request content
UnsupportedEncodingExceptionprotected Map<String,Closure> buildDefaultEncoderMap()
super.buildDefaultEncoderMap() and then add or remove
from that result as well.
public Closure getAt(Object contentType)
contentType -
public void putAt(Object contentType,
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 - closure - public Closure propertyMissing(Object key)
getAt(Object) to allow property-style access.
key -
public void propertyMissing(Object key,
Closure value)
putAt(Object, Closure) to allow property-style access.
key - value - public Iterator<Map.Entry<String,Closure>> iterator()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||