testing work
This commit is contained in:
parent
1157dc6b58
commit
cb6ee3684c
|
@ -134,6 +134,11 @@
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,10 @@ import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
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.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
|
@ -19,6 +22,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
public class HttpLiveServiceTemp {
|
public class HttpLiveServiceTemp {
|
||||||
|
|
||||||
|
@ -63,6 +67,60 @@ public class HttpLiveServiceTemp {
|
||||||
return newUrl;
|
return newUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final String expandSafe(final String urlArg) throws IOException {
|
||||||
|
String originalUrl = urlArg;
|
||||||
|
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
|
||||||
|
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
|
||||||
|
while (!originalUrl.equals(newUrl)) {
|
||||||
|
originalUrl = newUrl;
|
||||||
|
final Pair<Integer, String> 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<Integer, String> 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<Integer, String>(statusCode, url);
|
||||||
|
}
|
||||||
|
final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
|
||||||
|
Preconditions.checkState(headers.length == 1);
|
||||||
|
final String newUrl = headers[0].getValue();
|
||||||
|
|
||||||
|
return new ImmutablePair<Integer, String>(statusCode, newUrl);
|
||||||
|
} catch (final IllegalArgumentException uriEx) {
|
||||||
|
return new ImmutablePair<Integer, String>(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 {
|
final String expandSingleLevel(final String url) throws IOException {
|
||||||
HttpGet request = null;
|
HttpGet request = null;
|
||||||
HttpEntity httpEntity = null;
|
HttpEntity httpEntity = null;
|
||||||
|
|
Loading…
Reference in New Issue