Coverage Report - com.jcabi.http.RequestBody
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestBody
N/A
N/A
1.333
RequestBody$Printable
100%
13/13
100%
6/6
1.333
 
 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;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import java.nio.charset.Charset;
 34  
 import java.util.Map;
 35  
 import javax.json.JsonStructure;
 36  
 
 37  
 /**
 38  
  * Request body.
 39  
  *
 40  
  * <p>Instance of this interface is returned by {@link Request#body()},
 41  
  * and can be modified using one of the methods below. When modification
 42  
  * is done, method {@code back()} returns a modified instance of
 43  
  * {@link Request}, for example:
 44  
  *
 45  
  * <pre> new JdkRequest("http://my.example.com")
 46  
  *   .header("Content-Type", "application/x-www-form-urlencoded")
 47  
  *   .body()
 48  
  *   .formParam("name", "Jeff Lebowski")
 49  
  *   .formParam("age", "37")
 50  
  *   .formParam("employment", "none")
 51  
  *   .back() // returns a modified instance of Request
 52  
  *   .fetch()</pre>
 53  
  *
 54  
  * <p>Instances of this interface are immutable and thread-safe.
 55  
  *
 56  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 57  
  * @version $Id: 63686bd5055cd39dbf14fa5ba5d4df4bd61ba49e $
 58  
  * @since 0.8
 59  
  */
 60  
 @Immutable
 61  
 public interface RequestBody {
 62  
 
 63  
     /**
 64  
      * Get back to the request it's related to.
 65  
      * @return The request we're in
 66  
      */
 67  
     Request back();
 68  
 
 69  
     /**
 70  
      * Get text content.
 71  
      * @return Content in UTF-8
 72  
      */
 73  
     String get();
 74  
 
 75  
     /**
 76  
      * Set text content.
 77  
      * @param body Body content
 78  
      * @return New alternated body
 79  
      */
 80  
     RequestBody set(String body);
 81  
 
 82  
     /**
 83  
      * Set JSON content.
 84  
      * @param json JSON object
 85  
      * @return New alternated body
 86  
      * @since 0.11
 87  
      */
 88  
     RequestBody set(JsonStructure json);
 89  
 
 90  
     /**
 91  
      * Set byte array content.
 92  
      * @param body Body content
 93  
      * @return New alternated body
 94  
      */
 95  
     RequestBody set(byte[] body);
 96  
 
 97  
     /**
 98  
      * Add form param.
 99  
      * @param name Query param name
 100  
      * @param value Value of the query param to set
 101  
      * @return New alternated body
 102  
      */
 103  
     RequestBody formParam(String name, Object value);
 104  
 
 105  
     /**
 106  
      * Add form params.
 107  
      * @param params Map of params
 108  
      * @return New alternated body
 109  
      * @since 0.10
 110  
      */
 111  
     RequestBody formParams(Map<String, String> params);
 112  
 
 113  
     /**
 114  
      * Printer of byte array.
 115  
      */
 116  
     @Immutable
 117  
     final class Printable {
 118  
         /**
 119  
          * The Charset to use.
 120  
          */
 121  1
         private static final Charset CHARSET = Charset.forName("UTF-8");
 122  
         /**
 123  
          * Byte array.
 124  
          */
 125  
         @Immutable.Array
 126  
         private final transient byte[] array;
 127  
         /**
 128  
          * Ctor.
 129  
          * @param bytes Bytes to encapsulate
 130  
          */
 131  223
         public Printable(final byte[] bytes) {
 132  223
             this.array = bytes;
 133  223
         }
 134  
         @Override
 135  
         public String toString() {
 136  223
             final StringBuilder text = new StringBuilder(0);
 137  224
             final char[] chrs = new String(
 138  
                 this.array, RequestBody.Printable.CHARSET
 139  
             ).toCharArray();
 140  224
             if (chrs.length > 0) {
 141  4365
                 for (final char chr : chrs) {
 142  
                     // @checkstyle MagicNumber (1 line)
 143  4238
                     if (chr < 128) {
 144  4207
                         text.append(chr);
 145  
                     } else {
 146  31
                         text.append("\\u").append(
 147  
                             Integer.toHexString(chr)
 148  
                         );
 149  
                     }
 150  
                 }
 151  
             } else {
 152  97
                 text.append("<<empty>>");
 153  
             }
 154  224
             return text.toString();
 155  
         }
 156  
     }
 157  
 
 158  
 }