Coverage Report - com.jcabi.github.wire.RetryCarefulWire
 
Classes in this File Line Coverage Branch Coverage Complexity
RetryCarefulWire
71%
5/7
0%
0/10
1
 
 1  2
 /**
 2  
  * Copyright (c) 2013-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.github.wire;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.http.Request;
 34  
 import com.jcabi.http.Response;
 35  
 import com.jcabi.http.Wire;
 36  
 import com.jcabi.http.wire.RetryWire;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.util.Collection;
 40  
 import java.util.Map;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 
 45  
 /**
 46  
  * Wire that waits if the number of remaining requests per hour is less than
 47  
  * a given threshold, and in the event of {@link IOException} retries a few
 48  
  * times before giving up and rethrowing the exception.
 49  
  *
 50  
  * <p>Just a wrapper for a {@link com.jcabi.http.wire.RetryWire} that wraps a
 51  
  * {@link com.jcabi.github.wire.CarefulWire} that wraps the underlying wire.
 52  
  *
 53  
  * <p>You can use {@code RetryCarefulWire} with a
 54  
  * {@link com.jcabi.github.Github} object:
 55  
  * <pre>
 56  
  * {@code
 57  
  * Github github = new RtGithub(
 58  
  *     new RtGithub().entry().through(RetryCarefulWire.class, 50)
 59  
  * );
 60  
  * }
 61  
  * </pre>
 62  
  *
 63  
  * @author Chris Rebert (github@chrisrebert.com)
 64  
  * @version $Id$
 65  
  * @since 0.23
 66  
  */
 67  
 @Immutable
 68  0
 @ToString
 69  0
 @EqualsAndHashCode(of = { "real" })
 70  
 public final class RetryCarefulWire implements Wire {
 71  
     /**
 72  
      * RetryWire which we're merely wrapping.
 73  
      */
 74  
     private final transient Wire real;
 75  
 
 76  
     /**
 77  
      * Public ctor.
 78  
      *
 79  
      * @param wire Original wire
 80  
      * @param threshold Threshold of number of remaining requests, below which
 81  
      *  requests are blocked until reset
 82  
      */
 83  
     public RetryCarefulWire(@NotNull(message = "wire can't be NULL")
 84  3
         final Wire wire, final int threshold) {
 85  3
         this.real = new RetryWire(new CarefulWire(wire, threshold));
 86  3
     }
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      * @checkstyle ParameterNumber (6 lines)
 91  
      */
 92  
     @Override
 93  
     @NotNull(message = "response can't be NULL")
 94  
     public Response send(
 95  
         @NotNull(message = "req can't be NULL") final Request req,
 96  
         @NotNull(message = "home can't be NULL") final String home,
 97  
         @NotNull(message = "method can't be NULL")final String method,
 98  
         @NotNull(message = "headers can't be NULL")
 99  
         final Collection<Map.Entry<String, String>> headers,
 100  
         @NotNull(message = "content can't be NULL")
 101  
         final InputStream content
 102  
     ) throws IOException {
 103  3
         return this.real.send(req, home, method, headers, content);
 104  
     }
 105  
 }