diff --git a/spring-security-rest-custom/pom.xml b/spring-security-rest-custom/pom.xml
index 4e5436523f..b3bf977745 100644
--- a/spring-security-rest-custom/pom.xml
+++ b/spring-security-rest-custom/pom.xml
@@ -134,6 +134,11 @@
guava
${guava.version}
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
diff --git a/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java b/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java
index 1e1a08088b..03378284b1 100644
--- a/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java
+++ b/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java
@@ -5,7 +5,10 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;
+import java.util.List;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
@@ -19,6 +22,7 @@ import org.junit.Before;
import org.junit.Test;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
public class HttpLiveServiceTemp {
@@ -63,6 +67,60 @@ public class HttpLiveServiceTemp {
return newUrl;
}
+ final String expandSafe(final String urlArg) throws IOException {
+ String originalUrl = urlArg;
+ String newUrl = expandSingleLevelSafe(originalUrl).getRight();
+ final List alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
+ while (!originalUrl.equals(newUrl)) {
+ originalUrl = newUrl;
+ final Pair statusAndUrl = expandSingleLevelSafe(originalUrl);
+ newUrl = statusAndUrl.getRight();
+ final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
+ if (isRedirect && alreadyVisited.contains(newUrl)) {
+ throw new IllegalStateException("Likely a redirect loop");
+ }
+ alreadyVisited.add(newUrl);
+ }
+
+ return newUrl;
+ }
+
+ final Pair expandSingleLevelSafe(final String url) throws IOException {
+ HttpGet request = null;
+ HttpEntity httpEntity = null;
+ InputStream entityContentStream = null;
+
+ try {
+ request = new HttpGet(url);
+ final HttpResponse httpResponse = client.execute(request);
+
+ httpEntity = httpResponse.getEntity();
+ entityContentStream = httpEntity.getContent();
+
+ final int statusCode = httpResponse.getStatusLine().getStatusCode();
+ if (statusCode != 301 && statusCode != 302) {
+ return new ImmutablePair(statusCode, url);
+ }
+ final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
+ Preconditions.checkState(headers.length == 1);
+ final String newUrl = headers[0].getValue();
+
+ return new ImmutablePair(statusCode, newUrl);
+ } catch (final IllegalArgumentException uriEx) {
+ return new ImmutablePair(500, url);
+ } finally {
+ if (request != null) {
+ request.releaseConnection();
+ }
+ if (entityContentStream != null) {
+ entityContentStream.close();
+ }
+ if (httpEntity != null) {
+ EntityUtils.consume(httpEntity);
+ }
+ }
+ }
+
final String expandSingleLevel(final String url) throws IOException {
HttpGet request = null;
HttpEntity httpEntity = null;