Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AutoRedirectingWire |
|
| 3.6666666666666665;3.667 |
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 java.io.IOException; | |
38 | import java.io.InputStream; | |
39 | import java.net.HttpURLConnection; | |
40 | import java.net.URI; | |
41 | import java.util.Collection; | |
42 | import java.util.List; | |
43 | import java.util.Map; | |
44 | import java.util.concurrent.TimeUnit; | |
45 | import javax.ws.rs.core.HttpHeaders; | |
46 | import lombok.EqualsAndHashCode; | |
47 | import lombok.ToString; | |
48 | ||
49 | /** | |
50 | * Auto Redirecting Wire. | |
51 | * | |
52 | * <p>This wire will retry a request a certain number of times (default: 5) | |
53 | * after a short delay when a HTTP response with a status code of 300-399 is | |
54 | * received. On every next attempt a new URL will be used, according | |
55 | * to the value of {@code Location} HTTP header of the response. | |
56 | * | |
57 | * <p>If the maximum number of retries are reached, the last response | |
58 | * received is returned to the caller, regardless of its status code. | |
59 | * | |
60 | * <pre> String html = new JdkRequest("http://goggle.com") | |
61 | * .through(AutoRedirectingWire.class) | |
62 | * .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) | |
63 | * .fetch() | |
64 | * .body();</pre> | |
65 | * | |
66 | * <p>The class is immutable and thread-safe. | |
67 | * | |
68 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
69 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
70 | * @version $Id: 5afc9087c0f6b27898f3e085e213cde99f01694f $ | |
71 | * @since 1.6 | |
72 | */ | |
73 | @Immutable | |
74 | 0 | @ToString(of = "origin") |
75 | 0 | @EqualsAndHashCode(of = { "origin", "max" }) |
76 | public final class AutoRedirectingWire implements Wire { | |
77 | /** | |
78 | * Original wire. | |
79 | */ | |
80 | private final transient Wire origin; | |
81 | ||
82 | /** | |
83 | * Maximum number of retries to be made. | |
84 | */ | |
85 | private final transient int max; | |
86 | ||
87 | /** | |
88 | * Public ctor. | |
89 | * @param wire Original wire | |
90 | */ | |
91 | public AutoRedirectingWire(final Wire wire) { | |
92 | 1 | this(wire, Tv.FIVE); |
93 | 1 | } |
94 | ||
95 | /** | |
96 | * Public ctor. | |
97 | * @param wire Original wire | |
98 | * @param retries Maximum number of retries | |
99 | */ | |
100 | 2 | public AutoRedirectingWire(final Wire wire, final int retries) { |
101 | 2 | this.origin = wire; |
102 | 2 | this.max = retries; |
103 | 2 | } |
104 | ||
105 | // @checkstyle ParameterNumber (5 lines) | |
106 | @Override | |
107 | public Response send(final Request req, final String home, | |
108 | final String method, | |
109 | final Collection<Map.Entry<String, String>> headers, | |
110 | final InputStream content, | |
111 | final int connect, | |
112 | final int read) throws IOException { | |
113 | 2 | Response response = this.origin.send( |
114 | req, home, method, headers, content, connect, read | |
115 | ); | |
116 | 2 | int attempt = 1; |
117 | 2 | final URI uri = URI.create(home); |
118 | 6 | while (attempt < this.max) { |
119 | 5 | if (response.status() < HttpURLConnection.HTTP_MULT_CHOICE |
120 | || response.status() >= HttpURLConnection.HTTP_BAD_REQUEST) { | |
121 | 0 | break; |
122 | } | |
123 | 4 | final List<String> locations = response.headers().get( |
124 | HttpHeaders.LOCATION | |
125 | ); | |
126 | 4 | if (locations == null || locations.size() != 1) { |
127 | 0 | break; |
128 | } | |
129 | 4 | URI location = URI.create(locations.get(0)); |
130 | 4 | if (!location.isAbsolute()) { |
131 | 4 | location = uri.resolve(uri); |
132 | } | |
133 | 4 | response = this.origin.send( |
134 | req, location.toString(), | |
135 | method, headers, content, connect, read | |
136 | ); | |
137 | try { | |
138 | 4 | TimeUnit.SECONDS.sleep((long) attempt); |
139 | 0 | } catch (final InterruptedException ex) { |
140 | 0 | throw new IOException(ex); |
141 | 4 | } |
142 | 4 | ++attempt; |
143 | 4 | } |
144 | 2 | return response; |
145 | } | |
146 | ||
147 | } |