Skip to content

Commit e7ccbfb

Browse files
authored
Handle invalid POST to action resources. (#54)
1 parent a643e40 commit e7ccbfb

4 files changed

Lines changed: 70 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## [0.12.2] - 2020-05-04
6+
### Changed
7+
- Invalid POST requests to action resources now generate an error status.
8+
59
## [0.12.1] - 2020-03-27
610
### Added
711
- Support OPTIONS requests to allow for CORS.
@@ -24,7 +28,8 @@
2428
### Changed
2529
- Property, Action, and Event description now use `links` rather than `href`. - [Spec PR](https://github.com/mozilla-iot/wot/pull/119)
2630

27-
[Unreleased]: https://github.com/mozilla-iot/webthing-java/compare/v0.12.1...HEAD
31+
[Unreleased]: https://github.com/mozilla-iot/webthing-java/compare/v0.12.2...HEAD
32+
[0.12.2]: https://github.com/mozilla-iot/webthing-java/compare/v0.12.1...v0.12.2
2833
[0.12.1]: https://github.com/mozilla-iot/webthing-java/compare/v0.12.0...v0.12.1
2934
[0.12.0]: https://github.com/mozilla-iot/webthing-java/compare/v0.11.0...v0.12.0
3035
[0.11.0]: https://github.com/mozilla-iot/webthing-java/compare/v0.10.0...v0.11.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.mozilla.iot</groupId>
88
<artifactId>webthing</artifactId>
9-
<version>0.12.1</version>
9+
<version>0.12.2</version>
1010

1111
<name>WebThing</name>
1212
<description>Implementation of an HTTP Web Thing.</description>

src/main/java/org/mozilla/iot/webthing/Thing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,10 @@ public boolean validateActionInput(JSONObject actionInput) {
756756
return true;
757757
}
758758

759+
if (actionInput == null) {
760+
actionInput = new JSONObject();
761+
}
762+
759763
try {
760764
this.schema.validate(actionInput);
761765
} catch (ValidationException e) {

src/main/java/org/mozilla/iot/webthing/WebThingServer.java

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,35 +1224,40 @@ public Response post(UriResource uriResource,
12241224
}
12251225

12261226
try {
1227-
JSONObject response = new JSONObject();
12281227
JSONArray actionNames = json.names();
1229-
if (actionNames == null) {
1228+
if (actionNames == null || actionNames.length() != 1) {
12301229
return corsResponse(NanoHTTPD.newFixedLengthResponse(
12311230
Response.Status.BAD_REQUEST,
12321231
null,
12331232
null));
12341233
}
12351234

1236-
for (int i = 0; i < actionNames.length(); ++i) {
1237-
String actionName = actionNames.getString(i);
1238-
JSONObject params = json.getJSONObject(actionName);
1239-
JSONObject input = null;
1240-
if (params.has("input")) {
1241-
input = params.getJSONObject("input");
1242-
}
1243-
1244-
Action action = thing.performAction(actionName, input);
1245-
if (action != null) {
1246-
response.put(actionName,
1247-
action.asActionDescription()
1248-
.getJSONObject(actionName));
1249-
1250-
(new ActionRunner(action)).start();
1251-
}
1235+
String actionName = actionNames.getString(0);
1236+
JSONObject params = json.getJSONObject(actionName);
1237+
JSONObject input = null;
1238+
if (params.has("input")) {
1239+
input = params.getJSONObject("input");
1240+
}
1241+
1242+
Action action = thing.performAction(actionName, input);
1243+
if (action != null) {
1244+
JSONObject response = new JSONObject();
1245+
response.put(actionName,
1246+
action.asActionDescription()
1247+
.getJSONObject(actionName));
1248+
1249+
(new ActionRunner(action)).start();
1250+
1251+
return corsResponse(NanoHTTPD.newFixedLengthResponse(
1252+
Response.Status.CREATED,
1253+
"application/json",
1254+
response.toString()));
1255+
} else {
1256+
return corsResponse(NanoHTTPD.newFixedLengthResponse(
1257+
Response.Status.BAD_REQUEST,
1258+
null,
1259+
null));
12521260
}
1253-
return corsResponse(NanoHTTPD.newFixedLengthResponse(Response.Status.CREATED,
1254-
"application/json",
1255-
response.toString()));
12561261
} catch (JSONException e) {
12571262
return corsResponse(NanoHTTPD.newFixedLengthResponse(Response.Status.INTERNAL_ERROR,
12581263
null,
@@ -1352,39 +1357,47 @@ public Response post(UriResource uriResource,
13521357
String actionName = this.getActionName(uriResource, session);
13531358

13541359
try {
1355-
JSONObject response = new JSONObject();
13561360
JSONArray actionNames = json.names();
1357-
if (actionNames == null) {
1361+
if (actionNames == null || actionNames.length() != 1) {
1362+
return corsResponse(NanoHTTPD.newFixedLengthResponse(
1363+
Response.Status.BAD_REQUEST,
1364+
null,
1365+
null));
1366+
}
1367+
1368+
String name = actionNames.getString(0);
1369+
if (!name.equals(actionName)) {
13581370
return corsResponse(NanoHTTPD.newFixedLengthResponse(
13591371
Response.Status.BAD_REQUEST,
13601372
null,
13611373
null));
13621374
}
13631375

1364-
for (int i = 0; i < actionNames.length(); ++i) {
1365-
String name = actionNames.getString(i);
1366-
if (!name.equals(actionName)) {
1367-
continue;
1368-
}
1369-
1370-
JSONObject params = json.getJSONObject(name);
1371-
JSONObject input = null;
1372-
if (params.has("input")) {
1373-
input = params.getJSONObject("input");
1374-
}
1375-
1376-
Action action = thing.performAction(name, input);
1377-
if (action != null) {
1378-
response.put(name,
1379-
action.asActionDescription()
1380-
.getJSONObject(name));
1381-
1382-
(new ActionRunner(action)).start();
1383-
}
1376+
JSONObject params = json.getJSONObject(name);
1377+
JSONObject input = null;
1378+
if (params.has("input")) {
1379+
input = params.getJSONObject("input");
1380+
}
1381+
1382+
Action action = thing.performAction(name, input);
1383+
if (action != null) {
1384+
JSONObject response = new JSONObject();
1385+
response.put(name,
1386+
action.asActionDescription()
1387+
.getJSONObject(name));
1388+
1389+
(new ActionRunner(action)).start();
1390+
1391+
return corsResponse(NanoHTTPD.newFixedLengthResponse(
1392+
Response.Status.CREATED,
1393+
"application/json",
1394+
response.toString()));
1395+
} else {
1396+
return corsResponse(NanoHTTPD.newFixedLengthResponse(
1397+
Response.Status.BAD_REQUEST,
1398+
null,
1399+
null));
13841400
}
1385-
return corsResponse(NanoHTTPD.newFixedLengthResponse(Response.Status.CREATED,
1386-
"application/json",
1387-
response.toString()));
13881401
} catch (JSONException e) {
13891402
return corsResponse(NanoHTTPD.newFixedLengthResponse(Response.Status.INTERNAL_ERROR,
13901403
null,

0 commit comments

Comments
 (0)