Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OneMinuteWire |
|
| 1.0;1 |
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.Timeable; | |
34 | import com.jcabi.http.Request; | |
35 | import com.jcabi.http.Response; | |
36 | import com.jcabi.http.Wire; | |
37 | import java.io.IOException; | |
38 | import java.io.InputStream; | |
39 | import java.util.Collection; | |
40 | import java.util.Map; | |
41 | import java.util.concurrent.TimeUnit; | |
42 | import lombok.EqualsAndHashCode; | |
43 | import lombok.ToString; | |
44 | ||
45 | /** | |
46 | * Wire that throws an {@link IOException} if a request takes longer | |
47 | * than a minute. | |
48 | * | |
49 | * <p>It's recommended to use this decorator in production, in order | |
50 | * to avoid stuck requests: | |
51 | * | |
52 | * <pre> String html = new JdkRequest("http://goggle.com") | |
53 | * .through(OneMinuteWire.class) | |
54 | * .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | |
55 | * .fetch() | |
56 | * .body();</pre> | |
57 | * | |
58 | * <p>The class is immutable and thread-safe. | |
59 | * | |
60 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
61 | * @version $Id: d03f8f28e663369f3f5a119926ad2a4d2f6ff30c $ | |
62 | * @since 0.10 | |
63 | */ | |
64 | @Immutable | |
65 | 0 | @ToString(of = "origin") |
66 | 0 | @EqualsAndHashCode(of = "origin") |
67 | public final class OneMinuteWire implements Wire { | |
68 | ||
69 | /** | |
70 | * Original wire. | |
71 | */ | |
72 | private final transient Wire origin; | |
73 | ||
74 | /** | |
75 | * Public ctor. | |
76 | * @param wire Original wire | |
77 | */ | |
78 | 0 | public OneMinuteWire(final Wire wire) { |
79 | 0 | this.origin = wire; |
80 | 0 | } |
81 | ||
82 | // @checkstyle ParameterNumber (5 lines) | |
83 | @Override | |
84 | @Timeable(limit = 1, unit = TimeUnit.MINUTES) | |
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 | 0 | return this.origin.send( |
92 | req, home, method, headers, content, connect, read | |
93 | ); | |
94 | } | |
95 | } |