Repository navigation
Expand file tree
/
Copy pathCheckUrl.java
More file actions
79 lines (74 loc) · 2.79 KB
/
Copy pathCheckUrl.java
File metadata and controls
79 lines (74 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.jsoup.Jsoup;
public class CheckUrl implements Runnable {
String url;
Link temp;
String tempUrl;
List < Link > links;
List < Integer > pos;
URL fileUrl;
int responseCode;
public CheckUrl(List < Link > links, List < Integer > pos, String url) {
this.links = links;
this.pos = pos; // integer list of positions of links in the links list
this.url = url; // the domain from which the links were fetched
}
@Override
public void run() {
int j = 0;
while (j < pos.size()) {
try {
/**
* Get the link and if it is a relative link add the domain to it.
*/
tempUrl = links.get(pos.get(j)).url;
if (!tempUrl.startsWith("http")) {
if (!tempUrl.startsWith("/")) {
tempUrl = "/" + tempUrl;
}
tempUrl = url + tempUrl;
}
/**
* Attempt to connect to the link and get document.
* If successful set replace the link object in the list with
* an exact copy but with the valid field set to true.
*/
Jsoup.connect(tempUrl).get();
temp = new Link();
temp = links.get(pos.get(j));
temp.valid = true;
links.set(pos.get(j), temp);
} catch (Exception e) {
/**
* Attempt to get a response code.
* If successful set replace the link object in the list with
* an exact copy but with the valid field and the file field set to true.
* Otherwise, the object will have both fields set to false.
*/
try {
fileUrl = new URL(tempUrl);
HttpURLConnection huc = (HttpURLConnection) fileUrl.openConnection();
responseCode = huc.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
temp = new Link();
temp = links.get(pos.get(j));
temp.valid = true;
temp.file = true;
} else {
temp = new Link();
temp = links.get(pos.get(j));
temp.valid = false;
}
} catch (Exception a) {
temp = new Link();
temp = links.get(pos.get(j));
temp.valid = false;
}
links.set(pos.get(j), temp);
}
j++;
}
}
}