Add static methods to construct Page.

pull/1183/head
Joe Zhou 2 months ago
parent 7e500d7b95
commit 0a9fe8d3e0

@ -52,9 +52,44 @@ public class Page {
private String charset; private String charset;
/**
* Returns a {@link Page} with {@link #downloadSuccess} is {@code true},
* and {@link #request} is specified.
*
* @param request the request.
* @since 1.0.2
*/
public static Page ofSuccess(Request request) {
return new Page(request, true);
}
/**
* Returns a {@link Page} with {@link #downloadSuccess} is {@code true},
* and {@link #request} is specified.
*
* @param request the request.
* @since 1.0.2
*/
public static Page ofFailure(Request request) {
return new Page(request, false);
}
public Page() { public Page() {
} }
/**
* Constructs a {@link Page} with {@link #request}
* and {@link #downloadSuccess} specified.
*
* @param request the request.
* @param downloadSuccess the download success flag.
* @since 1.0.2
*/
private Page(Request request, boolean downloadSuccess) {
this.request = request;
this.downloadSuccess = downloadSuccess;
}
/** /**
* Returns a {@link Page} with {@link #downloadSuccess} is {@code false}. * Returns a {@link Page} with {@link #downloadSuccess} is {@code false}.
* *
@ -73,7 +108,9 @@ public class Page {
* @param request the {@link Request}. * @param request the {@link Request}.
* @return the page. * @return the page.
* @since 0.10.0 * @since 0.10.0
* @deprecated Use {@link #ofFailure(Request)} instead.
*/ */
@Deprecated(since = "1.0.2", forRemoval = true)
public static Page fail(Request request){ public static Page fail(Request request){
Page page = new Page(); Page page = new Page();
page.setRequest(request); page.setRequest(request);

@ -76,7 +76,7 @@ public class HttpClientDownloader extends AbstractDownloader {
CloseableHttpClient httpClient = getHttpClient(task.getSite()); CloseableHttpClient httpClient = getHttpClient(task.getSite());
Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(request, task) : null; Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(request, task) : null;
HttpClientRequestContext requestContext = httpUriRequestConverter.convert(request, task.getSite(), proxy); HttpClientRequestContext requestContext = httpUriRequestConverter.convert(request, task.getSite(), proxy);
Page page = Page.fail(request); Page page = Page.ofFailure(request);
try { try {
httpResponse = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext()); httpResponse = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext());
page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), httpResponse, task); page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), httpResponse, task);
@ -105,7 +105,7 @@ public class HttpClientDownloader extends AbstractDownloader {
HttpEntity entity = httpResponse.getEntity(); HttpEntity entity = httpResponse.getEntity();
byte[] bytes = entity != null ? IOUtils.toByteArray(entity.getContent()) : new byte[0]; byte[] bytes = entity != null ? IOUtils.toByteArray(entity.getContent()) : new byte[0];
String contentType = entity != null && entity.getContentType() != null ? entity.getContentType().getValue() : null; String contentType = entity != null && entity.getContentType() != null ? entity.getContentType().getValue() : null;
Page page = new Page(); Page page = Page.ofSuccess(request);
page.setBytes(bytes); page.setBytes(bytes);
if (!request.isBinaryContent()) { if (!request.isBinaryContent()) {
if (charset == null) { if (charset == null) {
@ -117,7 +117,6 @@ public class HttpClientDownloader extends AbstractDownloader {
page.setUrl(new PlainText(request.getUrl())); page.setUrl(new PlainText(request.getUrl()));
page.setRequest(request); page.setRequest(request);
page.setStatusCode(httpResponse.getStatusLine().getStatusCode()); page.setStatusCode(httpResponse.getStatusLine().getStatusCode());
page.setDownloadSuccess(true);
if (responseHeader) { if (responseHeader) {
page.setHeaders(HttpClientUtils.convertHeaders(httpResponse.getAllHeaders())); page.setHeaders(HttpClientUtils.convertHeaders(httpResponse.getAllHeaders()));
} }

Loading…
Cancel
Save