Coverage Report - com.jcabi.http.request.ApacheRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
ApacheRequest
50%
10/20
0%
0/12
1.222
ApacheRequest$1
95%
20/21
83%
5/6
1.222
ApacheRequest$1$1
100%
2/2
N/A
1.222
 
 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.request;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.ImmutableHeader;
 35  
 import com.jcabi.http.Request;
 36  
 import com.jcabi.http.RequestBody;
 37  
 import com.jcabi.http.RequestURI;
 38  
 import com.jcabi.http.Response;
 39  
 import com.jcabi.http.Wire;
 40  
 import com.jcabi.immutable.Array;
 41  
 import java.io.IOException;
 42  
 import java.io.InputStream;
 43  
 import java.net.URI;
 44  
 import java.net.URL;
 45  
 import java.util.Collection;
 46  
 import java.util.LinkedList;
 47  
 import java.util.Map;
 48  
 import lombok.EqualsAndHashCode;
 49  
 import lombok.ToString;
 50  
 import org.apache.http.Header;
 51  
 import org.apache.http.HttpEntity;
 52  
 import org.apache.http.client.config.RequestConfig;
 53  
 import org.apache.http.client.methods.CloseableHttpResponse;
 54  
 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 55  
 import org.apache.http.entity.BufferedHttpEntity;
 56  
 import org.apache.http.entity.InputStreamEntity;
 57  
 import org.apache.http.impl.client.HttpClients;
 58  
 import org.apache.http.util.EntityUtils;
 59  
 
 60  
 /**
 61  
  * Implementation of {@link Request},
 62  
  * based on Apache HTTP client.
 63  
  *
 64  
  * <p>The class is immutable and thread-safe.
 65  
  *
 66  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 67  
  * @version $Id: 936dcda49313747de85ec154cd03e5a3db247eea $
 68  
  * @since 0.8
 69  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 70  
  */
 71  
 @Immutable
 72  0
 @EqualsAndHashCode(of = "base")
 73  0
 @ToString(of = "base")
 74  
 @Loggable(Loggable.DEBUG)
 75  
 @SuppressWarnings("PMD.TooManyMethods")
 76  
 public final class ApacheRequest implements Request {
 77  
 
 78  
     /**
 79  
      * The wire to use.
 80  
      * @checkstyle AnonInnerLength (200 lines)
 81  
      */
 82  1
     private static final Wire WIRE = new Wire() {
 83  
         // @checkstyle ParameterNumber (6 lines)
 84  
         @Override
 85  
         public Response send(final Request req, final String home,
 86  
             final String method,
 87  
             final Collection<Map.Entry<String, String>> headers,
 88  
             final InputStream content,
 89  
             final int connect,
 90  
             final int read) throws IOException {
 91  18
             final CloseableHttpResponse response =
 92  
                 HttpClients.createSystem().execute(
 93  
                     this.httpRequest(
 94  
                         home, method, headers, content,
 95  
                         connect, read
 96  
                     )
 97  
                 );
 98  
             try {
 99  16
                 return new DefaultResponse(
 100  
                     req,
 101  
                     response.getStatusLine().getStatusCode(),
 102  
                     response.getStatusLine().getReasonPhrase(),
 103  
                     this.headers(response.getAllHeaders()),
 104  
                     this.consume(response.getEntity())
 105  
                 );
 106  
             } finally {
 107  16
                 response.close();
 108  
             }
 109  
         }
 110  
         /**
 111  
          * Create request.
 112  
          * @param home Home URI
 113  
          * @param method Method to use
 114  
          * @param headers HTTP Headers to use
 115  
          * @param content Content to send
 116  
          * @param connect Connect timeout
 117  
          * @param read Read timeout
 118  
          * @return Request
 119  
          * @throws IOException If an IO Exception occurs
 120  
          * @checkstyle ParameterNumber (6 lines)
 121  
          */
 122  
         public HttpEntityEnclosingRequestBase httpRequest(final String home,
 123  
             final String method,
 124  
             final Collection<Map.Entry<String, String>> headers,
 125  
             final InputStream content,
 126  
             final int connect,
 127  
             final int read) throws IOException {
 128  18
             final HttpEntityEnclosingRequestBase req =
 129  18
                 new HttpEntityEnclosingRequestBase() {
 130  
                     @Override
 131  
                     public String getMethod() {
 132  36
                         return method;
 133  
                     }
 134  
                 };
 135  17
             final URI uri = URI.create(home);
 136  18
             req.setConfig(
 137  
                 RequestConfig.custom()
 138  
                     .setCircularRedirectsAllowed(false)
 139  
                     .setRedirectsEnabled(false)
 140  
                     .setConnectTimeout(connect)
 141  
                     .setSocketTimeout(read)
 142  
                     .build()
 143  
             );
 144  18
             req.setURI(uri);
 145  18
             req.setEntity(
 146  
                 new BufferedHttpEntity(new InputStreamEntity(content))
 147  
             );
 148  18
             for (final Map.Entry<String, String> header : headers) {
 149  11
                 req.addHeader(header.getKey(), header.getValue());
 150  11
             }
 151  18
             return req;
 152  
         }
 153  
         /**
 154  
          * Fetch body from http entity.
 155  
          * @param entity HTTP entity
 156  
          * @return Body in UTF-8
 157  
          * @throws IOException If fails
 158  
          */
 159  
         private byte[] consume(final HttpEntity entity) throws IOException {
 160  
             final byte[] body;
 161  16
             if (entity == null) {
 162  0
                 body = new byte[0];
 163  
             } else {
 164  16
                 body = EntityUtils.toByteArray(entity);
 165  
             }
 166  16
             return body;
 167  
         }
 168  
         /**
 169  
          * Make a list of all hdrs.
 170  
          * @param list Apache HTTP hdrs
 171  
          * @return Body in UTF-8
 172  
          */
 173  
         @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 174  
         private Array<Map.Entry<String, String>> headers(final Header... list) {
 175  17
             final Collection<Map.Entry<String, String>> headers =
 176  
                 new LinkedList<>();
 177  85
             for (final Header header : list) {
 178  70
                 headers.add(
 179  
                     new ImmutableHeader(
 180  
                         header.getName(),
 181  
                         header.getValue()
 182  
                     )
 183  
                 );
 184  
             }
 185  17
             return new Array<Map.Entry<String, String>>(headers);
 186  
         }
 187  
     };
 188  
 
 189  
     /**
 190  
      * Base request.
 191  
      */
 192  
     private final transient Request base;
 193  
 
 194  
     /**
 195  
      * Public ctor.
 196  
      * @param url The resource to work with
 197  
      */
 198  
     public ApacheRequest(final URL url) {
 199  0
         this(url.toString());
 200  0
     }
 201  
 
 202  
     /**
 203  
      * Public ctor.
 204  
      * @param uri The resource to work with
 205  
      */
 206  
     public ApacheRequest(final URI uri) {
 207  23
         this(uri.toString());
 208  23
     }
 209  
 
 210  
     /**
 211  
      * Public ctor.
 212  
      * @param uri The resource to work with
 213  
      */
 214  23
     public ApacheRequest(final String uri) {
 215  23
         this.base = new BaseRequest(ApacheRequest.WIRE, uri);
 216  23
     }
 217  
 
 218  
     @Override
 219  
     public RequestURI uri() {
 220  3
         return this.base.uri();
 221  
     }
 222  
 
 223  
     @Override
 224  
     public Request header(final String name, final Object value) {
 225  0
         return this.base.header(name, value);
 226  
     }
 227  
 
 228  
     @Override
 229  
     public Request reset(final String name) {
 230  0
         return this.base.reset(name);
 231  
     }
 232  
 
 233  
     @Override
 234  
     public RequestBody body() {
 235  0
         return this.base.body();
 236  
     }
 237  
 
 238  
     @Override
 239  
     public RequestBody multipartBody() {
 240  0
         return this.base.multipartBody();
 241  
     }
 242  
 
 243  
     @Override
 244  
     public Request method(final String method) {
 245  11
         return this.base.method(method);
 246  
     }
 247  
 
 248  
     @Override
 249  
     public Request timeout(final int connect, final int read) {
 250  0
         return this.base.timeout(connect, read);
 251  
     }
 252  
 
 253  
     @Override
 254  
     public Response fetch() throws IOException {
 255  1
         return this.base.fetch();
 256  
     }
 257  
 
 258  
     @Override
 259  
     public Response fetch(final InputStream stream) throws IOException {
 260  0
         return this.base.fetch(stream);
 261  
     }
 262  
 
 263  
     @Override
 264  
     public <T extends Wire> Request through(final Class<T> type,
 265  
         final Object... args) {
 266  8
         return this.base.through(type, args);
 267  
     }
 268  
 
 269  
 }