|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovyx.net.http.ParserRegistry
public class ParserRegistry
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.
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 |
---|
protected Closure defaultParser
protected final org.apache.commons.logging.Log log
protected Map<String,Closure> registeredParsers
Constructor Detail |
---|
public ParserRegistry()
Method Detail |
---|
public static String getCharset(org.apache.http.HttpResponse resp)
Reader reader = new InputStreamReader( resp.getEntity().getContent(), ParserRegistry.getCharset( resp ) );
resp
- public static String getContentType(org.apache.http.HttpResponse resp)
resp
- public InputStream parseStream(org.apache.http.HttpResponse resp) throws IOException
resp
-
IllegalStateException
IOException
ContentType.BINARY
public Reader parseText(org.apache.http.HttpResponse resp) throws IOException
resp
-
UnsupportedEncodingException
IllegalStateException
IOException
ContentType.TEXT
public Map<String,String> parseForm(org.apache.http.HttpResponse resp) throws IOException
resp
-
IOException
ContentType.URLENC
public GPathResult parseHTML(org.apache.http.HttpResponse resp) throws IOException, SAXException
resp
- HTTP response from which to parse content
GPathResult
from calling XmlSlurper.parse(Reader)
IOException
SAXException
ContentType.HTML
,
SAXParser
,
XmlSlurper.parse(Reader)
public GPathResult parseXML(org.apache.http.HttpResponse resp) throws IOException, SAXException, ParserConfigurationException
resp
- HTTP response from which to parse content
GPathResult
from calling XmlSlurper.parse(Reader)
IOException
SAXException
ParserConfigurationException
ContentType.XML
,
XmlSlurper.parse(Reader)
public JSON parseJSON(org.apache.http.HttpResponse resp) throws IOException
resp
-
IOException
ContentType.JSON
public void register(String contentType, Closure closure)
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.
contentType
- content-type
stringclosure
- code that will parse the HttpResponse and return parsed
data to the response handler.protected Map<String,Closure> buildDefaultParserMap()
super.buildDefaultParserMap()
and then add or remove
from that result as well.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |