Class EncoderRegistry
- java.lang.Object
-
- groovyx.net.http.EncoderRegistry
-
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<java.lang.String,groovy.lang.Closure>>
public class EncoderRegistry extends java.lang.Object implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,groovy.lang.Closure>>This class handles creation of the request body (i.e. for a PUT or POST operation) based on content-type. When a
bodyis set from the builder, it is processed based on therequest content-type. For instance, theencodeForm(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 equivalenta=1&b=twoin 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 eachencode...method for details for each particular content-type.Contrary to its name, this class does not have anything to do with the
content-encodingHTTP header.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.nio.charset.Charsetcharsetprivate java.util.Map<java.lang.String,groovy.lang.Closure>registeredEncoders
-
Constructor Summary
Constructors Constructor Description EncoderRegistry()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Map<java.lang.String,groovy.lang.Closure>buildDefaultEncoderMap()Returns a map of default encoders.protected org.apache.http.entity.StringEntitycreateEntity(java.lang.Object ct, java.lang.String data)Helper method used by encoder methods to create anHttpEntityinstance that encapsulates the request data.org.apache.http.HttpEntityencodeForm(java.lang.String formData, java.lang.Object contentType)Accepts a String as a url-encoded form post.org.apache.http.client.entity.UrlEncodedFormEntityencodeForm(java.util.Map<?,?> params)Set the request body as a url-encoded list of parameters.org.apache.http.client.entity.UrlEncodedFormEntityencodeForm(java.util.Map<?,?> params, java.lang.Object contentType)org.apache.http.entity.InputStreamEntityencodeStream(java.lang.Object data, java.lang.Object contentType)Default request encoder for a binary stream.org.apache.http.HttpEntityencodeText(java.lang.Object data, java.lang.Object contentType)Default handler used for a plain text content-type.org.apache.http.HttpEntityencodeXML(java.lang.Object xml, java.lang.Object contentType)Encode the content as XML.groovy.lang.ClosuregetAt(java.lang.Object contentType)Retrieve a encoder for the given content-type.java.util.Iterator<java.util.Map.Entry<java.lang.String,groovy.lang.Closure>>iterator()Iterate over the entire parser mapgroovy.lang.ClosurepropertyMissing(java.lang.Object key)Alias forgetAt(Object)to allow property-style access.voidpropertyMissing(java.lang.Object key, groovy.lang.Closure value)Alias forputAt(Object, Closure)to allow property-style access.voidputAt(java.lang.Object contentType, groovy.lang.Closure value)Register a new encoder for the given content type.voidsetCharset(java.lang.String charset)Set the charset used in the content-type header of all requests that send textual data.
-
-
-
Method Detail
-
setCharset
public void setCharset(java.lang.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(java.lang.Object data, java.lang.Object contentType) throws java.io.UnsupportedEncodingExceptionDefault request encoder for a binary stream. Acceptable argument types are:- InputStream
- byte[] / ByteArrayOutputStream
- Closure
- Parameters:
data-- Returns:
- an
HttpEntityencapsulating this request data - Throws:
java.io.UnsupportedEncodingException
-
encodeText
public org.apache.http.HttpEntity encodeText(java.lang.Object data, java.lang.Object contentType) throws java.io.IOExceptionDefault handler used for a plain text content-type. Acceptable argument types are:- Closure
- Writable
- Reader
PrintWriteris 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
HttpEntityencapsulating this request data - Throws:
java.io.IOException
-
encodeForm
public org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(java.util.Map<?,?> params) throws java.io.UnsupportedEncodingExceptionSet the request body as a url-encoded list of parameters. This is typically used to simulate a HTTP form POST. For multi-valued parameters, enclose the values in a list, e.g.[ key1 : ['val1', 'val2'], key2 : 'etc.' ]
- Parameters:
params-- Returns:
- an
HttpEntityencapsulating this request data - Throws:
java.io.UnsupportedEncodingException
-
encodeForm
public org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(java.util.Map<?,?> params, java.lang.Object contentType) throws java.io.UnsupportedEncodingException- Throws:
java.io.UnsupportedEncodingException
-
encodeForm
public org.apache.http.HttpEntity encodeForm(java.lang.String formData, java.lang.Object contentType) throws java.io.UnsupportedEncodingExceptionAccepts a String as a url-encoded form post. This method assumes the String is an already-encoded POST string.- Parameters:
formData- a url-encoded form POST string. See The W3C spec for more info.- Returns:
- an
HttpEntityencapsulating this request data - Throws:
java.io.UnsupportedEncodingException
-
encodeXML
public org.apache.http.HttpEntity encodeXML(java.lang.Object xml, java.lang.Object contentType) throws java.io.UnsupportedEncodingExceptionEncode the content as XML. The argument may be either an object whosetoStringproduces valid markup, or a Closure which will be interpreted as a builder definition. A closure argument is passed toStreamingMarkupBuilder#bind(groovy.lang.Closure).- Parameters:
xml- data that defines the XML structure- Returns:
- an
HttpEntityencapsulating this request data - Throws:
java.io.UnsupportedEncodingException
-
createEntity
protected org.apache.http.entity.StringEntity createEntity(java.lang.Object ct, java.lang.String data) throws java.io.UnsupportedEncodingExceptionHelper method used by encoder methods to create anHttpEntityinstance that encapsulates the request data. This may be used by any non-streaming encoder that needs to send textual data. It also sets thecharsetportion of the content-type header.- Parameters:
ct- content-type of the datadata- textual request data to be encoded- Returns:
- an instance to be used for the
request content - Throws:
java.io.UnsupportedEncodingException
-
buildDefaultEncoderMap
protected java.util.Map<java.lang.String,groovy.lang.Closure> buildDefaultEncoderMap()
Returns a map of default encoders. Override this method to change what encoders are registered by default. You can of course callsuper.buildDefaultEncoderMap()and then add or remove from that result as well.
-
getAt
public groovy.lang.Closure getAt(java.lang.Object contentType)
Retrieve a encoder for the given content-type. This is called by HTTPBuilder to retrieve the correct encoder for a given content-type. The encoder is then used to serialize the request data in the request body.- Parameters:
contentType-- Returns:
- encoder that can interpret the given content type, or null.
-
putAt
public void putAt(java.lang.Object contentType, groovy.lang.Closure value)Register a new encoder for the given content type. If any encoder previously existed for that content type it will be replaced. The closure must return anHttpEntity. It will also usually accept a single argument, which will be whatever is set in the request configuration closure viaHTTPBuilder.RequestConfigDelegate.setBody(Object).- Parameters:
contentType-closure-
-
propertyMissing
public groovy.lang.Closure propertyMissing(java.lang.Object key)
Alias forgetAt(Object)to allow property-style access.- Parameters:
key-- Returns:
-
propertyMissing
public void propertyMissing(java.lang.Object key, groovy.lang.Closure value)Alias forputAt(Object, Closure)to allow property-style access.- Parameters:
key-value-
-
iterator
public java.util.Iterator<java.util.Map.Entry<java.lang.String,groovy.lang.Closure>> iterator()
Iterate over the entire parser map- Specified by:
iteratorin interfacejava.lang.Iterable<java.util.Map.Entry<java.lang.String,groovy.lang.Closure>>- Returns:
-
-