@@ -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