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.

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.
protected  String getCharset(HttpResponse resp)
          Get the charset from the response
protected  String getContentType(HttpResponse resp)
          Get the content-type string from the response (no charset)
 Map<String,String> parseForm(HttpResponse resp)
          Default parser used to decode a URL-encoded response.
 GPathResult parseHTML(HttpResponse resp)
          Parse an HTML document by passing it through the NekoHTML parser.
 JSON parseJSON(HttpResponse resp)
          Default parser used to decode a JSON response.
 InputStream parseStream(HttpResponse resp)
          Default parser used for binary data.
 Reader parseText(HttpResponse resp)
          Default parser used to handle plain text data.
 GPathResult parseXML(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

protected String getCharset(HttpResponse resp)
Get the charset from the response

Parameters:
resp -

getContentType

protected String getContentType(HttpResponse resp)
Get the content-type string from the response (no charset)

Parameters:
resp -

parseStream

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

Parameters:
resp -
Returns:
an InputStream
Throws:
IllegalStateException
IOException

parseText

public Reader parseText(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

parseForm

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

Parameters:
resp -
Returns:
Throws:
IOException

parseHTML

public GPathResult parseHTML(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:
SAXParser, XmlSlurper.parse(Reader)

parseXML

public GPathResult parseXML(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:
XmlSlurper.parse(Reader)

parseJSON

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

Parameters:
resp -
Returns:
Throws:
IOException

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. All Rights Reserved.