Skip to content

Commit cb6ee36

Browse files
author
eugenp
committed
testing work
1 parent 1157dc6 commit cb6ee36

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

spring-security-rest-custom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
<artifactId>guava</artifactId>
135135
<version>${guava.version}</version>
136136
</dependency>
137+
<dependency>
138+
<groupId>org.apache.commons</groupId>
139+
<artifactId>commons-lang3</artifactId>
140+
<version>${commons-lang3.version}</version>
141+
</dependency>
137142

138143
<!-- test scoped -->
139144

spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.util.List;
89

10+
import org.apache.commons.lang3.tuple.ImmutablePair;
11+
import org.apache.commons.lang3.tuple.Pair;
912
import org.apache.http.Header;
1013
import org.apache.http.HttpEntity;
1114
import org.apache.http.HttpHeaders;
@@ -19,6 +22,7 @@
1922
import org.junit.Test;
2023

2124
import com.google.common.base.Preconditions;
25+
import com.google.common.collect.Lists;
2226

2327
public class HttpLiveServiceTemp {
2428

@@ -63,6 +67,60 @@ final String expand(final String urlArg) throws IOException {
6367
return newUrl;
6468
}
6569

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+
66124
final String expandSingleLevel(final String url) throws IOException {
67125
HttpGet request = null;
68126
HttpEntity httpEntity = null;

0 commit comments

Comments
 (0)