Add latest url to the response to make detection of a redirect possible

Will be latest one in this commit because there's need to check the
history of redirects as of now.
This commit is contained in:
Mauricio Colli 2020-02-29 17:57:23 -03:00 committed by TobiGr
parent 3441946bea
commit 5edd774fc4
2 changed files with 19 additions and 3 deletions

View file

@ -15,12 +15,16 @@ public class Response {
private final Map<String, List<String>> responseHeaders;
private final String responseBody;
public Response(int responseCode, String responseMessage, Map<String, List<String>> responseHeaders, @Nullable String responseBody) {
private final String latestUrl;
public Response(int responseCode, String responseMessage, Map<String, List<String>> responseHeaders,
@Nullable String responseBody, @Nullable String latestUrl) {
this.responseCode = responseCode;
this.responseMessage = responseMessage;
this.responseHeaders = responseHeaders != null ? responseHeaders : Collections.<String, List<String>>emptyMap();
this.responseBody = responseBody == null ? "" : responseBody;
this.latestUrl = latestUrl;
}
public int responseCode() {
@ -40,6 +44,16 @@ public class Response {
return responseBody;
}
/**
* Used for detecting a possible redirection, limited to the latest one.
*
* @return latest url known right before this response object was created
*/
@Nonnull
public String latestUrl() {
return latestUrl;
}
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/

View file

@ -99,8 +99,9 @@ public class DownloaderTestImpl extends Downloader {
final int responseCode = connection.getResponseCode();
final String responseMessage = connection.getResponseMessage();
final Map<String, List<String>> responseHeaders = connection.getHeaderFields();
final String latestUrl = connection.getURL().toString();
return new Response(responseCode, responseMessage, responseHeaders, response.toString());
return new Response(responseCode, responseMessage, responseHeaders, response.toString(), latestUrl);
} catch (Exception e) {
final int responseCode = connection.getResponseCode();
@ -112,7 +113,8 @@ public class DownloaderTestImpl extends Downloader {
if (responseCode == 429) {
throw new ReCaptchaException("reCaptcha Challenge requested", url);
} else if (responseCode != -1) {
return new Response(responseCode, connection.getResponseMessage(), connection.getHeaderFields(), null);
final String latestUrl = connection.getURL().toString();
return new Response(responseCode, connection.getResponseMessage(), connection.getHeaderFields(), null, latestUrl);
}
throw new IOException("Error occurred while fetching the content", e);