groovyx.net.http
Class EncoderRegistry

java.lang.Object
  extended by groovyx.net.http.EncoderRegistry

public class EncoderRegistry
extends Object

This factory (or registry) handles request body "encoding." This is not to be confused with HTTP content-encoding header. When a body is set from the builder, it is processed based on the request content-type. For instance, if the body is set to a map and the request content-type is JSON, the map will be transformed to a JSON Object.

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.


Field Summary
protected  Map<String,Closure> registeredEncoders
           
 
Constructor Summary
EncoderRegistry()
           
 
Method Summary
protected  Map<String,Closure> buildDefaultEncoderMap()
          Returns a map of default encoders.
protected  org.apache.http.entity.StringEntity createEntity(ContentType ct, String data)
          Helper method used by encoder methods to creates an HttpEntity instance that encapsulates the request data.
 org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(Map<String,Object> params)
          Set the request body as a url-encoded list of parameters.
 org.apache.http.HttpEntity encodeJSON(Object model)
          Accepts a Map or a JavaBean object which is converted to JSON.
 org.apache.http.entity.InputStreamEntity encodeStream(Object data)
          Default request encoder for a binary stream.
 org.apache.http.HttpEntity encodeText(Object data)
          Default handler used for a plain text content-type.
 org.apache.http.HttpEntity encodeXML(Closure xmlBuilder)
          Executes the given closure and passes a bound StreamingMarkupBuilder.
 void register(String contentType, Closure closure)
          Used to set an additional 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
 

Field Detail

registeredEncoders

protected Map<String,Closure> registeredEncoders
Constructor Detail

EncoderRegistry

public EncoderRegistry()
Method Detail

setCharset

public void setCharset(String charset)
Set the charset used in the content-type header of all requests that send textual data. This must be a chaset supported by the Java platform

Parameters:
charset -
See Also:
Charset.forName(String)

encodeStream

public org.apache.http.entity.InputStreamEntity encodeStream(Object data)
                                                      throws UnsupportedEncodingException
Default request encoder for a binary stream. Acceptable argument types are: If a closure is given, it is executed with an OutputStream passed as the single closure argument. Any data sent to the stream from the body of the closure is used as the request content body.

Parameters:
data -
Returns:
an HttpEntity encapsulating this request data
Throws:
UnsupportedEncodingException

encodeText

public org.apache.http.HttpEntity encodeText(Object data)
                                      throws IOException
Default handler used for a plain text content-type. Acceptable argument types are: For Closure argument, a 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.

Parameters:
data -
Returns:
an HttpEntity encapsulating this request data
Throws:
IOException

encodeForm

public org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(Map<String,Object> params)
                                                              throws UnsupportedEncodingException
Set the request body as a url-encoded list of parameters. This is typically used to simulate a HTTP form POST.

Parameters:
params -
Returns:
an HttpEntity encapsulating this request data
Throws:
UnsupportedEncodingException

encodeXML

public org.apache.http.HttpEntity encodeXML(Closure xmlBuilder)
                                     throws UnsupportedEncodingException
Executes the given closure and passes a bound StreamingMarkupBuilder.

Parameters:
xmlBuilder -
Returns:
an HttpEntity encapsulating this request data
Throws:
UnsupportedEncodingException

encodeJSON

public org.apache.http.HttpEntity encodeJSON(Object model)
                                      throws UnsupportedEncodingException
Accepts a Map or a JavaBean object which is converted to JSON. If a Closure is passed, it will be executed with a JsonGroovyBuilder as the closure's delegate. The closure must return the result of the outermost builder method call.

Parameters:
model - data to be converted to JSON, as specified above.
Returns:
an HttpEntity encapsulating this request data
Throws:
UnsupportedEncodingException

createEntity

protected org.apache.http.entity.StringEntity createEntity(ContentType ct,
                                                           String data)
                                                    throws UnsupportedEncodingException
Helper method used by encoder methods to creates an 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.

Parameters:
ct - content-type of the data
data - textual request data to be encoded
Returns:
an instance to be used for the request content
Throws:
UnsupportedEncodingException

register

public void register(String contentType,
                     Closure closure)
Used to set an additional encoder for the given content type. The Closure must return an HttpEntity. It will also usually accept a single argument, which will be the value given in HTTPBuilder.SendDelegate.setBody(Object).

Parameters:
contentType -
closure -

buildDefaultEncoderMap

protected Map<String,Closure> buildDefaultEncoderMap()
Returns a map of default encoders. Override this method to change what encoders are registered by default. You can of course call super.buildDefaultEncoderMap() and then add or remove from that result as well.



Copyright © 2008-2009. All Rights Reserved.