Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.schabi.newpipe.extractor.exceptions.HttpResponseException;

/**
* A Data class used to hold the results from requests made by the Downloader implementation.
*/
@SuppressWarnings("checkstyle:NeedBraces")
public class Response {
private final int responseCode;
private final String responseMessage;
Expand Down Expand Up @@ -80,4 +84,135 @@ public String getHeader(final String name) {

return null;
}

/**
* Ensure the response code is 2xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is not 2xx
*/
public Response ensureSuccessResponseCode() throws HttpResponseException {
return ensureResponseCodeInRange(200, 299);
}

/**
* Ensure the response code is 3xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is not 3xx
*/
public Response ensureRedirectResponseCode() throws HttpResponseException {
return ensureResponseCodeInRange(300, 399);
}

/**
* Ensure the response code is not 4xx or 5xx
* @return this {@code Response}
* @throws HttpResponseException if the response code is client or server error
*/
public Response ensureResponseCodeIsNotError() throws HttpResponseException {
return ensureResponseCodeNotInRange(400, 599);
}

/**
* Ensure the HTTP response code is within range of min and max inclusive
* @return this Response
* @throws HttpResponseException if the code is outside the range
*/
public Response ensureResponseCodeInRange(final int min, final int max)
throws HttpResponseException {
if (min > max) throw new IllegalArgumentException("min must be less than max");
if (responseCode() < min || responseCode() > max) {
throw new HttpResponseException(this);
}
return this;
}

public Response ensureResponseCodeNotInRange(
final int min,
final int max,
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
if (min > max) throw new IllegalArgumentException("min must be less than max");
if (responseCode() >= min && responseCode() <= max) {
throw errorSupplier.apply(this);
}
return this;
}

public Response ensureResponseCodeNotInRange(final int min, final int max)
throws HttpResponseException {
return ensureResponseCodeNotInRange(min, max, HttpResponseException::new);
}

/**
* Throw exception if response code is a 4xx client error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 4xx
*/
public Response throwIfClientError(
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
return throwIfResponseCodeInRange(400, 499, errorSupplier);
}

/**
* Throw exception if response code is a 4xx client error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 4xx
*/
public Response throwIfClientError()
throws HttpResponseException {
return throwIfClientError(HttpResponseException::new);
}

/**
* Throw exception if response code is a 5xx server error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 5xx
*/
public Response throwIfServerError(
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
return throwIfResponseCodeInRange(500, 599, errorSupplier);
}

/**
* Throw exception if response code is a 5xx server error
* @return this {@code Response}
* @throws HttpResponseException if the response code is 5xx
*/
public Response throwIfServerError()
throws HttpResponseException {
return throwIfServerError(HttpResponseException::new);
}

public Response throwIfResponseCode(
final int errorCode,
final Function<Response, HttpResponseException> errorSupplier
)
throws HttpResponseException {
if (responseCode() == errorCode) {
throw errorSupplier.apply(this);
}
return this;
}

public Response throwIfResponseCode(final int errorCode) throws HttpResponseException {
return throwIfResponseCode(errorCode, HttpResponseException::new);
}

public Response throwIfResponseCodeInRange(final int min,
final int max,
final Function<Response,
HttpResponseException> errorSupplier)
throws HttpResponseException {
return ensureResponseCodeNotInRange(min, max, errorSupplier);
}

public Response throwIfResponseCodeInRange(final int min,
final int max) throws HttpResponseException {
return throwIfResponseCodeInRange(min, max, HttpResponseException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.schabi.newpipe.extractor.exceptions;

import org.schabi.newpipe.extractor.downloader.Response;

public class HttpResponseException extends ExtractionException {
public HttpResponseException(final Response response) {
this("Error in HTTP Response for " + response.latestUrl() + "\n\t"
+ response.responseCode() + " - " + response.responseMessage());
}

public HttpResponseException(final String message) {
super(message);
}
}
Loading