Package com.jcabi.http.response
Class XmlResponse
java.lang.Object
com.jcabi.http.response.XmlResponse
- All Implemented Interfaces:
Response
XML response.
This response decorator is able to parse HTTP response body as an XML document and manipulate with it afterwards, 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.
- Since:
- 0.8
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends Response>
TConvert it to another type, by encapsulation.assertXPath(String xpath) Verifies HTTP response body XHTML/XML content against XPath query, and throwsAssertionErrorin case of mismatch.final Requestback()Get back to the request it's related to.final byte[]binary()Raw body as an array of bytes.body()Get body as a string, assuming it'sUTF-8(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).headers()Get a collection of all headers.final Stringreason()Get status line reason phrase.registerNs(String prefix, String uri) Register this new namespace.Follow XML link.final intstatus()Get status of the response as a positive integer number.final StringtoString()com.jcabi.xml.XMLxml()Get XML body.
-
Constructor Details
-
XmlResponse
Public ctor.- Parameters:
resp- Response
-
-
Method Details
-
xml
public com.jcabi.xml.XML xml()Get XML body.- Returns:
- XML body
-
registerNs
Register this new namespace.- Parameters:
prefix- Prefix to useuri- Namespace URI- Returns:
- This object
-
assertXPath
Verifies HTTP response body XHTML/XML content against XPath query, and throwsAssertionErrorin case of mismatch.- Parameters:
xpath- Query to use- Returns:
- This object
-
rel
Follow XML link.- Parameters:
query- XPath query to fetch new URI- Returns:
- New request
-
toString
-
back
Description copied from interface:ResponseGet back to the request it's related to. -
status
public final int status()Description copied from interface:ResponseGet status of the response as a positive integer number. -
reason
Description copied from interface:ResponseGet status line reason phrase. -
headers
Description copied from interface:ResponseGet a collection of all headers. -
body
Description copied from interface:ResponseGet body as a string, assuming it'sUTF-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
RuntimeExceptionwill be thrown. If you need to use some other encodings, useResponse.binary()instead. -
binary
public final byte[] binary()Description copied from interface:ResponseRaw body as an array of bytes. -
as
Description copied from interface:ResponseConvert it to another type, by encapsulation.
-