99import java .util .Map ;
1010import java .util .logging .Logger ;
1111
12+ import org .jsoup .Connection ;
1213import org .jsoup .Connection .Method ;
1314import org .jsoup .Connection .Response ;
1415import 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