Class EncoderRegistry

java.lang.Object
groovyx.net.http.EncoderRegistry
All Implemented Interfaces:
Iterable<Map.Entry<String,groovy.lang.Closure>>

public class EncoderRegistry extends Object implements Iterable<Map.Entry<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 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=1invalid input: '&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.

See Also:
  • Field Details

    • charset

      Charset charset
    • registeredEncoders

      private Map<String,groovy.lang.Closure> registeredEncoders
  • Constructor Details

    • EncoderRegistry

      public EncoderRegistry()
  • Method Details

    • 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:
    • encodeStream

      public org.apache.http.entity.InputStreamEntity encodeStream(Object data, Object contentType) throws UnsupportedEncodingException
      Default request encoder for a binary stream. Acceptable argument types are:
      • InputStream
      • byte[] / ByteArrayOutputStream
      • Closure
      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, Object contentType) throws IOException
      Default handler used for a plain text content-type. Acceptable argument types are:
      • Closure
      • Writable
      • Reader
      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<?,?> params) throws UnsupportedEncodingException
      Set 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 HttpEntity encapsulating this request data
      Throws:
      UnsupportedEncodingException
    • encodeForm

      public org.apache.http.client.entity.UrlEncodedFormEntity encodeForm(Map<?,?> params, Object contentType) throws UnsupportedEncodingException
      Throws:
      UnsupportedEncodingException
    • encodeForm

      public org.apache.http.HttpEntity encodeForm(String formData, Object contentType) throws UnsupportedEncodingException
      Accepts 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 HttpEntity encapsulating this request data
      Throws:
      UnsupportedEncodingException
    • encodeXML

      public org.apache.http.HttpEntity encodeXML(Object xml, Object contentType) throws UnsupportedEncodingException
      Encode the content as XML. The argument may be either an object whose 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).
      Parameters:
      xml - data that defines the XML structure
      Returns:
      an HttpEntity encapsulating this request data
      Throws:
      UnsupportedEncodingException
    • createEntity

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

      protected Map<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 call super.buildDefaultEncoderMap() and then add or remove from that result as well.
    • getAt

      public groovy.lang.Closure getAt(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(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 an 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).
      Parameters:
      contentType -
      closure -
    • propertyMissing

      public groovy.lang.Closure propertyMissing(Object key)
      Alias for getAt(Object) to allow property-style access.
      Parameters:
      key -
      Returns:
    • propertyMissing

      public void propertyMissing(Object key, groovy.lang.Closure value)
      Alias for putAt(Object, Closure) to allow property-style access.
      Parameters:
      key -
      value -
    • iterator

      public Iterator<Map.Entry<String,groovy.lang.Closure>> iterator()
      Iterate over the entire parser map
      Specified by:
      iterator in interface Iterable<Map.Entry<String,groovy.lang.Closure>>
      Returns: