From 97afea6d54eebb1656233c6b4e52ff5fedd65334 Mon Sep 17 00:00:00 2001 From: Larry Sachs Date: Thu, 29 Sep 2016 10:46:19 -0700 Subject: [PATCH] Throw OcException for invalid (null) parameters in Java Layer of android_api [IOT-1323] -- [RI][Android] observe(with QualityOfService) API does not throw OcException while passing parameter-observeType as Null value Change-Id: If0d7acb0f5bbfda75e2fd774b6cccc8ce21e5650 Signed-off-by: Larry Sachs Reviewed-on: https://gerrit.iotivity.org/gerrit/12573 Tested-by: jenkins-iotivity Reviewed-by: Rick Bell --- .../java/org/iotivity/base/SmokeTest.java | 7 +- .../main/java/org/iotivity/base/OcResource.java | 87 +++++++++++++++++++++- .../org/iotivity/base/examples/FridgeClient.java | 7 +- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java index 9f814d8..a3a2ba7 100644 --- a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java +++ b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java @@ -1335,7 +1335,12 @@ public class SmokeTest extends InstrumentationTestCase { headerOptionList.add(new OcHeaderOption(2885, "OptionData1")); headerOptionList.add(new OcHeaderOption(2886, "OptionData2")); - resource.setHeaderOptions(headerOptionList); + try { + resource.setHeaderOptions(headerOptionList); + } catch (OcException e) { + Log.e(TAG, "onResourceFound, error in setHeaderOptions -- " + e.getMessage()); + } + resource.unsetHeaderOptions(); OcResourceIdentifier resourceIdentifier = resource.getUniqueIdentifier(); diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java b/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java index c0fd090..f9cc04f 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcResource.java @@ -65,6 +65,11 @@ public class OcResource { public void get(Map queryParamsMap, OnGetListener onGetListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.get1(queryParamsMap, onGetListener, qualityOfService.getValue()); } @@ -116,6 +121,11 @@ public class OcResource { Map queryParamsMap, OnGetListener onGetListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.get3( resourceType, resourceInterface, @@ -157,6 +167,11 @@ public class OcResource { Map queryParamsMap, OnPutListener onPutListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.put1( ocRepresentation, queryParamsMap, @@ -217,6 +232,11 @@ public class OcResource { Map queryParamsMap, OnPutListener onPutListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.put3( resourceType, resourceInterface, @@ -260,6 +280,11 @@ public class OcResource { Map queryParamsMap, OnPostListener onPostListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.post1( ocRepresentation, queryParamsMap, @@ -320,6 +345,11 @@ public class OcResource { Map queryParamsMap, OnPostListener onPostListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.post3( resourceType, resourceInterface, @@ -351,6 +381,11 @@ public class OcResource { */ public void deleteResource(OnDeleteListener onDeleteListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.deleteResource1(onDeleteListener, qualityOfService.getValue()); } @@ -399,6 +434,15 @@ public class OcResource { Map queryParamsMap, OnObserveListener onObserveListener, QualityOfService qualityOfService) throws OcException { + + if (observeType == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "observeType cannot be null"); + } + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.observe1( observeType.getValue(), queryParamsMap, @@ -427,6 +471,11 @@ public class OcResource { * @throws OcException */ public void cancelObserve(QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.cancelObserve1(qualityOfService.getValue()); } @@ -437,8 +486,14 @@ public class OcResource { * * @param headerOptionList List where header information(header optionID and * optionData is passed + * @throws OcException */ - public void setHeaderOptions(List headerOptionList) { + public void setHeaderOptions(List headerOptionList) throws OcException { + + if (headerOptionList == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "headerOptionList cannot be null"); + } + this.setHeaderOptions(headerOptionList.toArray( new OcHeaderOption[headerOptionList.size()]) ); @@ -609,6 +664,11 @@ public class OcResource { public void discoveryMQTopics(Map queryParamsMap, OnMQTopicFoundListener onTopicFoundListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.discoveryMQTopicsImpl(queryParamsMap, onTopicFoundListener, qualityOfService.getValue()); } @@ -634,6 +694,11 @@ public class OcResource { Map queryParamsMap, OnMQTopicCreatedListener onTopicCreatedListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.createMQTopicImpl(ocRepresentation, uri, queryParamsMap, onTopicCreatedListener, qualityOfService.getValue()); } @@ -657,6 +722,11 @@ public class OcResource { public void subscribeMQTopic(Map queryParamsMap, OnObserveListener onObserveListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.subscribeMQTopicImpl(queryParamsMap, onObserveListener, qualityOfService.getValue()); @@ -673,6 +743,11 @@ public class OcResource { * @throws OcException */ public void unsubscribeMQTopic(QualityOfService qualityOfService) throws OcException{ + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.unsubscribeMQTopicImpl(qualityOfService.getValue()); } @@ -691,6 +766,11 @@ public class OcResource { public void requestMQPublish(Map queryParamsMap, OnPostListener onPostListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.requestMQPublishImpl(queryParamsMap, onPostListener, qualityOfService.getValue()); @@ -714,6 +794,11 @@ public class OcResource { Map queryParamsMap, OnPostListener onPostListener, QualityOfService qualityOfService) throws OcException { + + if (qualityOfService == null) { + throw new OcException(ErrorCode.INVALID_PARAM, "qualityOfService cannot be null"); + } + this.publishMQTopicImpl(ocRepresentation, queryParamsMap, onPostListener, diff --git a/android/examples/fridgeclient/src/main/java/org/iotivity/base/examples/FridgeClient.java b/android/examples/fridgeclient/src/main/java/org/iotivity/base/examples/FridgeClient.java index 736e219..73c69a9 100755 --- a/android/examples/fridgeclient/src/main/java/org/iotivity/base/examples/FridgeClient.java +++ b/android/examples/fridgeclient/src/main/java/org/iotivity/base/examples/FridgeClient.java @@ -163,7 +163,12 @@ public class FridgeClient extends Activity implements OcHeaderOption clientToken = new OcHeaderOption(CLIENT_TOKEN_KEY, CLIENT_TOKEN); headerOptions.add(apiVersion); headerOptions.add(clientToken); - mFridgeResource.setHeaderOptions(headerOptions); + try { + mFridgeResource.setHeaderOptions(headerOptions); + } catch (OcException e) { + logMessage("Error in setHeaderOptions"); + Log.e(TAG, e.getMessage()); + } logMessage("Calling GET api on mFridgeResource and other component resources"); try { -- 2.7.4