Coverage Report - com.jcabi.http.Request
 
Classes in this File Line Coverage Branch Coverage Complexity
Request
N/A
N/A
1
 
 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;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import java.io.IOException;
 34  
 import java.io.InputStream;
 35  
 
 36  
 /**
 37  
  * RESTful request.
 38  
  *
 39  
  * <p>Instance of this class is supposed to be used this way:
 40  
  *
 41  
  * <pre> String name = new ApacheRequest("https://www.example.com:8080")
 42  
  *   .uri().path("/users").queryParam("id", 333).back()
 43  
  *   .method(Request.GET)
 44  
  *   .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
 45  
  *   .fetch()
 46  
  *   .as(RestResponse.class)
 47  
  *   .assertStatus(HttpURLConnection.HTTP_OK)
 48  
  *   .as(XmlResponse.class)
 49  
  *   .assertXPath("/page/links/link[@rel='see']")
 50  
  *   .rel("/page/links/link[@rel='see']/@href")
 51  
  *   .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
 52  
  *   .fetch()
 53  
  *   .as(JsonResponse.class)
 54  
  *   .json().getJsonObject().getString("name");</pre>
 55  
  *
 56  
  * <p>Since version 0.10 it is recommended to use
 57  
  * {@link com.jcabi.http.wire.RetryWire}
 58  
  * decorator to avoid accidental {@link IOException} when connection is weak
 59  
  * or unstable, for example:
 60  
  *
 61  
  * <pre> String body = new JdkRequest("https://www.google.com")
 62  
  *   .through(RetryWire.class)
 63  
  *   .fetch()
 64  
  *   .body();</pre>
 65  
  *
 66  
  * <p>Instances of this interface are immutable and thread-safe.
 67  
  *
 68  
  * <p>You can use either {@link ApacheRequest} or {@link JdkRequest},
 69  
  * according to your needs. {@link JdkRequest} won't require any additional
 70  
  * dependencies, while {@link ApacheRequest} will properly support all
 71  
  * possible HTTP methods ({@link JdkRequest} doesn't support {@code PATCH},
 72  
  * for example).
 73  
  *
 74  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 75  
  * @version $Id: 9ca0e88ef1aaac16f928d5d67fada8c08df4dcc7 $
 76  
  * @since 0.8
 77  
  * @see com.jcabi.http.request.JdkRequest
 78  
  * @see com.jcabi.http.request.ApacheRequest
 79  
  */
 80  
 @Immutable
 81  
 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
 82  
 public interface Request {
 83  
 
 84  
     /**
 85  
      * GET method name.
 86  
      */
 87  
     String GET = "GET";
 88  
 
 89  
     /**
 90  
      * POST method name.
 91  
      */
 92  
     String POST = "POST";
 93  
 
 94  
     /**
 95  
      * PUT method name.
 96  
      */
 97  
     String PUT = "PUT";
 98  
 
 99  
     /**
 100  
      * HEAD method name.
 101  
      */
 102  
     String HEAD = "HEAD";
 103  
 
 104  
     /**
 105  
      * DELETE method name.
 106  
      */
 107  
     String DELETE = "DELETE";
 108  
 
 109  
     /**
 110  
      * OPTIONS method name.
 111  
      */
 112  
     String OPTIONS = "OPTIONS";
 113  
 
 114  
     /**
 115  
      * PATCH method name.
 116  
      */
 117  
     String PATCH = "PATCH";
 118  
 
 119  
     /**
 120  
      * Get destination URI.
 121  
      * @return The destination it is currently pointing to
 122  
      */
 123  
     RequestURI uri();
 124  
 
 125  
     /**
 126  
      * Get request body.
 127  
      * @return New alternated request
 128  
      */
 129  
     RequestBody body();
 130  
 
 131  
     /**
 132  
      * Set request header.
 133  
      * @param name ImmutableHeader name
 134  
      * @param value Value of the header to set
 135  
      * @return New alternated request
 136  
      */
 137  
     Request header(String name, Object value);
 138  
 
 139  
     /**
 140  
      * Remove all headers with this name.
 141  
      * @param name ImmutableHeader name
 142  
      * @return New alternated request
 143  
      * @since 0.10
 144  
      */
 145  
     Request reset(String name);
 146  
 
 147  
     /**
 148  
      * Use this method.
 149  
      * @param method The method to use
 150  
      * @return New alternated request
 151  
      */
 152  
     Request method(String method);
 153  
 
 154  
     /**
 155  
      * Use this timeout values.
 156  
      * @param connect The connect timeout to use in ms
 157  
      * @param read The read timeout to use in ms
 158  
      * @return New alternated request
 159  
      */
 160  
     Request timeout(int connect, int read);
 161  
 
 162  
     /**
 163  
      * Execute it with a specified HTTP method.
 164  
      * @return Response
 165  
      * @throws IOException If fails to fetch HTTP request
 166  
      */
 167  
     Response fetch() throws IOException;
 168  
 
 169  
     /**
 170  
      * Execute this request using the content provided by the
 171  
      * {@link InputStream} being passed as the request body. Note that the
 172  
      * request MUST have an empty body when this method is being invoked, or
 173  
      * it will throw an {@link IllegalStateException}.
 174  
      *
 175  
      * @param stream The input stream to use
 176  
      * @return Response
 177  
      * @throws IOException If fails to fetch HTTP request
 178  
      * @since 1.8
 179  
      */
 180  
     Response fetch(InputStream stream) throws IOException;
 181  
 
 182  
     /**
 183  
      * Send it through a decorating {@link Wire}.
 184  
      * @param type Type of wire to use
 185  
      * @param args Optional arguments for the wire constructor
 186  
      * @param <T> Type to use
 187  
      * @return New request with a wire decorated
 188  
      * @since 0.10
 189  
      */
 190  
     <T extends Wire> Request through(Class<T> type, Object... args);
 191  
 
 192  
 }