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