From 4790de94ef06fe1a55658351f4f995119e5e9bf3 Mon Sep 17 00:00:00 2001 From: mharindu Date: Fri, 21 Aug 2015 16:46:45 +0530 Subject: [PATCH 1/4] Implemented policy compliance checking function for Android plugin --- .../pom.xml | 7 +++++- .../AndroidPolicyMonitoringService.java | 25 ++++++++++++++++--- .../pom.xml | 4 +++ pom.xml | 6 +++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml index b72c5b8e09..05201d0159 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml @@ -76,7 +76,8 @@ org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.session, org.wso2.carbon.registry.api, - org.wso2.carbon.device.mgt.extensions.license.mgt.registry + org.wso2.carbon.device.mgt.extensions.license.mgt.registry, + com.google.gson.* !org.wso2.carbon.device.mgt.mobile.internal, @@ -165,5 +166,9 @@ h2-database-engine test + + com.google.code.gson + gson + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index de37861c83..c9dee0daec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -19,6 +19,10 @@ package org.wso2.carbon.device.mgt.mobile.impl.android; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -43,15 +47,28 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService { } @Override - public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object o) throws PolicyComplianceException { - ComplianceData complianceData = new ComplianceData(); - if (log.isDebugEnabled()) { + public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object o) + throws PolicyComplianceException { + if (log.isDebugEnabled()) { log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); } if (o == null || policy == null) { return null; } - List complianceFeatures = (List) o; + ComplianceData complianceData = new ComplianceData(); + List complianceFeatures = new ArrayList(); + + // Parsing json string to get compliance features. + JsonElement jsonElement = new JsonParser().parse((String) o); + JsonArray jsonArray = jsonElement.getAsJsonArray(); + Gson gson = new Gson(); + ComplianceFeature complianceFeature; + + for (int i = 0; i < jsonArray.size(); i++) { + complianceFeature = gson.fromJson(jsonArray.get(i), ComplianceFeature.class); + complianceFeatures.add(complianceFeature); + } + complianceData.setComplianceFeatures(complianceFeatures); for (ComplianceFeature cf : complianceFeatures) { diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/pom.xml b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/pom.xml index 92c9564374..6be1ceae20 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/pom.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/pom.xml @@ -58,6 +58,10 @@ org.wso2.carbon.device.mgt.extensions.feature zip + + com.google.code.gson + gson + diff --git a/pom.xml b/pom.xml index b5a35ee11d..34409e1cdf 100644 --- a/pom.xml +++ b/pom.xml @@ -511,6 +511,11 @@ axis2 ${axis2.orbit.version} + + com.google.code.gson + gson + ${google.gson.version} + @@ -577,6 +582,7 @@ 1.49 2.0.0 3.0.21 + 2.2.4 From eab9aa93d0409775020ea28b9cbb65fb38fa1513 Mon Sep 17 00:00:00 2001 From: mharindu Date: Fri, 21 Aug 2015 16:57:56 +0530 Subject: [PATCH 2/4] Fixed formating issues --- .../AndroidPolicyMonitoringService.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index c9dee0daec..d6e0fceeb5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -39,27 +39,27 @@ import java.util.List; public class AndroidPolicyMonitoringService implements PolicyMonitoringService { - private static Log log = LogFactory.getLog(AndroidPolicyMonitoringService.class); + private static Log log = LogFactory.getLog(AndroidPolicyMonitoringService.class); - @Override - public void notifyDevices(List list) throws PolicyComplianceException { + @Override + public void notifyDevices(List list) throws PolicyComplianceException { - } + } - @Override - public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object o) - throws PolicyComplianceException { + @Override + public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, + Object compliancePayload) throws PolicyComplianceException { if (log.isDebugEnabled()) { - log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); - } - if (o == null || policy == null) { - return null; - } + log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); + } + if (compliancePayload == null || policy == null) { + return null; + } ComplianceData complianceData = new ComplianceData(); List complianceFeatures = new ArrayList(); // Parsing json string to get compliance features. - JsonElement jsonElement = new JsonParser().parse((String) o); + JsonElement jsonElement = new JsonParser().parse((String) compliancePayload); JsonArray jsonArray = jsonElement.getAsJsonArray(); Gson gson = new Gson(); ComplianceFeature complianceFeature; @@ -69,19 +69,19 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService { complianceFeatures.add(complianceFeature); } - complianceData.setComplianceFeatures(complianceFeatures); + complianceData.setComplianceFeatures(complianceFeatures); - for (ComplianceFeature cf : complianceFeatures) { - if(!cf.isCompliance()){ - complianceData.setStatus(false); - break; - } - } - return complianceData; - } + for (ComplianceFeature cf : complianceFeatures) { + if (!cf.isCompliance()) { + complianceData.setStatus(false); + break; + } + } + return complianceData; + } - @Override - public String getType() { - return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; - } + @Override + public String getType() { + return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; + } } \ No newline at end of file From 6e469087b6d525a1b603443c5711be822ce520e2 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 21 Aug 2015 17:42:06 +0530 Subject: [PATCH 3/4] Tenant configuration changes --- .../impl/android/AndroidDeviceManager.java | 19 +++++++------- .../impl/windows/WindowsDeviceManager.java | 15 ++++++----- .../util/MobileDeviceManagementUtil.java | 26 ++++++++++--------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java index d4e064ef4a..5db5baa8b9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java @@ -75,8 +75,7 @@ public class AndroidDeviceManager implements DeviceManager { @Override public boolean saveConfiguration(TenantConfiguration tenantConfiguration) throws DeviceManagementException { - boolean status = false; - Resource resource; + boolean status; try { if (log.isDebugEnabled()) { log.debug("Persisting android configurations in Registry"); @@ -89,7 +88,7 @@ public class AndroidDeviceManager implements DeviceManager { Marshaller marshaller = context.createMarshaller(); marshaller.marshal(tenantConfiguration, writer); - resource = MobileDeviceManagementUtil.getRegistry().newResource(); + Resource resource = MobileDeviceManagementUtil.getConfigurationRegistry().newResource(); resource.setContent(writer.toString()); resource.setMediaType(MobilePluginConstants.MEDIA_TYPE_XML); MobileDeviceManagementUtil.putRegistryResource(resourcePath, resource); @@ -115,12 +114,14 @@ public class AndroidDeviceManager implements DeviceManager { MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); resource = MobileDeviceManagementUtil.getRegistryResource(androidRegPath); - JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); - Unmarshaller unmarshaller = context.createUnmarshaller(); - return (TenantConfiguration) unmarshaller.unmarshal( - new StringReader(new String((byte[]) resource.getContent(), Charset - .forName(MobilePluginConstants.CHARSET_UTF8)))); - + if(resource != null){ + JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (TenantConfiguration) unmarshaller.unmarshal( + new StringReader(new String((byte[]) resource.getContent(), Charset + .forName(MobilePluginConstants.CHARSET_UTF8)))); + } + return new TenantConfiguration(); } catch (MobileDeviceMgtPluginException e) { throw new DeviceManagementException( "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java index d7dbe3e614..691fe454db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java @@ -79,7 +79,7 @@ public class WindowsDeviceManager implements DeviceManager { Marshaller marshaller = context.createMarshaller(); marshaller.marshal(tenantConfiguration, writer); - resource = MobileDeviceManagementUtil.getRegistry().newResource(); + resource = MobileDeviceManagementUtil.getConfigurationRegistry().newResource(); resource.setContent(writer.toString()); resource.setMediaType(MobilePluginConstants.MEDIA_TYPE_XML); MobileDeviceManagementUtil.putRegistryResource(resourcePath, resource); @@ -105,11 +105,14 @@ public class WindowsDeviceManager implements DeviceManager { MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); resource = MobileDeviceManagementUtil.getRegistryResource(androidRegPath); - JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); - Unmarshaller unmarshaller = context.createUnmarshaller(); - return (TenantConfiguration) unmarshaller.unmarshal( - new StringReader(new String((byte[]) resource.getContent(), Charset - .forName(MobilePluginConstants.CHARSET_UTF8)))); + if(resource != null){ + JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (TenantConfiguration) unmarshaller.unmarshal( + new StringReader(new String((byte[]) resource.getContent(), Charset + .forName(MobilePluginConstants.CHARSET_UTF8)))); + } + return new TenantConfiguration(); } catch (MobileDeviceMgtPluginException e) { throw new DeviceManagementException( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java index 3975c08f64..768c55d4c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java @@ -198,7 +198,7 @@ public class MobileDeviceManagementUtil { return feature; } - public static Registry getRegistry() throws MobileDeviceMgtPluginException { + public static Registry getConfigurationRegistry() throws MobileDeviceMgtPluginException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); return MobileDeviceManagementDataHolder.getInstance().getRegistryService() @@ -213,8 +213,10 @@ public class MobileDeviceManagementUtil { public static Resource getRegistryResource(String path) throws MobileDeviceMgtPluginException { try { - return MobileDeviceManagementUtil.getRegistry().get(path); - + if(MobileDeviceManagementUtil.getConfigurationRegistry().resourceExists(path)){ + return MobileDeviceManagementUtil.getConfigurationRegistry().get(path); + } + return null; } catch (RegistryException e) { throw new MobileDeviceMgtPluginException("Error in retrieving registry resource : " + e.getMessage(), e); @@ -224,11 +226,11 @@ public class MobileDeviceManagementUtil { public static boolean putRegistryResource(String path, Resource resource) throws MobileDeviceMgtPluginException { - boolean status = false; + boolean status; try { - MobileDeviceManagementUtil.getRegistry().beginTransaction(); - MobileDeviceManagementUtil.getRegistry().put(path, resource); - MobileDeviceManagementUtil.getRegistry().commitTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().beginTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().put(path, resource); + MobileDeviceManagementUtil.getConfigurationRegistry().commitTransaction(); status = true; } catch (RegistryException e) { throw new MobileDeviceMgtPluginException( @@ -279,11 +281,11 @@ public class MobileDeviceManagementUtil { public static boolean createRegistryCollection(String path) throws MobileDeviceMgtPluginException { try { - if (! MobileDeviceManagementUtil.getRegistry().resourceExists(path)) { - Resource resource = MobileDeviceManagementUtil.getRegistry().newCollection(); - MobileDeviceManagementUtil.getRegistry().beginTransaction(); - MobileDeviceManagementUtil.getRegistry().put(path, resource); - MobileDeviceManagementUtil.getRegistry().commitTransaction(); + if (! MobileDeviceManagementUtil.getConfigurationRegistry().resourceExists(path)) { + Resource resource = MobileDeviceManagementUtil.getConfigurationRegistry().newCollection(); + MobileDeviceManagementUtil.getConfigurationRegistry().beginTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().put(path, resource); + MobileDeviceManagementUtil.getConfigurationRegistry().commitTransaction(); } return true; } catch (MobileDeviceMgtPluginException e) { From 3eec1a38466cc6ef2692918f235226f042f3c223 Mon Sep 17 00:00:00 2001 From: mharindu Date: Sat, 22 Aug 2015 04:55:22 +0530 Subject: [PATCH 4/4] Refactored policy compliance --- .../mobile/impl/android/AndroidPolicyMonitoringService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index d6e0fceeb5..acc7337c2d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -52,10 +52,10 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService { if (log.isDebugEnabled()) { log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); } + ComplianceData complianceData = new ComplianceData(); if (compliancePayload == null || policy == null) { - return null; + return complianceData; } - ComplianceData complianceData = new ComplianceData(); List complianceFeatures = new ArrayList(); // Parsing json string to get compliance features.