-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebpage.java
More file actions
134 lines (115 loc) · 4 KB
/
Copy pathWebpage.java
File metadata and controls
134 lines (115 loc) · 4 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package webcrawler;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
public class Webpage {
private String link;
private URL url;
private HttpURLConnection connection;
private String content;
public Webpage(String link) {
this.link = link;
try {
this.url = new URL(link);
if (link.startsWith("https://")) {
this.connection = (HttpsURLConnection) url.openConnection();
} else if (link.startsWith("http://")) {
this.connection = (HttpURLConnection) url.openConnection();
}
this.connection.setConnectTimeout(2000);
try {
if (this.connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
loadContent();
}
} catch (SocketTimeoutException ste) {
System.err.println(ste + " " + link);
} catch (SocketException | ProtocolException e) {
System.err.println(e + " " + link);
} catch (Exception | Error e) {
System.err.println(e + " " + link);
}
} catch (SocketTimeoutException ste) {
System.err.println("Timed out on " + link);
} catch (SocketException | ProtocolException se) {
} catch (Exception e) {
System.err.println(e.getMessage());
} catch (Error s) {
System.err.println(s.getMessage());
}
}
private void loadContent() {
StringBuilder output = new StringBuilder();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(getConnection().getInputStream()))) {
String input;
while ((input = br.readLine()) != null) {
output = output.append(input);
}
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
setContent(output.toString());
}
public ArrayList<String> getLinks() {
if (getContent() == null) {
return new ArrayList<>();
}
Pattern pattern = Pattern.compile("href=\"([\\w\\.:/]++)\"");
Matcher matcher = pattern.matcher(this.getContent());
ArrayList<String> pages = new ArrayList<>();
while (matcher.find()) {
String link = matcher.group(1);
if (link.startsWith("https://") || link.startsWith("http://")) {
if (!link.equals(this.link)) {
pages.add(link);
}
} else {
if (link.startsWith("/")) {
link = link.substring(1);
}
String urlString = this.getUrl().toString();
if (!urlString.endsWith("/")) {
urlString = urlString + "/";
}
pages.add(urlString + link);
}
}
return pages;
}
@Override
public String toString() {
return getUrl().toExternalForm();
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public HttpURLConnection getConnection() {
return connection;
}
public void setConnection(HttpURLConnection connection) {
this.connection = connection;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}