Coverage Report - com.jcabi.http.request.DefaultResponse
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultResponse
90%
28/31
15%
6/38
1.778
 
 1  
 /**
 2  
  * Copyright (c) 2011-2015, 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.request;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.RequestBody;
 36  
 import com.jcabi.http.Response;
 37  
 import com.jcabi.immutable.Array;
 38  
 import com.jcabi.log.Logger;
 39  
 import java.lang.reflect.InvocationTargetException;
 40  
 import java.nio.charset.Charset;
 41  
 import java.util.LinkedList;
 42  
 import java.util.List;
 43  
 import java.util.Map;
 44  
 import java.util.concurrent.ConcurrentHashMap;
 45  
 import java.util.concurrent.ConcurrentMap;
 46  
 import lombok.EqualsAndHashCode;
 47  
 
 48  
 /**
 49  
  * Default implementation of {@link com.jcabi.http.Response}.
 50  
  *
 51  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 52  
  * @version $Id: 9f832e33d79528fedf34ac56d0e2f99aa6d2242e $
 53  
  */
 54  
 @Immutable
 55  0
 @EqualsAndHashCode(of = { "req", "code", "phrase", "hdrs", "content" })
 56  
 @Loggable(Loggable.DEBUG)
 57  
 public final class DefaultResponse implements Response {
 58  
 
 59  
     /**
 60  
      * The Charset to use.
 61  
      */
 62  1
     private static final Charset CHARSET = Charset.forName("UTF-8");
 63  
 
 64  
     /**
 65  
      * UTF-8 error marker.
 66  
      */
 67  
     private static final String ERR = "\uFFFD";
 68  
 
 69  
     /**
 70  
      * Request.
 71  
      */
 72  
     private final transient Request req;
 73  
 
 74  
     /**
 75  
      * Status code.
 76  
      */
 77  
     private final transient int code;
 78  
 
 79  
     /**
 80  
      * Reason phrase.
 81  
      */
 82  
     private final transient String phrase;
 83  
 
 84  
     /**
 85  
      * Headers.
 86  
      */
 87  
     private final transient Array<Map.Entry<String, String>> hdrs;
 88  
 
 89  
     /**
 90  
      * Content received.
 91  
      */
 92  
     @Immutable.Array
 93  
     private final transient byte[] content;
 94  
 
 95  
     /**
 96  
      * Public ctor.
 97  
      * @param request The request
 98  
      * @param status HTTP status
 99  
      * @param reason HTTP reason phrase
 100  
      * @param headers HTTP headers
 101  
      * @param body Body of HTTP response
 102  
      * @checkstyle ParameterNumber (5 lines)
 103  
      */
 104  
     public DefaultResponse(final Request request, final int status,
 105  
         final String reason, final Array<Map.Entry<String, String>> headers,
 106  134
         final byte[] body) {
 107  134
         this.req = request;
 108  134
         this.code = status;
 109  134
         this.phrase = reason;
 110  134
         this.hdrs = headers;
 111  134
         this.content = body.clone();
 112  134
     }
 113  
 
 114  
     @Override
 115  
     public Request back() {
 116  198
         return this.req;
 117  
     }
 118  
 
 119  
     @Override
 120  
     public int status() {
 121  274
         return this.code;
 122  
     }
 123  
 
 124  
     @Override
 125  
     public String reason() {
 126  140
         return this.phrase;
 127  
     }
 128  
 
 129  
     @Override
 130  
     @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 131  
     public Map<String, List<String>> headers() {
 132  52
         final ConcurrentMap<String, List<String>> map =
 133  
             new ConcurrentHashMap<>(0);
 134  52
         for (final Map.Entry<String, String> header : this.hdrs) {
 135  213
             map.putIfAbsent(header.getKey(), new LinkedList<String>());
 136  213
             map.get(header.getKey()).add(header.getValue());
 137  213
         }
 138  52
         return map;
 139  
     }
 140  
 
 141  
     @Override
 142  
     public String body() {
 143  65
         final String body = new String(this.content, DefaultResponse.CHARSET);
 144  65
         if (body.contains(DefaultResponse.ERR)) {
 145  1
             throw new IllegalStateException(
 146  
                 Logger.format(
 147  
                     "broken Unicode text at line #%d in '%[text]s' (%d bytes)",
 148  
                     body.length() - body.replace("\n", "").length(),
 149  
                     body,
 150  
                     this.content.length
 151  
                 )
 152  
             );
 153  
         }
 154  64
         return body;
 155  
     }
 156  
 
 157  
     @Override
 158  
     public byte[] binary() {
 159  7
         return this.content.clone();
 160  
     }
 161  
     // @checkstyle MethodName (4 lines)
 162  
     @Override
 163  
     @SuppressWarnings("PMD.ShortMethodName")
 164  
     public <T extends Response> T as(final Class<T> type) {
 165  
         try {
 166  118
             return type.getDeclaredConstructor(Response.class)
 167  
                 .newInstance(this);
 168  0
         } catch (final InstantiationException
 169  
             | IllegalAccessException | NoSuchMethodException
 170  
             | InvocationTargetException ex) {
 171  0
             throw new IllegalStateException(ex);
 172  
         }
 173  
     }
 174  
 
 175  
     @Override
 176  
     @SuppressWarnings("PMD.ConsecutiveLiteralAppends")
 177  
     public String toString() {
 178  186
         final StringBuilder text = new StringBuilder(0)
 179  
             .append(this.code).append(' ')
 180  
             .append(this.phrase)
 181  
             .append(" [")
 182  
             .append(this.back().uri().get())
 183  
             .append("]\n");
 184  184
         for (final Map.Entry<String, String> header : this.hdrs) {
 185  759
             text.append(
 186  
                 Logger.format(
 187  
                     "%s: %s\n",
 188  
                     header.getKey(),
 189  
                     header.getValue()
 190  
                 )
 191  
             );
 192  759
         }
 193  185
         return text.append('\n')
 194  
             .append(new RequestBody.Printable(this.content))
 195  
             .toString();
 196  
     }
 197  
 
 198  
 }