@Immutable public final class XmlResponse extends Object
This response decorator is able to parse HTTP response body as an XML document and manipulate with it afterwords, for example:
String name = new JdkRequest("http://my.example.com") .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML) .fetch() .as(XmlResponse.class) .assertXPath("/user/name") .xml() .xpath("/user/name/text()") .get(0);
In HATEOAS
responses it is convenient to use this decorator's
method rel(String)
in order to follow the link provided in <link>
XML element,
for example:
String data = new JdkRequest("http://my.example.com") .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML) .fetch() .as(XmlResponse.class) .rel("/user/links/link[@rel='next']/@href") .fetch() .body();
The class is immutable and thread-safe.
Constructor and Description |
---|
XmlResponse(Response resp)
Public ctor.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
as(Class<T> type)
Convert it to another type, by encapsulation.
|
XmlResponse |
assertXPath(String xpath)
Verifies HTTP response body XHTML/XML content against XPath query,
and throws
AssertionError in case of mismatch. |
Request |
back()
Get back to the request it's related to.
|
byte[] |
binary()
Raw body as a an array of bytes.
|
String |
body()
Get body as a string, assuming it's
UTF-8 (if there is something
else that can't be translated into a UTF-8 string a runtime exception
will be thrown). |
Map<String,List<String>> |
headers()
Get a collection of all headers.
|
String |
reason()
Get status line reason phrase.
|
XmlResponse |
registerNs(String prefix,
String uri)
Register this new namespace.
|
Request |
rel(String query)
Follow XML link.
|
int |
status()
Get status of the response as a positive integer number.
|
String |
toString() |
com.jcabi.xml.XML |
xml()
Get XML body.
|
public XmlResponse(@NotNull(message="response can\'t be NULL") Response resp)
resp
- Response@NotNull(message="XML is never NULL") public com.jcabi.xml.XML xml()
@NotNull(message="response is never NULL") public XmlResponse registerNs(@NotNull(message="prefix can\'t be NULL") String prefix, @NotNull(message="URI can\'t be NULL") String uri)
prefix
- Prefix to useuri
- Namespace URI@NotNull(message="response is never NULL") public XmlResponse assertXPath(@NotNull(message="xpath can\'t be NULL") String xpath)
AssertionError
in case of mismatch.xpath
- Query to use@NotNull(message="request is never NULL") public Request rel(@NotNull(message="query can\'t be NULL") String query)
query
- XPath query to fetch new URIpublic final Request back()
Response
public final int status()
Response
public final String reason()
Response
public final Map<String,List<String>> headers()
Response
public final String body()
Response
UTF-8
(if there is something
else that can't be translated into a UTF-8 string a runtime exception
will be thrown).
DISCLAIMER:
The only encoding supported here is UTF-8. If the body of response
contains any chars that can't be used and should be replaced with
a "replacement character", a RuntimeException
will be thrown. If
you need to use some other encodings, use
Response.binary()
instead.
public final byte[] binary()
Response
Copyright © 2012–2014 jcabi.com. All rights reserved.