|
5 | 5 |
|
6 | 6 | import java.io.IOException; |
7 | 7 | import java.io.InputStream; |
| 8 | +import java.util.List; |
8 | 9 |
|
| 10 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 11 | +import org.apache.commons.lang3.tuple.Pair; |
9 | 12 | import org.apache.http.Header; |
10 | 13 | import org.apache.http.HttpEntity; |
11 | 14 | import org.apache.http.HttpHeaders; |
|
19 | 22 | import org.junit.Test; |
20 | 23 |
|
21 | 24 | import com.google.common.base.Preconditions; |
| 25 | +import com.google.common.collect.Lists; |
22 | 26 |
|
23 | 27 | public class HttpLiveServiceTemp { |
24 | 28 |
|
@@ -63,6 +67,60 @@ final String expand(final String urlArg) throws IOException { |
63 | 67 | return newUrl; |
64 | 68 | } |
65 | 69 |
|
| 70 | + final String expandSafe(final String urlArg) throws IOException { |
| 71 | + String originalUrl = urlArg; |
| 72 | + String newUrl = expandSingleLevelSafe(originalUrl).getRight(); |
| 73 | + final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl); |
| 74 | + while (!originalUrl.equals(newUrl)) { |
| 75 | + originalUrl = newUrl; |
| 76 | + final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl); |
| 77 | + newUrl = statusAndUrl.getRight(); |
| 78 | + final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302; |
| 79 | + if (isRedirect && alreadyVisited.contains(newUrl)) { |
| 80 | + throw new IllegalStateException("Likely a redirect loop"); |
| 81 | + } |
| 82 | + alreadyVisited.add(newUrl); |
| 83 | + } |
| 84 | + |
| 85 | + return newUrl; |
| 86 | + } |
| 87 | + |
| 88 | + final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException { |
| 89 | + HttpGet request = null; |
| 90 | + HttpEntity httpEntity = null; |
| 91 | + InputStream entityContentStream = null; |
| 92 | + |
| 93 | + try { |
| 94 | + request = new HttpGet(url); |
| 95 | + final HttpResponse httpResponse = client.execute(request); |
| 96 | + |
| 97 | + httpEntity = httpResponse.getEntity(); |
| 98 | + entityContentStream = httpEntity.getContent(); |
| 99 | + |
| 100 | + final int statusCode = httpResponse.getStatusLine().getStatusCode(); |
| 101 | + if (statusCode != 301 && statusCode != 302) { |
| 102 | + return new ImmutablePair<Integer, String>(statusCode, url); |
| 103 | + } |
| 104 | + final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); |
| 105 | + Preconditions.checkState(headers.length == 1); |
| 106 | + final String newUrl = headers[0].getValue(); |
| 107 | + |
| 108 | + return new ImmutablePair<Integer, String>(statusCode, newUrl); |
| 109 | + } catch (final IllegalArgumentException uriEx) { |
| 110 | + return new ImmutablePair<Integer, String>(500, url); |
| 111 | + } finally { |
| 112 | + if (request != null) { |
| 113 | + request.releaseConnection(); |
| 114 | + } |
| 115 | + if (entityContentStream != null) { |
| 116 | + entityContentStream.close(); |
| 117 | + } |
| 118 | + if (httpEntity != null) { |
| 119 | + EntityUtils.consume(httpEntity); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
66 | 124 | final String expandSingleLevel(final String url) throws IOException { |
67 | 125 | HttpGet request = null; |
68 | 126 | HttpEntity httpEntity = null; |
|
0 commit comments