@@ -41,6 +41,8 @@ public class API {
41
41
private boolean useMimeTypes ;
42
42
private MimeTypeHelper helper ;
43
43
44
+ private static int RETRY_WAIT_TIME = 2000 ;
45
+
44
46
static Gson gson = new GsonBuilder ().setDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'" ).create ();
45
47
46
48
public API (String baseUrl ) {
@@ -69,7 +71,7 @@ public boolean authenticate(String username, String password) {
69
71
ResponseData result = navigate (ResponseData .class , getAuthLink ());
70
72
71
73
if (result == null || result .getItems () != null ) {
72
- System .out .println ("Could not authenticate, please check credentials" );
74
+ System .out .println ("Could not authenticate, please check credentials and service status from http://status.nfleet.fi " );
73
75
return false ;
74
76
}
75
77
@@ -189,7 +191,6 @@ public void setBaseUrl(String baseUrl) {
189
191
this .baseUrl = baseUrl ;
190
192
}
191
193
192
-
193
194
private <T extends BaseData > T sendRequest (Link l , Class <T > tClass , Object object ) throws IOException {
194
195
URL serverAddress ;
195
196
BufferedReader br ;
@@ -216,7 +217,6 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
216
217
if (!useMimeTypes )
217
218
connection .setRequestProperty ("Accept" , "application/json" );
218
219
219
-
220
220
if (doOutput && useMimeTypes ) {
221
221
//this handles the case if the link is self made and the type field has not been set.
222
222
if (type == null || type .equals ("" )) {
@@ -243,8 +243,7 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
243
243
OutputStreamWriter osw = new OutputStreamWriter (connection .getOutputStream ());
244
244
osw .write (json );
245
245
osw .flush ();
246
- osw .close ();
247
-
246
+ osw .close ();
248
247
}
249
248
250
249
connection .connect ();
@@ -259,16 +258,16 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
259
258
}
260
259
261
260
if (connection .getResponseCode () == HttpURLConnection .HTTP_UNAUTHORIZED ) {
262
- System .out .println ("Authentication expired " + connection .getResponseMessage ());
261
+ System .out .println ("Authentication expired " + connection .getResponseMessage () + " trying to reauthenticate" );
263
262
if (retry && this .tokenData != null ) {
264
263
this .tokenData = null ;
265
264
retry = false ;
266
265
if ( authenticate () ) {
267
- System .out .println ("Authenticated again" );
266
+ System .out .println ("Reauthentication success, will continue with " + l . getMethod () + " request on " + l . getRel () );
268
267
return sendRequest (l , tClass , object );
269
268
}
270
269
}
271
- else throw new IOException ("Could not authenticate " );
270
+ else throw new IOException ("Tried to reauthenticate but failed, please check the credentials and status of NFleet-API " );
272
271
}
273
272
274
273
if (connection .getResponseCode () == HttpURLConnection .HTTP_NOT_MODIFIED ) {
@@ -287,8 +286,26 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
287
286
throw (NFleetRequestException ) gson .fromJson (errorString , NFleetRequestException .class );
288
287
}
289
288
else if (connection .getResponseCode () >= HttpURLConnection .HTTP_INTERNAL_ERROR ) {
290
- String errorString = readErrorStreamAndCloseConnection (connection );
291
- throw new IOException (errorString );
289
+ if (retry ) {
290
+ System .out .println ("Request caused internal server error, waiting " + RETRY_WAIT_TIME + " ms and trying again." );
291
+ return waitAndRetry (connection , l , tClass , object );
292
+ } else {
293
+ System .
out .
println (
"Requst caused internal server error, please contact [email protected] " );
294
+ String errorString = readErrorStreamAndCloseConnection (connection );
295
+ throw new IOException (errorString );
296
+ }
297
+ }
298
+
299
+ if (connection .getResponseCode () >= HttpURLConnection .HTTP_BAD_GATEWAY ) {
300
+ if (retry ) {
301
+ System .out .println ("Could not connect to NFleet-API, waiting " + RETRY_WAIT_TIME + " ms and trying again." );
302
+ return waitAndRetry (connection , l , tClass , object );
303
+ } else {
304
+ System .out .println ("Could not connect to NFleet-API, please check service status from http://status.nfleet.fi and try again later." );
305
+ String errorString = readErrorStreamAndCloseConnection (connection );
306
+ throw new IOException (errorString );
307
+ }
308
+
292
309
}
293
310
294
311
result = readDataFromConnection (connection );
@@ -338,7 +355,7 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
338
355
}
339
356
connection .connect ();
340
357
341
- if (connection .getResponseCode () == HttpURLConnection .HTTP_SEE_OTHER || connection .getResponseCode () == HttpURLConnection .HTTP_CREATED ) {
358
+ if (connection .getResponseCode () == HttpURLConnection .HTTP_SEE_OTHER || connection .getResponseCode () == HttpURLConnection .HTTP_CREATED ) {
342
359
Link location = parseLocationLinkFromString (connection .getHeaderField ("Location" ));
343
360
Link l = new Link ("self" , "/tokens" , "GET" ,"" , true );
344
361
ArrayList <Link > links = new ArrayList <Link >();
@@ -356,12 +373,11 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
356
373
retry = false ;
357
374
this .tokenData = null ;
358
375
if ( authenticate () ) {
359
- System .out .println ("Authenticated again" );
376
+ System .out .println ("Reauthentication success, will continue with " + verb + " request on " + url );
360
377
return sendRequestWithAddedHeaders (verb , url , tClass , object , headers );
361
378
}
362
- System .out .println ("Could not authenticate" );
363
379
}
364
- else throw new IOException ("Could not authenticate " );
380
+ else throw new IOException ("Tried to reauthenticate but failed, please check the credentials and status of NFleet-API " );
365
381
}
366
382
367
383
if (connection .getResponseCode () >= HttpURLConnection .HTTP_BAD_REQUEST && connection .getResponseCode () < HttpURLConnection .HTTP_INTERNAL_ERROR ) {
@@ -372,6 +388,19 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
372
388
throw (NFleetRequestException ) gson .fromJson (errorString , NFleetRequestException .class );
373
389
}
374
390
else if (connection .getResponseCode () >= HttpURLConnection .HTTP_INTERNAL_ERROR ) {
391
+ if (retry ) {
392
+ System .out .println ("Server responded with internal server error, trying again in " + RETRY_WAIT_TIME + " msec." );
393
+ try {
394
+ retry = false ;
395
+ Thread .sleep (RETRY_WAIT_TIME );
396
+ return sendRequestWithAddedHeaders (verb , url , tClass , object , headers );
397
+ } catch (InterruptedException e ) {
398
+
399
+ }
400
+ } else {
401
+ System .
out .
println (
"Server responded with internal server error, please contact [email protected] " );
402
+ }
403
+
375
404
String errorString = readErrorStreamAndCloseConnection (connection );
376
405
throw new IOException (errorString );
377
406
}
@@ -410,12 +439,7 @@ private String method(Verb verb) {
410
439
}
411
440
412
441
private Link parseLocationLinkFromString (String s ) {
413
- //Azure emulator hack
414
- if (s .contains ("82" ))
415
- s = s .replace ("82" , "81" );
416
- if (s .contains ("/tokens" ))
417
- return new Link ("location" , s , "GET" ,"" , true );
418
- else
442
+ if (!s .contains ("/tokens" ))
419
443
s = s .substring (s .lastIndexOf ("/users" ));
420
444
return new Link ("location" , s , "GET" , "" , true );
421
445
}
@@ -531,11 +555,11 @@ private String readErrorStreamAndCloseConnection(HttpURLConnection connection) {
531
555
sb .append (s );
532
556
}
533
557
} catch (IOException e ) {
534
- e .printStackTrace ();
558
+ System .out .println ("Could not read error stream from the connection." );
559
+ } finally {
560
+ connection .disconnect ();
535
561
}
536
- String body = sb .toString ();
537
- connection .disconnect ();
538
- return body ;
562
+ return sb .toString ();
539
563
}
540
564
541
565
private String readDataFromConnection (HttpURLConnection connection ) {
@@ -556,11 +580,24 @@ private String readDataFromConnection(HttpURLConnection connection) {
556
580
sb .insert (sb .lastIndexOf ("}" ), ",\" VersionNumber\" :" + eTag + "" );
557
581
}
558
582
} catch (IOException e ) {
559
- e . printStackTrace ( );
560
- }
583
+ System . out . println ( "Could not read data from connection" );
584
+ }
561
585
return sb .toString ();
562
586
}
563
587
588
+ private <T extends BaseData > T waitAndRetry (HttpURLConnection connection , Link l , Class <T > tClass , Object object ) {
589
+ try {
590
+ retry = false ;
591
+ Thread .sleep (RETRY_WAIT_TIME );
592
+ return sendRequest (l , tClass , object );
593
+ } catch (InterruptedException e ) {
594
+ return null ;
595
+ } catch (IOException e ) {
596
+ return null ;
597
+ }
598
+ }
599
+
600
+
564
601
public TokenData getTokenData () {
565
602
return this .tokenData ;
566
603
}
0 commit comments