From 6d5ea7f58600ba00339e5dd7beb66b6f2160abf8 Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 11 Dec 2014 15:21:18 +0530 Subject: [PATCH] Added JSON to Device object conversion --- .../wso2/carbon/device/mgt/common/Device.java | 9 +++ .../carbon/device/mgt/common/Property.java | 39 +++++++++++ .../src/main/java/cdm/api/android/Device.java | 1 - .../main/java/cdm/api/android/Enrollment.java | 17 +++-- .../cdm/api/android/util/AndroidAPIUtil.java | 66 ++++++++++++++++--- .../api/android/util/AndroidConstants.java | 48 ++++++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 1 + 7 files changed, 166 insertions(+), 15 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Property.java create mode 100644 product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidConstants.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java index 5ee72f4da..8675c8af8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java @@ -44,6 +44,8 @@ public class Device { private List features; + private List properties; + public int getId() { return id; } @@ -140,4 +142,11 @@ public class Device { this.type = type; } + public List getProperties() { + return properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Property.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Property.java new file mode 100644 index 000000000..399ee6707 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Property.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wso2.carbon.device.mgt.common; + +public class Property { + + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java index abca85290..108e3859d 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java @@ -26,7 +26,6 @@ import javax.ws.rs.core.Response; /** * Android Device Management REST-API implementation. */ -@Path("/devices") public class Device { @GET diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java index 866bae0cf..87d2feadb 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -17,6 +17,7 @@ package cdm.api.android; import cdm.api.android.util.AndroidAPIUtil; +import com.google.gson.Gson; import com.google.gson.JsonObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -34,13 +35,12 @@ import javax.ws.rs.core.Response; /** * Android Device Enrollment REST-API implementation. */ -@Path("/enrollment") public class Enrollment { private static Log log = LogFactory.getLog(Enrollment.class); @POST - public Response enrollDevice() { + public Response enrollDevice(String jsonPayload) { boolean result = false; int status = 0; String msg = ""; @@ -50,16 +50,21 @@ public class Enrollment { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - dmService = (DeviceManagementService) ctx .getOSGiService(DeviceManagementService.class, null); } finally { PrivilegedCarbonContext.endTenantFlow(); } - Device device = AndroidAPIUtil.convertToDeviceObject(null); + Device device = AndroidAPIUtil.convertToDeviceObject(jsonPayload); try { - result = dmService.enrollDevice(device); - status = 1; + if(dmService!=null){ + result = dmService.enrollDevice(device); + status = 1; + }else{ + status = -1; + msg = "Device Manager service not available"; + } + } catch (DeviceManagementException e) { msg = "Error occurred while enrolling the device"; log.error(msg, e); 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 01c384de6..e5ec78997 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 @@ -16,26 +16,76 @@ package cdm.api.android.util; +import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.*; +import java.util.*; /** - * AndroidAPIUtil class provides utility function used by Android REST-API classes. + * AndroidAPIUtil class provides utility function used by Android REST-API classes. */ public class AndroidAPIUtil { - public static Device convertToDeviceObject(JsonObject json){ + 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.setName("Test Device"); - device.setOwner("harshan"); + 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)); return device; } - public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId){ + private static List parseProperties(JsonObject properties) { + List propertyList = new ArrayList(0); + for (Map.Entry entry : properties.entrySet()) { + propertyList.add(parseProperty(entry.getKey(), entry.getValue())); + } + // propertyList.add(parseProperty("regid", properties.get("regid").getAsString())); + // propertyList.add(parseProperty("osversion", properties.get("osversion").getAsString())); + // propertyList.add(parseProperty("vendor", properties.get("vendor").getAsString())); + // propertyList.add(parseProperty("imei", properties.get("imei").getAsString())); + // propertyList.add(parseProperty("imsi", properties.get("imsi").getAsString())); + // propertyList.add(parseProperty("model", properties.get("model").getAsString())); + return propertyList; + } + + private static List parseFeatures(JsonObject features) { + List featureList = new ArrayList(0); + return featureList; + } + + private static Property parseProperty(String property, JsonElement value) { + Property prop = new Property(); + prop.setName(property); + prop.setValue(value.getAsString()); + return prop; + } + + private static Feature parseFeature(JsonElement featureElement) { + Feature feature = new Feature(); + return feature; + } + + public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId) { DeviceIdentifier identifier = new DeviceIdentifier(); identifier.setId(deviceId); identifier.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidConstants.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidConstants.java new file mode 100644 index 000000000..b76820fc5 --- /dev/null +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidConstants.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cdm.api.android.util; + +/** + * Defines constants used in Android-REST API bundle. + */ +public final class AndroidConstants { + + public final class DeviceProperties{ + private DeviceProperties() { + throw new AssertionError(); + } + public static final String PROPERTY_USER_KEY = "username"; + public static final String PROPERTY_DEVICE_KEY = "device"; + } + + public final class DeviceFeatures{ + private DeviceFeatures() { + throw new AssertionError(); + } + } + + public final class DeviceConstants{ + private DeviceConstants() { + throw new AssertionError(); + } + public static final String DEVICE_MAC_KEY = "mac"; + public static final String DEVICE_DESCRIPTION_KEY = "description"; + public static final String DEVICE_OWNERSHIP_KEY = "ownership"; + public static final String DEVICE_PROPERTIES_KEY = "properties"; + public static final String DEVICE_FEATURES_KEY = "features"; + } +} diff --git a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml index 230a2b45e..a75cad359 100644 --- a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -38,6 +38,7 @@ +