Skip to content

Commit 4237618

Browse files
author
Jarkko Laitinen
committed
Merge branch 'development'
2 parents 0202d25 + 4e412e6 commit 4237618

7 files changed

+450
-232
lines changed

fi/cosky/sdk/API.java

+63-26
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class API {
4141
private boolean useMimeTypes;
4242
private MimeTypeHelper helper;
4343

44+
private static int RETRY_WAIT_TIME = 2000;
45+
4446
static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'").create();
4547

4648
public API(String baseUrl) {
@@ -69,7 +71,7 @@ public boolean authenticate(String username, String password) {
6971
ResponseData result = navigate(ResponseData.class, getAuthLink());
7072

7173
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");
7375
return false;
7476
}
7577

@@ -189,7 +191,6 @@ public void setBaseUrl(String baseUrl) {
189191
this.baseUrl = baseUrl;
190192
}
191193

192-
193194
private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object object) throws IOException {
194195
URL serverAddress;
195196
BufferedReader br;
@@ -216,7 +217,6 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
216217
if (!useMimeTypes)
217218
connection.setRequestProperty("Accept", "application/json");
218219

219-
220220
if (doOutput && useMimeTypes) {
221221
//this handles the case if the link is self made and the type field has not been set.
222222
if (type == null || type.equals("")) {
@@ -243,8 +243,7 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
243243
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
244244
osw.write(json);
245245
osw.flush();
246-
osw.close();
247-
246+
osw.close();
248247
}
249248

250249
connection.connect();
@@ -259,16 +258,16 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
259258
}
260259

261260
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");
263262
if (retry && this.tokenData != null) {
264263
this.tokenData = null;
265264
retry = false;
266265
if( authenticate() ) {
267-
System.out.println("Authenticated again");
266+
System.out.println("Reauthentication success, will continue with " + l.getMethod() + " request on " + l.getRel());
268267
return sendRequest(l, tClass, object);
269268
}
270269
}
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");
272271
}
273272

274273
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
@@ -287,8 +286,26 @@ private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object objec
287286
throw (NFleetRequestException) gson.fromJson(errorString, NFleetRequestException.class);
288287
}
289288
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+
292309
}
293310

294311
result = readDataFromConnection(connection);
@@ -338,7 +355,7 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
338355
}
339356
connection.connect();
340357

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) {
342359
Link location = parseLocationLinkFromString(connection.getHeaderField("Location"));
343360
Link l = new Link("self", "/tokens", "GET","", true);
344361
ArrayList<Link> links = new ArrayList<Link>();
@@ -356,12 +373,11 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
356373
retry = false;
357374
this.tokenData = null;
358375
if( authenticate() ) {
359-
System.out.println("Authenticated again");
376+
System.out.println("Reauthentication success, will continue with " + verb + " request on " + url);
360377
return sendRequestWithAddedHeaders(verb, url, tClass, object, headers);
361378
}
362-
System.out.println("Could not authenticate");
363379
}
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");
365381
}
366382

367383
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
372388
throw (NFleetRequestException) gson.fromJson(errorString, NFleetRequestException.class);
373389
}
374390
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+
375404
String errorString = readErrorStreamAndCloseConnection(connection);
376405
throw new IOException(errorString);
377406
}
@@ -410,12 +439,7 @@ private String method(Verb verb) {
410439
}
411440

412441
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"))
419443
s = s.substring(s.lastIndexOf("/users"));
420444
return new Link("location", s, "GET", "", true);
421445
}
@@ -531,11 +555,11 @@ private String readErrorStreamAndCloseConnection(HttpURLConnection connection) {
531555
sb.append(s);
532556
}
533557
} catch (IOException e) {
534-
e.printStackTrace();
558+
System.out.println("Could not read error stream from the connection.");
559+
} finally {
560+
connection.disconnect();
535561
}
536-
String body = sb.toString();
537-
connection.disconnect();
538-
return body;
562+
return sb.toString();
539563
}
540564

541565
private String readDataFromConnection(HttpURLConnection connection) {
@@ -556,11 +580,24 @@ private String readDataFromConnection(HttpURLConnection connection) {
556580
sb.insert(sb.lastIndexOf("}"), ",\"VersionNumber\":" + eTag + "");
557581
}
558582
} catch (IOException e) {
559-
e.printStackTrace();
560-
}
583+
System.out.println("Could not read data from connection");
584+
}
561585
return sb.toString();
562586
}
563587

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+
564601
public TokenData getTokenData() {
565602
return this.tokenData;
566603
}
+55-34
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
1-
package fi.cosky.sdk;
2-
3-
/*
4-
* This file is subject to the terms and conditions defined in
5-
* file 'LICENSE.txt', which is part of this source code package.
6-
*/
7-
8-
public class RoutingProblemSettingsData extends BaseData {
9-
public static final String MimeType = "application/vnd.jyu.nfleet.problemsettings";
10-
public static final double MimeVersion = 2.0;
11-
12-
private int VersionNumber;
13-
private double DefaultVehicleSpeedFactor;
14-
private String DefaultVehicleSpeedProfile;
15-
16-
public int getVersionNumber() {
17-
return VersionNumber;
18-
}
19-
public void setVersionNumber(int versionNumber) {
20-
VersionNumber = versionNumber;
21-
}
22-
public double getDefaultVehicleSpeedFactor() {
23-
return DefaultVehicleSpeedFactor;
24-
}
25-
public void setDefaultVehicleSpeedFactor(double defaultVehicleSpeedFactor) {
26-
DefaultVehicleSpeedFactor = defaultVehicleSpeedFactor;
27-
}
28-
public String getDefaultVehicleSpeedProfile() {
29-
return DefaultVehicleSpeedProfile;
30-
}
31-
public void setDefaultVehicleSpeedProfile(String defaultVehicleSpeedProfile) {
32-
DefaultVehicleSpeedProfile = defaultVehicleSpeedProfile;
33-
}
34-
}
1+
package fi.cosky.sdk;
2+
3+
/*
4+
* This file is subject to the terms and conditions defined in
5+
* file 'LICENSE.txt', which is part of this source code package.
6+
*/
7+
8+
public class RoutingProblemSettingsData extends BaseData {
9+
public static final String MimeType = "application/vnd.jyu.nfleet.problemsettings";
10+
public static final double MimeVersion = 2.1;
11+
12+
private int VersionNumber;
13+
private double DefaultVehicleSpeedFactor;
14+
private String DefaultVehicleSpeedProfile;
15+
private double InsertionAggressiveness;
16+
private String AlgorithmTree;
17+
private String DateTimeFormatString;
18+
19+
public int getVersionNumber() {
20+
return VersionNumber;
21+
}
22+
public void setVersionNumber(int versionNumber) {
23+
VersionNumber = versionNumber;
24+
}
25+
public double getDefaultVehicleSpeedFactor() {
26+
return DefaultVehicleSpeedFactor;
27+
}
28+
public void setDefaultVehicleSpeedFactor(double defaultVehicleSpeedFactor) {
29+
DefaultVehicleSpeedFactor = defaultVehicleSpeedFactor;
30+
}
31+
public String getDefaultVehicleSpeedProfile() {
32+
return DefaultVehicleSpeedProfile;
33+
}
34+
public void setDefaultVehicleSpeedProfile(String defaultVehicleSpeedProfile) {
35+
DefaultVehicleSpeedProfile = defaultVehicleSpeedProfile;
36+
}
37+
public double getInsertionAggressiveness() {
38+
return InsertionAggressiveness;
39+
}
40+
public void setInsertionAggressiveness(double insertionAggressiveness) {
41+
InsertionAggressiveness = insertionAggressiveness;
42+
}
43+
public String getAlgorithmTree() {
44+
return AlgorithmTree;
45+
}
46+
public void setAlgorithmTree(String algorithmTree) {
47+
AlgorithmTree = algorithmTree;
48+
}
49+
public String getDateTimeFormatString() {
50+
return DateTimeFormatString;
51+
}
52+
public void setDateTimeFormatString(String dateTimeFormatString) {
53+
DateTimeFormatString = dateTimeFormatString;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,70 @@
1-
package fi.cosky.sdk;
2-
3-
/*
4-
* This file is subject to the terms and conditions defined in
5-
* file 'LICENSE.txt', which is part of this source code package.
6-
*/
7-
8-
public class RoutingProblemSettingsUpdateRequest {
9-
public static final String MimeType = "application/vnd.jyu.nfleet.problemsettings";
10-
public static final double MimeVersion = 2.0;
11-
12-
private double DefaultVehicleSpeedFactor;
13-
private SpeedProfile DefaultVehicleSpeedProfile;
14-
15-
public double getDefaultVehicleSpeedFactor() {
16-
return DefaultVehicleSpeedFactor;
17-
}
18-
public void setDefaultVehicleSpeedFactor(double defaultVehicleSpeedFactor) {
19-
DefaultVehicleSpeedFactor = defaultVehicleSpeedFactor;
20-
}
21-
public SpeedProfile getDefaultVehicleSpeedProfile() {
22-
return DefaultVehicleSpeedProfile;
23-
}
24-
public void setDefaultVehicleSpeedProfile(SpeedProfile defaultVehicleSpeedProfile) {
25-
DefaultVehicleSpeedProfile = defaultVehicleSpeedProfile;
26-
}
27-
28-
}
29-
1+
package fi.cosky.sdk;
2+
3+
/*
4+
* This file is subject to the terms and conditions defined in
5+
* file 'LICENSE.txt', which is part of this source code package.
6+
*/
7+
8+
public class RoutingProblemSettingsUpdateRequest {
9+
public static final String MimeType = RoutingProblemSettingsData.MimeType;
10+
public static final double MimeVersion = RoutingProblemSettingsData.MimeVersion;
11+
12+
private double DefaultVehicleSpeedFactor;
13+
private SpeedProfile DefaultVehicleSpeedProfile;
14+
private double InsertionAggressiveness;
15+
private String AlgorithmTree;
16+
private String DateTimeFormatString;
17+
18+
/**
19+
* Default constructor with good values for settings. These
20+
* are also the ones that NFleet uses when settings
21+
* are not specified
22+
*/
23+
public RoutingProblemSettingsUpdateRequest() {
24+
this.DefaultVehicleSpeedFactor = 0.9;
25+
this.DefaultVehicleSpeedProfile = SpeedProfile.Max100Kmh;
26+
this.InsertionAggressiveness = 1;
27+
}
28+
29+
public double getDefaultVehicleSpeedFactor() {
30+
return DefaultVehicleSpeedFactor;
31+
}
32+
33+
public void setDefaultVehicleSpeedFactor(double defaultVehicleSpeedFactor) {
34+
DefaultVehicleSpeedFactor = defaultVehicleSpeedFactor;
35+
}
36+
37+
public SpeedProfile getDefaultVehicleSpeedProfile() {
38+
return DefaultVehicleSpeedProfile;
39+
}
40+
41+
public void setDefaultVehicleSpeedProfile(SpeedProfile defaultVehicleSpeedProfile) {
42+
DefaultVehicleSpeedProfile = defaultVehicleSpeedProfile;
43+
}
44+
45+
public double getInsertionAggressiveness() {
46+
return InsertionAggressiveness;
47+
}
48+
49+
public void setInsertionAggressiveness(double insertionAggressiveness) {
50+
InsertionAggressiveness = insertionAggressiveness;
51+
}
52+
53+
public String getAlgorithmTree() {
54+
return AlgorithmTree;
55+
}
56+
57+
public void setAlgorithmTree(String algorithmTree) {
58+
AlgorithmTree = algorithmTree;
59+
}
60+
61+
public String getDateTimeFormatString() {
62+
return DateTimeFormatString;
63+
}
64+
65+
public void setDateTimeFormatString(String dateTimeFormatString) {
66+
DateTimeFormatString = dateTimeFormatString;
67+
}
68+
69+
}
70+

0 commit comments

Comments
 (0)