Skip to content

Commit 837253c

Browse files
committed
Merge branch 'release/1.0.2'
2 parents e98f7d7 + bf1088b commit 837253c

File tree

11 files changed

+50
-13
lines changed

11 files changed

+50
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ WebMagic use slf4j with slf4j-log4j12 implementation. If you customized your slf
5050

5151
### First crawler:
5252

53-
Write a class implements PageProcessor. For example, I wrote a crawler of github repository infomation.
53+
Write a class implements PageProcessor. For example, I wrote a crawler of github repository information.
5454

5555
```java
5656
public class GithubRepoPageProcessor implements PageProcessor {
@@ -112,7 +112,7 @@ public class GithubRepo {
112112

113113
Documents: [http://webmagic.io/docs/](http://webmagic.io/docs/)
114114

115-
The architecture of webmagic (refered to [Scrapy](http://scrapy.org/))
115+
The architecture of webmagic (referred to [Scrapy](http://scrapy.org/))
116116

117117
![image](http://code4craft.github.io/images/posts/webmagic.png)
118118

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<version>2.2.1</version>
1313
</parent>
1414
<groupId>us.codecraft</groupId>
15-
<version>1.0.1</version>
15+
<version>1.0.2</version>
1616
<packaging>pom</packaging>
1717
<properties>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

webmagic-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

webmagic-core/src/main/java/us/codecraft/webmagic/Page.java

+37
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,44 @@ public class Page {
5252

5353
private String charset;
5454

55+
/**
56+
* Returns a {@link Page} with {@link #downloadSuccess} is {@code true},
57+
* and {@link #request} is specified.
58+
*
59+
* @param request the request.
60+
* @since 1.0.2
61+
*/
62+
public static Page ofSuccess(Request request) {
63+
return new Page(request, true);
64+
}
65+
66+
/**
67+
* Returns a {@link Page} with {@link #downloadSuccess} is {@code true},
68+
* and {@link #request} is specified.
69+
*
70+
* @param request the request.
71+
* @since 1.0.2
72+
*/
73+
public static Page ofFailure(Request request) {
74+
return new Page(request, false);
75+
}
76+
5577
public Page() {
5678
}
5779

80+
/**
81+
* Constructs a {@link Page} with {@link #request}
82+
* and {@link #downloadSuccess} specified.
83+
*
84+
* @param request the request.
85+
* @param downloadSuccess the download success flag.
86+
* @since 1.0.2
87+
*/
88+
private Page(Request request, boolean downloadSuccess) {
89+
this.request = request;
90+
this.downloadSuccess = downloadSuccess;
91+
}
92+
5893
/**
5994
* Returns a {@link Page} with {@link #downloadSuccess} is {@code false}.
6095
*
@@ -73,7 +108,9 @@ public static Page fail() {
73108
* @param request the {@link Request}.
74109
* @return the page.
75110
* @since 0.10.0
111+
* @deprecated Use {@link #ofFailure(Request)} instead.
76112
*/
113+
@Deprecated(since = "1.0.2", forRemoval = true)
77114
public static Page fail(Request request){
78115
Page page = new Page();
79116
page.setRequest(request);

webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ public Page download(Request request, Task task) {
7676
CloseableHttpClient httpClient = getHttpClient(task.getSite());
7777
Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(request, task) : null;
7878
HttpClientRequestContext requestContext = httpUriRequestConverter.convert(request, task.getSite(), proxy);
79-
Page page = Page.fail(request);
79+
Page page = null;
8080
try {
8181
httpResponse = httpClient.execute(requestContext.getHttpUriRequest(), requestContext.getHttpClientContext());
8282
page = handleResponse(request, request.getCharset() != null ? request.getCharset() : task.getSite().getCharset(), httpResponse, task);
8383
onSuccess(page, task);
8484
return page;
8585
} catch (IOException e) {
86+
page = Page.ofFailure(request);
8687
onError(page, task, e);
8788
return page;
8889
} finally {
@@ -105,7 +106,7 @@ protected Page handleResponse(Request request, String charset, HttpResponse http
105106
HttpEntity entity = httpResponse.getEntity();
106107
byte[] bytes = entity != null ? IOUtils.toByteArray(entity.getContent()) : new byte[0];
107108
String contentType = entity != null && entity.getContentType() != null ? entity.getContentType().getValue() : null;
108-
Page page = new Page();
109+
Page page = Page.ofSuccess(request);
109110
page.setBytes(bytes);
110111
if (!request.isBinaryContent()) {
111112
if (charset == null) {
@@ -117,7 +118,6 @@ protected Page handleResponse(Request request, String charset, HttpResponse http
117118
page.setUrl(new PlainText(request.getUrl()));
118119
page.setRequest(request);
119120
page.setStatusCode(httpResponse.getStatusLine().getStatusCode());
120-
page.setDownloadSuccess(true);
121121
if (responseHeader) {
122122
page.setHeaders(HttpClientUtils.convertHeaders(httpResponse.getAllHeaders()));
123123
}

webmagic-coverage/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>us.codecraft</groupId>
1212
<artifactId>webmagic</artifactId>
13-
<version>1.0.1</version>
13+
<version>1.0.2</version>
1414
</parent>
1515

1616
<artifactId>webmagic-coverage</artifactId>

webmagic-extension/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

webmagic-samples/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

webmagic-saxon/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

webmagic-scripts/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

webmagic-selenium/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>us.codecraft</groupId>
1010
<artifactId>webmagic</artifactId>
11-
<version>1.0.1</version>
11+
<version>1.0.2</version>
1212
</parent>
1313
<modelVersion>4.0.0</modelVersion>
1414

0 commit comments

Comments
 (0)