From e889f2ea6034ec3ac187518846c02ca10ede3c06 Mon Sep 17 00:00:00 2001 From: Minji Park Date: Fri, 19 Aug 2016 13:27:57 +0900 Subject: [PATCH] fix detected potential defects in RD - detected warnings with SVACE fixed Change-Id: I2b03fdb09905703f70eba03354d37f0cab9949fe Signed-off-by: Minji Park Reviewed-on: https://gerrit.iotivity.org/gerrit/10643 Tested-by: jenkins-iotivity Reviewed-by: Glen Youngjin Kim Reviewed-by: Jee Hyeok Kim Reviewed-by: Eunok Shin --- .../org/iotivity/cloud/rdserver/db/MongoDB.java | 2 +- .../resources/directory/res/DiscoveryResource.java | 11 +++--- .../presence/device/DevicePresenceResource.java | 40 +++++++++++++--------- .../cloud/rdserver/util/TypeCastingManager.java | 10 +++--- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/MongoDB.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/MongoDB.java index cd0dacd..ff48f63 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/MongoDB.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/MongoDB.java @@ -208,7 +208,7 @@ public class MongoDB { resPayload.setRt(rt.toString()); } Object href = doc.get(Constants.HREF); - if (rt != null) { + if (href != null) { Object di = doc.get(Constants.DEVICE_ID); if (di != null) { resPayload.setHref(href.toString()); diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java index ef91fb5..0462ed3 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java @@ -102,14 +102,14 @@ public class DiscoveryResource extends Resource { value = listITF.get(0); } - for (String deviceId : deviceList) { + for (String deviceId : deviceList) { if(key != null && value != null){ foundResList = DBManager.getInstance().findResourceAboutDiAndFilter(deviceId, key, value); - } else { + } else { foundResList = DBManager.getInstance().findResourceAboutDi(deviceId); } - + if (foundResList != null) { resourceList.add(makeDiscoveryPayloadSegment(foundResList)); } @@ -150,9 +150,10 @@ public class DiscoveryResource extends Resource { ArrayList> responseMapList = new ArrayList>(); for (DiscoveryPayload discoveryPayload : discoveryPayloadList) { - HashMap responseSegment = null; + DiscoveryTags tags = discoveryPayload.getTags(); - responseSegment = mDiscoveryTagsTypeManager + + HashMap responseSegment = mDiscoveryTagsTypeManager .convertObjectToMap(tags); ArrayList discoveryLinksList = discoveryPayload diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java index 6ed867d..eab6bf8 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java @@ -95,8 +95,8 @@ public class DevicePresenceResource extends Resource { break; default: - throw new BadRequestException( - request.getMethod() + " request type is not supported"); + throw new BadRequestException(request.getMethod() + + " request type is not supported"); } srcDevice.sendResponse(response); @@ -123,8 +123,8 @@ public class DevicePresenceResource extends Resource { for (String deviceId : deviceList) { HashMap payloadSegment = new HashMap(); payloadSegment.put(Constants.DEVICE_ID, deviceId); - payloadSegment.put(Constants.PRESENCE_STATE, - DBManager.getInstance().findDeviceState(deviceId)); + payloadSegment.put(Constants.PRESENCE_STATE, DBManager + .getInstance().findDeviceState(deviceId)); getPayload.add(payloadSegment); } @@ -157,8 +157,8 @@ public class DevicePresenceResource extends Resource { for (String deviceId : deviceList) { HashMap payloadSegment = new HashMap(); payloadSegment.put(Constants.DEVICE_ID, deviceId); - payloadSegment.put(Constants.PRESENCE_STATE, - DBManager.getInstance().findDeviceState(deviceId)); + payloadSegment.put(Constants.PRESENCE_STATE, DBManager + .getInstance().findDeviceState(deviceId)); getPayload.add(payloadSegment); } Log.i("Get observe response" + getPayload.toString()); @@ -168,13 +168,21 @@ public class DevicePresenceResource extends Resource { mCbor.encodingPayloadToCbor(getPayload)); } - public IResponse handlePostRequest(IRequest request) - throws ServerException { + public IResponse handlePostRequest(IRequest request) throws ServerException { // check payload byte[] payload = request.getPayload(); - HashMap parsedPayload = mCbor - .parsePayloadFromCbor(payload, HashMap.class); + if (payload == null) { + throw new PreconditionFailedException("payload is null"); + } + + HashMap parsedPayload = mCbor.parsePayloadFromCbor( + payload, HashMap.class); + + checkPayloadException( + Arrays.asList(Constants.DEVICE_ID, Constants.PRESENCE_STATE), + parsedPayload); + String deviceId = parsedPayload.get(Constants.DEVICE_ID).toString(); String state = parsedPayload.get(Constants.PRESENCE_STATE).toString(); DeviceState deviceState = new DeviceState(); @@ -205,8 +213,8 @@ public class DevicePresenceResource extends Resource { mDeviceSubscriber.put(deviceId, subscribers); } - subscribers.put(request.getRequestId(), - new PresenceSubscriber(srcDevice, request)); + subscribers.put(request.getRequestId(), new PresenceSubscriber( + srcDevice, request)); } mSubscribedDevices.put(request.getRequestId(), deviceIdList); @@ -214,8 +222,8 @@ public class DevicePresenceResource extends Resource { private void removeObserver(IRequest request) { - List deviceIdList = mSubscribedDevices - .get(request.getRequestId()); + List deviceIdList = mSubscribedDevices.get(request + .getRequestId()); if (deviceIdList == null) { return; @@ -246,8 +254,8 @@ public class DevicePresenceResource extends Resource { if (tokenNSubscribers != null) { for (PresenceSubscriber subscriber : tokenNSubscribers.values()) { - subscriber.mSubscriber.sendResponse( - MessageBuilder.createResponse(subscriber.mRequest, + subscriber.mSubscriber.sendResponse(MessageBuilder + .createResponse(subscriber.mRequest, ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(response))); diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/util/TypeCastingManager.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/util/TypeCastingManager.java index 3a3597a..cc9df99 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/util/TypeCastingManager.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/util/TypeCastingManager.java @@ -45,10 +45,11 @@ public class TypeCastingManager { } public HashMap convertObjectToMap(T objClass) { - try { + HashMap map = new HashMap(); + + try { Field[] fieldList = objClass.getClass().getDeclaredFields(); - HashMap map = new HashMap(); for (Field field : fieldList) { field.setAccessible(true); @@ -61,12 +62,11 @@ public class TypeCastingManager { map.put(fieldName, value); } } - - return map; } catch (Exception e) { e.printStackTrace(); } - return null; + + return map; } public T convertMaptoObject(HashMap map, T objClass) { -- 2.7.4