Coverage Report - com.jcabi.http.wire.RetryWire
 
Classes in this File Line Coverage Branch Coverage Complexity
RetryWire
68%
11/16
18%
3/16
4
 
 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.wire;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Tv;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.Response;
 36  
 import com.jcabi.http.Wire;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 import java.net.HttpURLConnection;
 41  
 import java.util.Collection;
 42  
 import java.util.Map;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 
 46  
 /**
 47  
  * Wire that retries a few times before giving up and throwing exception.
 48  
  *
 49  
  * <p>This wire retries again (at least three times) if an original one throws
 50  
  * {@link IOException}:
 51  
  *
 52  
  * <pre> String html = new JdkRequest("http://goggle.com")
 53  
  *   .through(RetryWire.class)
 54  
  *   .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN)
 55  
  *   .fetch()
 56  
  *   .body();</pre>
 57  
  *
 58  
  * <p>Since version 1.9 this wire retries also if HTTP status code
 59  
  * is between 500 and 599.
 60  
  *
 61  
  * <p>The class is immutable and thread-safe.
 62  
  *
 63  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 64  
  * @version $Id: 44cfd76ad2a1e04bfbfe565717ba1723a471fede $
 65  
  * @since 0.10
 66  
  */
 67  
 @Immutable
 68  0
 @ToString(of = "origin")
 69  0
 @EqualsAndHashCode(of = "origin")
 70  
 public final class RetryWire implements Wire {
 71  
 
 72  
     /**
 73  
      * Original wire.
 74  
      */
 75  
     private final transient Wire origin;
 76  
 
 77  
     /**
 78  
      * Public ctor.
 79  
      * @param wire Original wire
 80  
      */
 81  1
     public RetryWire(final Wire wire) {
 82  1
         this.origin = wire;
 83  1
     }
 84  
 
 85  
     // @checkstyle ParameterNumber (13 lines)
 86  
     @Override
 87  
     public Response send(final Request req, final String home,
 88  
         final String method,
 89  
         final Collection<Map.Entry<String, String>> headers,
 90  
         final InputStream content,
 91  
         final int connect, final int read) throws IOException {
 92  1
         int attempt = 0;
 93  
         while (true) {
 94  3
             if (attempt > Tv.THREE) {
 95  0
                 throw new IOException(
 96  
                     String.format("failed after %d attempts", attempt)
 97  
                 );
 98  
             }
 99  
             try {
 100  3
                 final Response rsp = this.origin.send(
 101  
                     req, home, method, headers, content,
 102  
                     connect, read
 103  
                 );
 104  3
                 if (rsp.status() < HttpURLConnection.HTTP_INTERNAL_ERROR) {
 105  1
                     return rsp;
 106  
                 }
 107  2
                 Logger.warn(
 108  
                     this, "%s %s returns %d status (attempt #%d)",
 109  
                     method, home, rsp.status(), attempt + 1
 110  
                 );
 111  0
             } catch (final IOException ex) {
 112  0
                 Logger.warn(
 113  
                     this, "%s: %s",
 114  
                     ex.getClass().getName(), ex.getLocalizedMessage()
 115  
                 );
 116  2
             }
 117  2
             ++attempt;
 118  
         }
 119  
     }
 120  
 }