From dacd16b852e65fe18d3084bc40e4eecc7cef1ec5 Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 11 Dec 2014 15:48:59 +0530 Subject: [PATCH] Added validations to json objects in Android-REST apis --- .../cdm/api/android/util/AndroidAPIUtil.java | 60 +++++++++++++------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java index e5ec78997..be6d1fcf8 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java @@ -29,28 +29,50 @@ import java.util.*; public class AndroidAPIUtil { public static Device convertToDeviceObject(String jsonString) { - JsonObject obj = new Gson().fromJson(jsonString, JsonObject.class); - JsonObject properties = - new Gson().fromJson(obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY) - , JsonObject.class); - JsonObject features = - new Gson().fromJson( - obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY), - JsonObject.class); Device device = new Device(); + device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY).getAsString()); - device.setDeviceIdentifier( - obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY).getAsString()); - device.setDescription( - obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY).getAsString()); - device.setOwnership( - obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY).getAsString()); - device.setName(properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY) - .getAsString()); - device.setFeatures(parseFeatures(features)); - device.setProperties(parseProperties(properties)); + if (obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY) != null) { + device.setDeviceIdentifier( + obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY).getAsString()); + } + if (obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY) != null) { + device.setDescription( + obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY).getAsString()); + } + if (obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY) != null) { + device.setOwnership( + obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY).getAsString()); + } + + if (obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY) != null) { + JsonObject properties = + new Gson().fromJson( + obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY) + , JsonObject.class); + if (properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY) != null) { + device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY) + .getAsString()); + } + if (properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY) != null) { + device.setName(properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY) + .getAsString()); + } + device.setProperties(parseProperties(properties)); + }else{ + device.setProperties(new ArrayList(0)); + } + + if (obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY) != null) { + JsonObject features = + new Gson().fromJson( + obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY), + JsonObject.class); + device.setFeatures(parseFeatures(features)); + }else{ + device.setFeatures(new ArrayList(0)); + } return device; }