Class XmlResponse

  • All Implemented Interfaces:
    Response

    @Immutable
    public final class XmlResponse
    extends Object
    XML response.

    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.

    Since:
    0.8
    • Constructor Detail

      • XmlResponse

        public XmlResponse​(Response resp)
        Public ctor.
        Parameters:
        resp - Response
    • Method Detail

      • xml

        public com.jcabi.xml.XML xml()
        Get XML body.
        Returns:
        XML body
      • registerNs

        public XmlResponse registerNs​(String prefix,
                                      String uri)
        Register this new namespace.
        Parameters:
        prefix - Prefix to use
        uri - Namespace URI
        Returns:
        This object
      • assertXPath

        public XmlResponse assertXPath​(String xpath)
        Verifies HTTP response body XHTML/XML content against XPath query, and throws AssertionError in case of mismatch.
        Parameters:
        xpath - Query to use
        Returns:
        This object
      • rel

        public Request rel​(String query)
        Follow XML link.
        Parameters:
        query - XPath query to fetch new URI
        Returns:
        New request
      • back

        public final Request back()
        Description copied from interface: Response
        Get back to the request it's related to.
        Specified by:
        back in interface Response
        Returns:
        The request we're in
      • status

        public final int status()
        Description copied from interface: Response
        Get status of the response as a positive integer number.
        Specified by:
        status in interface Response
        Returns:
        The status code
      • reason

        public final String reason()
        Description copied from interface: Response
        Get status line reason phrase.
        Specified by:
        reason in interface Response
        Returns:
        The status line reason phrase
      • body

        public String body()
        Description copied from interface: Response
        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).

        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.

        Specified by:
        body in interface Response
        Returns:
        The body, as a UTF-8 string
      • binary

        public final byte[] binary()
        Description copied from interface: Response
        Raw body as a an array of bytes.
        Specified by:
        binary in interface Response
        Returns:
        The body, as a UTF-8 string
      • as

        public final <T extends Response> T as​(Class<T> type)
        Description copied from interface: Response
        Convert it to another type, by encapsulation.
        Specified by:
        as in interface Response
        Type Parameters:
        T - Type to use
        Parameters:
        type - Type to use
        Returns:
        New response