Skip to content

Commit 357fe82

Browse files
koebiTheGreatRefrigerator
authored andcommitted
feat: enhance RoadPropertySpeedParser
- add handling of unknown key - throw StatusCodeException on validation error
1 parent 17114af commit 357fe82

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/userspeed/RoadPropertySpeedParser.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
11
package org.heigit.ors.routing.graphhopper.extensions.userspeed;
22

3+
import org.heigit.ors.exceptions.*;
4+
import org.heigit.ors.routing.RoutingErrorCodes;
35
import org.json.JSONObject;
46

7+
import java.util.Arrays;
8+
import java.util.List;
9+
510
public class RoadPropertySpeedParser {
6-
public RoadPropertySpeedMap parse(String input) {
11+
public RoadPropertySpeedMap parse(String input) throws StatusCodeException {
712
return parse(new JSONObject(input));
813
}
914

10-
public RoadPropertySpeedMap parse(org.json.simple.JSONObject input) {
15+
public RoadPropertySpeedMap parse(org.json.simple.JSONObject input) throws StatusCodeException {
1116
return parse(input.toJSONString());
1217
}
1318

14-
public RoadPropertySpeedMap parse(JSONObject json) {
19+
public RoadPropertySpeedMap parse(JSONObject json) throws StatusCodeException {
1520
RoadPropertySpeedMap rsm = new RoadPropertySpeedMap();
1621

22+
// test that only valid keys are present:
23+
List<String> validKeys = Arrays.asList("unit", "roadSpeeds", "surfaceSpeeds");
24+
for (String key : json.keySet()) {
25+
if (!validKeys.contains(key)) {
26+
throw new UnknownParameterException(RoutingErrorCodes.UNKNOWN_PARAMETER, key);
27+
}
28+
}
29+
1730
// parse units
1831
String unit = "kmh";
1932
if (json.has("unit")) {
2033
unit = json.getString("unit");
2134
}
22-
double unitFactor = 1.0;
23-
if (unit.equals("mph")) {
35+
36+
double unitFactor;
37+
if (unit.equals("kmh")) {
38+
unitFactor = 1.0;
39+
} else if (unit.equals("mph")) {
2440
unitFactor = 1.60934;
41+
} else {
42+
throw new ParameterValueException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "unit", unit);
2543
}
2644

2745
// parse road speeds
2846
JSONObject roadSpeeds;
2947
if (json.has("roadSpeeds")) {
3048
roadSpeeds = json.getJSONObject("roadSpeeds");
3149
for (String roadType : roadSpeeds.keySet()) {
32-
rsm.addRoadPropertySpeed(roadType, roadSpeeds.getDouble(roadType)*unitFactor);
50+
try {
51+
rsm.addRoadPropertySpeed(roadType, roadSpeeds.getDouble(roadType) * unitFactor);
52+
} catch (IllegalArgumentException e) {
53+
if (e.getMessage().contains("must be")) {
54+
throw new ParameterOutOfRangeException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "speed");
55+
} else {
56+
throw new UnknownParameterException(RoutingErrorCodes.UNKNOWN_PARAMETER, e.getMessage().substring(23));
57+
}
58+
}
3359
}
3460
}
3561

@@ -38,7 +64,15 @@ public RoadPropertySpeedMap parse(JSONObject json) {
3864
if (json.has("surfaceSpeeds")) {
3965
surfaceSpeeds = json.getJSONObject("surfaceSpeeds");
4066
for (String surfaceType : surfaceSpeeds.keySet()) {
41-
rsm.addRoadPropertySpeed(surfaceType, surfaceSpeeds.getDouble(surfaceType)*unitFactor);
67+
try {
68+
rsm.addRoadPropertySpeed(surfaceType, surfaceSpeeds.getDouble(surfaceType) * unitFactor);
69+
} catch (IllegalArgumentException e) {
70+
if (e.getMessage().contains("must be")) {
71+
throw new ParameterOutOfRangeException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "speed");
72+
} else {
73+
throw new UnknownParameterException(RoutingErrorCodes.UNKNOWN_PARAMETER, e.getMessage().substring(23));
74+
}
75+
}
4276
}
4377
}
4478

0 commit comments

Comments
 (0)