Skip to content

Commit dd3ff72

Browse files
author
Stefan Bley
committed
[crawler-jsoup] configurable timeout; fixes #29
1 parent dca99b3 commit dd3ff72

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

crawler-api/src/main/java/de/saxsys/projectiler/crawler/Settings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.saxsys.projectiler.crawler;
22

3+
import java.util.concurrent.TimeUnit;
4+
35
/**
46
* Projectile settings.
57
*
@@ -15,4 +17,9 @@ public String getProjectileUrl() {
1517
public String getTimeFormat() {
1618
return "HH:mm";
1719
}
20+
21+
/** Connection timeout in millis */
22+
public int getTimeout() {
23+
return (int) TimeUnit.SECONDS.toMillis(10);
24+
}
1825
}

crawler-jsoup/src/main/java/de/saxsys/projectiler/crawler/jsoup/JSoupCrawler.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.logging.Logger;
1111

12+
import org.jsoup.Connection;
1213
import org.jsoup.Connection.Method;
1314
import org.jsoup.Connection.Response;
1415
import org.jsoup.Jsoup;
@@ -98,10 +99,9 @@ public void clock(final Credentials credentials, final String projectName, final
9899
* if the credentials are wrong
99100
*/
100101
private Document login(final Credentials cred) throws IOException, InvalidCredentialsException {
101-
Response response = Jsoup.connect(settings.getProjectileUrl()).method(Method.POST)
102-
.data("login", cred.getUsername()).data("password", cred.getPassword())
103-
.data("jsenabled", "0").data("external.loginOK.x", "8")
104-
.data("external.loginOK.y", "8").execute();
102+
Response response = jsoupConnection().data("login", cred.getUsername())
103+
.data("password", cred.getPassword()).data("jsenabled", "0")
104+
.data("external.loginOK.x", "8").data("external.loginOK.y", "8").execute();
105105
Document startPage = response.parse();
106106
if (startPage.getElementsByAttributeValue("name", "password").isEmpty()) {
107107
String sessionId = response.cookie(JSESSIONID);
@@ -124,9 +124,7 @@ private Document openTimeTracker(final Document startPage) throws IOException {
124124
Document introPage = openStandardIntroPage(currentPage);
125125

126126
String today = formatToday();
127-
Response response = Jsoup
128-
.connect(settings.getProjectileUrl())
129-
.method(Method.POST)
127+
Response response = jsoupConnection()
130128
.cookies(cookies)
131129
.data("taid", taid)
132130
.data("CurrentFocusField", "0")
@@ -150,9 +148,7 @@ private Document openTimeTracker(final Document startPage) throws IOException {
150148
}
151149

152150
private Document openIntroPage(Document startPage) throws IOException {
153-
Response response = Jsoup
154-
.connect(settings.getProjectileUrl())
155-
.method(Method.POST)
151+
Response response = jsoupConnection()
156152
.cookies(cookies)
157153
.data("taid", taid)
158154
.data("CurrentFocusField", "0")
@@ -170,9 +166,7 @@ private Document openIntroPage(Document startPage) throws IOException {
170166
}
171167

172168
private Document openStandardIntroPage(final Document startPage) throws IOException {
173-
Response response = Jsoup
174-
.connect(settings.getProjectileUrl())
175-
.method(Method.POST)
169+
Response response = jsoupConnection()
176170
.cookies(cookies)
177171
.data("taid", taid)
178172
.data("CurrentFocusField", "0")
@@ -188,7 +182,7 @@ private Document openStandardIntroPage(final Document startPage) throws IOExcept
188182

189183
private List<String> readProjectNames(final Document timeTrackerPage) throws IOException,
190184
CrawlingException {
191-
List<String> projectNames = new ArrayList<>();
185+
List<String> projectNames = new ArrayList<String>();
192186
Elements options = timeTrackerPage.select("select[id$=NewWhat_0_0] option");
193187
if (options.isEmpty()) {
194188
throw new CrawlingException("No projects found.");
@@ -213,9 +207,7 @@ private void clockTime(final Date start, final Date end, final String projectNam
213207
break;
214208
}
215209
}
216-
Response response = Jsoup
217-
.connect(settings.getProjectileUrl())
218-
.method(Method.POST)
210+
Response response = jsoupConnection()
219211
.cookies(cookies)
220212
.data("taid", taid)
221213
.data("CurrentFocusField", "name")
@@ -262,8 +254,7 @@ private void clockTime(final Date start, final Date end, final String projectNam
262254
}
263255

264256
private void logout(final Document page) throws IOException {
265-
Response execute = Jsoup.connect(settings.getProjectileUrl()).method(Method.POST)
266-
.cookies(cookies).data("taid", taid)
257+
Response execute = jsoupConnection().cookies(cookies).data("taid", taid)
267258
.data(page.select("input[name$=L.BUTTON.logout]").first().attr("name") + ".x", "8")
268259
.data(page.select("input[name$=L.BUTTON.logout]").first().attr("name") + ".y", "8")
269260
.execute();
@@ -275,6 +266,15 @@ private void logout(final Document page) throws IOException {
275266
}
276267
}
277268

269+
/**
270+
* Creates a JSoup connection to Projectile with method POST and given
271+
* timeout
272+
*/
273+
private Connection jsoupConnection() {
274+
return Jsoup.connect(settings.getProjectileUrl()).timeout(settings.getTimeout())
275+
.method(Method.POST);
276+
}
277+
278278
/** Reads the transaction ID from the response and stores it to a field */
279279
private void saveTaid(final Document page) {
280280
taid = page.select("input[name=taid]").val();

0 commit comments

Comments
 (0)