|
15 | 15 | import org.apache.http.HttpResponse; |
16 | 16 | import org.apache.http.client.methods.HttpGet; |
17 | 17 | import org.apache.http.impl.client.CloseableHttpClient; |
18 | | -import org.apache.http.impl.client.DefaultHttpClient; |
19 | | -import org.apache.http.params.BasicHttpParams; |
20 | | -import org.apache.http.params.HttpParams; |
| 18 | +import org.apache.http.impl.client.HttpClientBuilder; |
21 | 19 | import org.apache.http.util.EntityUtils; |
22 | 20 | import org.junit.Before; |
23 | 21 | import org.junit.Test; |
|
27 | 25 |
|
28 | 26 | public class HttpClientUnshortenLiveTest { |
29 | 27 |
|
30 | | - private CloseableHttpClient client; |
31 | | - |
32 | | - // fixtures |
33 | | - |
34 | | - @Before |
35 | | - public final void before() { |
36 | | - final HttpParams httpParameters = new BasicHttpParams(); |
37 | | - httpParameters.setParameter("http.protocol.handle-redirects", false); |
38 | | - |
39 | | - client = new DefaultHttpClient(httpParameters); |
40 | | - } |
41 | | - |
42 | | - // tests |
43 | | - |
44 | | - @Test |
45 | | - public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException { |
46 | | - final String expectedResult = "http://www.baeldung.com/rest-versioning"; |
47 | | - final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1"); |
48 | | - assertThat(actualResult, equalTo(expectedResult)); |
49 | | - } |
50 | | - |
51 | | - @Test |
52 | | - public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException { |
53 | | - final String expectedResult = "http://www.baeldung.com/rest-versioning"; |
54 | | - final String actualResult = expand("http://t.co/e4rDDbnzmk"); |
55 | | - assertThat(actualResult, equalTo(expectedResult)); |
56 | | - } |
57 | | - |
58 | | - // API |
59 | | - |
60 | | - final String expand(final String urlArg) throws IOException { |
61 | | - String originalUrl = urlArg; |
62 | | - String newUrl = expandSingleLevel(originalUrl); |
63 | | - while (!originalUrl.equals(newUrl)) { |
64 | | - originalUrl = newUrl; |
65 | | - newUrl = expandSingleLevel(originalUrl); |
66 | | - } |
67 | | - |
68 | | - return newUrl; |
69 | | - } |
70 | | - |
71 | | - final String expandSafe(final String urlArg) throws IOException { |
72 | | - String originalUrl = urlArg; |
73 | | - String newUrl = expandSingleLevelSafe(originalUrl).getRight(); |
74 | | - final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl); |
75 | | - while (!originalUrl.equals(newUrl)) { |
76 | | - originalUrl = newUrl; |
77 | | - final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl); |
78 | | - newUrl = statusAndUrl.getRight(); |
79 | | - final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302; |
80 | | - if (isRedirect && alreadyVisited.contains(newUrl)) { |
81 | | - throw new IllegalStateException("Likely a redirect loop"); |
82 | | - } |
83 | | - alreadyVisited.add(newUrl); |
84 | | - } |
85 | | - |
86 | | - return newUrl; |
87 | | - } |
88 | | - |
89 | | - final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException { |
90 | | - HttpGet request = null; |
91 | | - HttpEntity httpEntity = null; |
92 | | - InputStream entityContentStream = null; |
93 | | - |
94 | | - try { |
95 | | - request = new HttpGet(url); |
96 | | - final HttpResponse httpResponse = client.execute(request); |
97 | | - |
98 | | - httpEntity = httpResponse.getEntity(); |
99 | | - entityContentStream = httpEntity.getContent(); |
100 | | - |
101 | | - final int statusCode = httpResponse.getStatusLine().getStatusCode(); |
102 | | - if (statusCode != 301 && statusCode != 302) { |
103 | | - return new ImmutablePair<Integer, String>(statusCode, url); |
104 | | - } |
105 | | - final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); |
106 | | - Preconditions.checkState(headers.length == 1); |
107 | | - final String newUrl = headers[0].getValue(); |
108 | | - |
109 | | - return new ImmutablePair<Integer, String>(statusCode, newUrl); |
110 | | - } catch (final IllegalArgumentException uriEx) { |
111 | | - return new ImmutablePair<Integer, String>(500, url); |
112 | | - } finally { |
113 | | - if (request != null) { |
114 | | - request.releaseConnection(); |
115 | | - } |
116 | | - if (entityContentStream != null) { |
117 | | - entityContentStream.close(); |
118 | | - } |
119 | | - if (httpEntity != null) { |
120 | | - EntityUtils.consume(httpEntity); |
121 | | - } |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - final String expandSingleLevel(final String url) throws IOException { |
126 | | - HttpGet request = null; |
127 | | - HttpEntity httpEntity = null; |
128 | | - InputStream entityContentStream = null; |
129 | | - |
130 | | - try { |
131 | | - request = new HttpGet(url); |
132 | | - final HttpResponse httpResponse = client.execute(request); |
133 | | - |
134 | | - httpEntity = httpResponse.getEntity(); |
135 | | - entityContentStream = httpEntity.getContent(); |
136 | | - |
137 | | - final int statusCode = httpResponse.getStatusLine().getStatusCode(); |
138 | | - if (statusCode != 301 && statusCode != 302) { |
139 | | - return url; |
140 | | - } |
141 | | - final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); |
142 | | - Preconditions.checkState(headers.length == 1); |
143 | | - final String newUrl = headers[0].getValue(); |
144 | | - |
145 | | - return newUrl; |
146 | | - } catch (final IllegalArgumentException uriEx) { |
147 | | - return url; |
148 | | - } finally { |
149 | | - if (request != null) { |
150 | | - request.releaseConnection(); |
151 | | - } |
152 | | - if (entityContentStream != null) { |
153 | | - entityContentStream.close(); |
154 | | - } |
155 | | - if (httpEntity != null) { |
156 | | - EntityUtils.consume(httpEntity); |
157 | | - } |
158 | | - } |
159 | | - } |
| 28 | + private CloseableHttpClient client; |
| 29 | + |
| 30 | + // fixtures |
| 31 | + |
| 32 | + @Before |
| 33 | + public final void before() { |
| 34 | + client = HttpClientBuilder.create().disableRedirectHandling().build(); |
| 35 | + } |
| 36 | + |
| 37 | + // tests |
| 38 | + |
| 39 | + @Test |
| 40 | + public final void givenShortenedOnce_whenUrlIsUnshortened_thenCorrectResult() throws IOException { |
| 41 | + final String expectedResult = "http://www.baeldung.com/rest-versioning"; |
| 42 | + final String actualResult = expandSingleLevel("http://bit.ly/13jEoS1"); |
| 43 | + assertThat(actualResult, equalTo(expectedResult)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public final void givenShortenedMultiple_whenUrlIsUnshortened_thenCorrectResult() throws IOException { |
| 48 | + final String expectedResult = "http://www.baeldung.com/rest-versioning"; |
| 49 | + final String actualResult = expand("http://t.co/e4rDDbnzmk"); |
| 50 | + assertThat(actualResult, equalTo(expectedResult)); |
| 51 | + } |
| 52 | + |
| 53 | + // API |
| 54 | + |
| 55 | + final String expand(final String urlArg) throws IOException { |
| 56 | + String originalUrl = urlArg; |
| 57 | + String newUrl = expandSingleLevel(originalUrl); |
| 58 | + while (!originalUrl.equals(newUrl)) { |
| 59 | + originalUrl = newUrl; |
| 60 | + newUrl = expandSingleLevel(originalUrl); |
| 61 | + } |
| 62 | + |
| 63 | + return newUrl; |
| 64 | + } |
| 65 | + |
| 66 | + final String expandSafe(final String urlArg) throws IOException { |
| 67 | + String originalUrl = urlArg; |
| 68 | + String newUrl = expandSingleLevelSafe(originalUrl).getRight(); |
| 69 | + final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl); |
| 70 | + while (!originalUrl.equals(newUrl)) { |
| 71 | + originalUrl = newUrl; |
| 72 | + final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl); |
| 73 | + newUrl = statusAndUrl.getRight(); |
| 74 | + final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302; |
| 75 | + if (isRedirect && alreadyVisited.contains(newUrl)) { |
| 76 | + throw new IllegalStateException("Likely a redirect loop"); |
| 77 | + } |
| 78 | + alreadyVisited.add(newUrl); |
| 79 | + } |
| 80 | + |
| 81 | + return newUrl; |
| 82 | + } |
| 83 | + |
| 84 | + final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException { |
| 85 | + HttpGet request = null; |
| 86 | + HttpEntity httpEntity = null; |
| 87 | + InputStream entityContentStream = null; |
| 88 | + |
| 89 | + try { |
| 90 | + request = new HttpGet(url); |
| 91 | + final HttpResponse httpResponse = client.execute(request); |
| 92 | + |
| 93 | + httpEntity = httpResponse.getEntity(); |
| 94 | + entityContentStream = httpEntity.getContent(); |
| 95 | + |
| 96 | + final int statusCode = httpResponse.getStatusLine().getStatusCode(); |
| 97 | + if (statusCode != 301 && statusCode != 302) { |
| 98 | + return new ImmutablePair<Integer, String>(statusCode, url); |
| 99 | + } |
| 100 | + final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); |
| 101 | + Preconditions.checkState(headers.length == 1); |
| 102 | + final String newUrl = headers[0].getValue(); |
| 103 | + |
| 104 | + return new ImmutablePair<Integer, String>(statusCode, newUrl); |
| 105 | + } catch (final IllegalArgumentException uriEx) { |
| 106 | + return new ImmutablePair<Integer, String>(500, url); |
| 107 | + } finally { |
| 108 | + if (request != null) { |
| 109 | + request.releaseConnection(); |
| 110 | + } |
| 111 | + if (entityContentStream != null) { |
| 112 | + entityContentStream.close(); |
| 113 | + } |
| 114 | + if (httpEntity != null) { |
| 115 | + EntityUtils.consume(httpEntity); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + final String expandSingleLevel(final String url) throws IOException { |
| 121 | + HttpGet request = null; |
| 122 | + HttpEntity httpEntity = null; |
| 123 | + InputStream entityContentStream = null; |
| 124 | + |
| 125 | + try { |
| 126 | + request = new HttpGet(url); |
| 127 | + final HttpResponse httpResponse = client.execute(request); |
| 128 | + |
| 129 | + httpEntity = httpResponse.getEntity(); |
| 130 | + entityContentStream = httpEntity.getContent(); |
| 131 | + |
| 132 | + final int statusCode = httpResponse.getStatusLine().getStatusCode(); |
| 133 | + if (statusCode != 301 && statusCode != 302) { |
| 134 | + return url; |
| 135 | + } |
| 136 | + final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); |
| 137 | + Preconditions.checkState(headers.length == 1); |
| 138 | + final String newUrl = headers[0].getValue(); |
| 139 | + |
| 140 | + return newUrl; |
| 141 | + } catch (final IllegalArgumentException uriEx) { |
| 142 | + return url; |
| 143 | + } finally { |
| 144 | + if (request != null) { |
| 145 | + request.releaseConnection(); |
| 146 | + } |
| 147 | + if (entityContentStream != null) { |
| 148 | + entityContentStream.close(); |
| 149 | + } |
| 150 | + if (httpEntity != null) { |
| 151 | + EntityUtils.consume(httpEntity); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
160 | 155 |
|
161 | 156 | } |
0 commit comments