groovyx.net.http
Class ParserRegistry

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

public class ParserRegistry
extends Object

Keeps track of response parsers for each content type. Each parser should should be a closure that accepts an HttpResponse instance, and returns whatever handler is appropriate for reading the response data for that content-type. For example, a plain-text response should probably be parsed with a Reader, while an XML response might be parsed by an XmlSlurper, which would then be passed to the response closure.

Note that all methods in this class assume HttpResponse.getEntity() return a non-null value. It is the job of the HTTPBuilder instance to ensure a NullPointerException is not thrown by passing a response that contains no entity.

See Also:
ContentType

Field Summary
protected  Closure defaultParser
           
protected  org.apache.commons.logging.Log log
           
protected  Map<String,Closure> registeredParsers
           
 
Constructor Summary
ParserRegistry()
           
 
Method Summary
protected  Map<String,Closure> buildDefaultParserMap()
          Returns a map of default parsers.
static String getCharset(org.apache.http.HttpResponse resp)
          Helper method to get the charset from the response.
static String getContentType(org.apache.http.HttpResponse resp)
          Helper method to get the content-type string from the response (no charset).
 Map<String,String> parseForm(org.apache.http.HttpResponse resp)
          Default parser used to decode a URL-encoded response.
 GPathResult parseHTML(org.apache.http.HttpResponse resp)
          Parse an HTML document by passing it through the NekoHTML parser.
 JSON parseJSON(org.apache.http.HttpResponse resp)
          Default parser used to decode a JSON response.
 InputStream parseStream(org.apache.http.HttpResponse resp)
          Default parser used for binary data.
 Reader parseText(org.apache.http.HttpResponse resp)
          Default parser used to handle plain text data.
 GPathResult parseXML(org.apache.http.HttpResponse resp)
          Default parser used to decode an XML response.
 void register(String contentType, Closure closure)
          Register a new parser for the given content-type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultParser

protected Closure defaultParser

log

protected final org.apache.commons.logging.Log log

registeredParsers

protected Map<String,Closure> registeredParsers
Constructor Detail

ParserRegistry

public ParserRegistry()
Method Detail

getCharset

public static String getCharset(org.apache.http.HttpResponse resp)
Helper method to get the charset from the response. This should be done when manually parsing any text response to ensure it is decoded using the correct charset. For instance:
 Reader reader = new InputStreamReader( resp.getEntity().getContent(), 
   ParserRegistry.getCharset( resp ) );

Parameters:
resp -

getContentType

public static String getContentType(org.apache.http.HttpResponse resp)
Helper method to get the content-type string from the response (no charset).

Parameters:
resp -

parseStream

public InputStream parseStream(org.apache.http.HttpResponse resp)
                        throws IOException
Default parser used for binary data.

Parameters:
resp -
Returns:
an InputStream
Throws:
IllegalStateException
IOException
See Also:
ContentType.BINARY

parseText

public Reader parseText(org.apache.http.HttpResponse resp)
                 throws IOException
Default parser used to handle plain text data. The response text is decoded using the charset passed in the response content-type header.

Parameters:
resp -
Returns:
Throws:
UnsupportedEncodingException
IllegalStateException
IOException
See Also:
ContentType.TEXT

parseForm

public Map<String,String> parseForm(org.apache.http.HttpResponse resp)
                             throws IOException
Default parser used to decode a URL-encoded response.

Parameters:
resp -
Returns:
Throws:
IOException
See Also:
ContentType.URLENC

parseHTML

public GPathResult parseHTML(org.apache.http.HttpResponse resp)
                      throws IOException,
                             SAXException
Parse an HTML document by passing it through the NekoHTML parser.

Parameters:
resp - HTTP response from which to parse content
Returns:
the GPathResult from calling XmlSlurper.parse(Reader)
Throws:
IOException
SAXException
See Also:
ContentType.HTML, SAXParser, XmlSlurper.parse(Reader)

parseXML

public GPathResult parseXML(org.apache.http.HttpResponse resp)
                     throws IOException,
                            SAXException,
                            ParserConfigurationException
Default parser used to decode an XML response.

Parameters:
resp - HTTP response from which to parse content
Returns:
the GPathResult from calling XmlSlurper.parse(Reader)
Throws:
IOException
SAXException
ParserConfigurationException
See Also:
ContentType.XML, XmlSlurper.parse(Reader)

parseJSON

public JSON parseJSON(org.apache.http.HttpResponse resp)
               throws IOException
Default parser used to decode a JSON response.

Parameters:
resp -
Returns:
Throws:
IOException
See Also:
ContentType.JSON

register

public void register(String contentType,
                     Closure closure)
Register a new parser for the given content-type. The parser closure should accept an HttpResponse argument and return a type suitable to be passed to a response handler. The value returned from the parser closure is always the second parameter of the response handler closure.

Parameters:
contentType - content-type string
closure - code that will parse the HttpResponse and return parsed data to the response handler.

buildDefaultParserMap

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



Copyright © 2008-2009. All Rights Reserved.