Coverage Report - com.jcabi.http.mock.GrizzlyQuery
 
Classes in this File Line Coverage Branch Coverage Complexity
GrizzlyQuery
100%
36/36
90%
9/10
1.556
 
 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.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.http.ImmutableHeader;
 34  
 import com.jcabi.immutable.ArrayMap;
 35  
 import com.sun.grizzly.tcp.http11.GrizzlyRequest;
 36  
 import java.io.ByteArrayOutputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.net.URI;
 40  
 import java.nio.charset.Charset;
 41  
 import java.util.Arrays;
 42  
 import java.util.Collections;
 43  
 import java.util.Enumeration;
 44  
 import java.util.LinkedList;
 45  
 import java.util.List;
 46  
 import java.util.Map;
 47  
 import java.util.concurrent.ConcurrentHashMap;
 48  
 import java.util.concurrent.ConcurrentMap;
 49  
 
 50  
 /**
 51  
  * Mock HTTP query/request.
 52  
  *
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id: b4bcadd0c0bce359d9be6092d08b08041934dbfe $
 55  
  * @since 0.10
 56  
  */
 57  
 @Immutable
 58  
 final class GrizzlyQuery implements MkQuery {
 59  
 
 60  
     /**
 61  
      * The encoding to use.
 62  
      */
 63  
     private static final String ENCODING = "UTF-8";
 64  
 
 65  
     /**
 66  
      * The Charset to use.
 67  
      */
 68  1
     private static final Charset CHARSET = Charset.forName(ENCODING);
 69  
 
 70  
     /**
 71  
      * HTTP request method.
 72  
      */
 73  
     private final transient String mtd;
 74  
 
 75  
     /**
 76  
      * HTTP request content.
 77  
      */
 78  
     @Immutable.Array
 79  
     private final transient byte[] content;
 80  
 
 81  
     /**
 82  
      * HTTP request URI.
 83  
      */
 84  
     private final transient String home;
 85  
 
 86  
     /**
 87  
      * HTTP request headers.
 88  
      */
 89  
     private final transient ArrayMap<String, List<String>> hdrs;
 90  
 
 91  
     /**
 92  
      * Ctor.
 93  
      * @param request Grizzly request
 94  
      * @throws IOException If fails
 95  
      */
 96  110
     GrizzlyQuery(final GrizzlyRequest request) throws IOException {
 97  110
         request.setCharacterEncoding(GrizzlyQuery.ENCODING);
 98  110
         this.home = GrizzlyQuery.uri(request);
 99  110
         this.mtd = request.getMethod();
 100  108
         this.hdrs = GrizzlyQuery.headers(request);
 101  
         // @checkstyle MagicNumber (1 line)
 102  110
         final byte[] buffer = new byte[8192];
 103  112
         final InputStream input = request.getInputStream();
 104  111
         final ByteArrayOutputStream output = new ByteArrayOutputStream();
 105  111
         for (int bytes = input.read(buffer); bytes != -1;
 106  15
             bytes = input.read(buffer)) {
 107  15
             output.write(buffer, 0, bytes);
 108  
         }
 109  110
         this.content = output.toByteArray();
 110  110
     }
 111  
 
 112  
     @Override
 113  
     public URI uri() {
 114  6
         return URI.create(this.home);
 115  
     }
 116  
 
 117  
     @Override
 118  
     public String method() {
 119  3
         return this.mtd;
 120  
     }
 121  
 
 122  
     @Override
 123  
     public Map<String, List<String>> headers() {
 124  19
         return Collections.unmodifiableMap(this.hdrs);
 125  
     }
 126  
 
 127  
     @Override
 128  
     public String body() {
 129  17
         return new String(this.content, GrizzlyQuery.CHARSET);
 130  
     }
 131  
 
 132  
     @Override
 133  
     public byte[] binary() {
 134  1
         return Arrays.copyOf(this.content, this.content.length);
 135  
     }
 136  
 
 137  
     /**
 138  
      * Fetch URI from the request.
 139  
      * @param request Request
 140  
      * @return URI
 141  
      */
 142  
     private static String uri(final GrizzlyRequest request) {
 143  110
         final StringBuilder uri = new StringBuilder(request.getRequestURI());
 144  108
         final String query = request.getQueryString();
 145  108
         if (query != null && !query.isEmpty()) {
 146  4
             uri.append('?').append(query);
 147  
         }
 148  108
         return uri.toString();
 149  
     }
 150  
 
 151  
     /**
 152  
      * Fetch headers from the request.
 153  
      * @param request Request
 154  
      * @return Headers
 155  
      */
 156  
     private static ArrayMap<String, List<String>> headers(
 157  
         final GrizzlyRequest request) {
 158  111
         final ConcurrentMap<String, List<String>> headers =
 159  
             new ConcurrentHashMap<String, List<String>>(0);
 160  109
         final Enumeration<?> names = request.getHeaderNames();
 161  812
         while (names.hasMoreElements()) {
 162  703
             final String name = names.nextElement().toString();
 163  700
             headers.put(
 164  
                 ImmutableHeader.normalize(name),
 165  
                 GrizzlyQuery.headers(request, name)
 166  
             );
 167  702
         }
 168  111
         return new ArrayMap<>(headers);
 169  
     }
 170  
 
 171  
     /**
 172  
      * Get headers by name.
 173  
      * @param request Grizzly request
 174  
      * @param name Name of header
 175  
      * @return List of values
 176  
      */
 177  
     private static List<String> headers(
 178  
         final GrizzlyRequest request, final String name) {
 179  711
         final List<String> list = new LinkedList<String>();
 180  712
         final Enumeration<?> values = request.getHeaders(name);
 181  1410
         while (values.hasMoreElements()) {
 182  708
             list.add(values.nextElement().toString());
 183  
         }
 184  702
         return list;
 185  
     }
 186  
 
 187  
 }