Coverage Report - com.jcabi.http.response.JsoupResponse
 
Classes in this File Line Coverage Branch Coverage Complexity
JsoupResponse
85%
6/7
0%
0/8
1
 
 1  
 /**
 2  
  * Copyright (c) 2011-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.http.response;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.http.Response;
 34  
 import lombok.EqualsAndHashCode;
 35  
 import org.jsoup.Jsoup;
 36  
 import org.jsoup.nodes.Document;
 37  
 import org.jsoup.nodes.Entities;
 38  
 
 39  
 /**
 40  
  * Jsoup response.
 41  
  *
 42  
  * <p>This response decorator is able to parse HTTP response body as an HTML
 43  
  * document. Example usage:
 44  
  *
 45  
  * <pre> String body = new JdkRequest("http://my.example.com")
 46  
  *   .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
 47  
  *   .fetch()
 48  
  *   .as(JsoupResponse.class)
 49  
  *   .body();</pre>
 50  
  *
 51  
  * <p>{@link #body()} will try to output clean HTML even for
 52  
  *  malformed responses. For example:
 53  
  * <ul>
 54  
  *  <li>Unclosed tags will be closed ("&lt;p&gt;Hello" will become
 55  
  *      "&lt;p&gt;Hello&lt;/p&gt;")
 56  
  *  <li>Implicit tags will be made explicit (e.g. a naked &lt;td&gt; will be
 57  
  *      wrapped in a &lt;table&gt;&lt;tr&gt;&lt;td&gt;)
 58  
  *  <li>Basic structure is guaranteed (i.e. html, head, body elements)
 59  
  * </ul>
 60  
  *
 61  
  * <p>The class is immutable and thread-safe.
 62  
  *
 63  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 64  
  * @version $Id: 0ff5096af7e9130353e98b195ffdb750b944468d $
 65  
  * @since 1.4
 66  
  * @see <a href="http://jsoup.org/">Jsoup website</a>
 67  
  */
 68  
 @Immutable
 69  0
 @EqualsAndHashCode(callSuper = true)
 70  
 public final class JsoupResponse extends AbstractResponse {
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param resp Response
 75  
      */
 76  
     public JsoupResponse(final Response resp) {
 77  1
         super(resp);
 78  1
     }
 79  
 
 80  
     @Override
 81  
     public String body() {
 82  1
         final Document html = Jsoup.parse(super.body());
 83  1
         html.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
 84  1
         html.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
 85  1
         return html.html();
 86  
     }
 87  
 
 88  
 }