From 80334ef00d03e99ebc51f56943da3cea77be924a Mon Sep 17 00:00:00 2001 From: harshanL Date: Thu, 11 Dec 2014 17:22:32 +0530 Subject: [PATCH] Added new APIs to enrollment --- .../core/service/DeviceManagementService.java | 93 ++++++++++--------- .../main/java/cdm/api/android/Enrollment.java | 85 ++++++++++++----- ...droidAPIUtil.java => AndroidAPIUtils.java} | 2 +- 3 files changed, 110 insertions(+), 70 deletions(-) rename product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/{AndroidAPIUtil.java => AndroidAPIUtils.java} (99%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index d7508fb784d..93cb7f18bc1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -25,59 +25,64 @@ import java.util.List; public class DeviceManagementService implements DeviceManagerService { - @Override - public String getProviderType() { - return null; - } + @Override + public String getProviderType() { + return null; + } - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().enrollDevice(device); - } + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().enrollDevice(device); + } - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().modifyEnrollment(device); - } + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().modifyEnrollment(device); + } - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().disenrollDevice(deviceId); - } + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager() + .disenrollDevice(deviceId); + } - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isEnrolled(deviceId); - } + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().isEnrolled(deviceId); + } - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isActive(deviceId); - } + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().isActive(deviceId); + } - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().setActive(deviceId, status); - } + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager() + .setActive(deviceId, status); + } - @Override - public List getAllDevices(String type) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getAllDevices(type); - } + @Override + public List getAllDevices(String type) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().getAllDevices(type); + } - @Override - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getDevice(deviceId); - } + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().getDevice(deviceId); + } - @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().updateDeviceInfo(device); - } + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().updateDeviceInfo(device); + } - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().setOwnership(deviceId, ownershipType); - } + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManager() + .setOwnership(deviceId, ownershipType); + } } 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 87d2feadbe4..98501e3bddb 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 @@ -16,12 +16,9 @@ package cdm.api.android; -import cdm.api.android.util.AndroidAPIUtil; -import com.google.gson.Gson; -import com.google.gson.JsonObject; +import cdm.api.android.util.AndroidAPIUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; @@ -55,7 +52,7 @@ public class Enrollment { } finally { PrivilegedCarbonContext.endTenantFlow(); } - Device device = AndroidAPIUtil.convertToDeviceObject(jsonPayload); + Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { if(dmService!=null){ result = dmService.enrollDevice(device); @@ -88,13 +85,26 @@ public class Enrollment { boolean result = false; int status = 0; String msg = ""; - CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); - DeviceManagementService dmService = (DeviceManagementService) context - .getOSGiService(DeviceManagementService.class, null); + DeviceManagementService dmService; try { - DeviceIdentifier deviceIdentifier = AndroidAPIUtil.convertToDeviceIdentifierObject(id); - result = dmService.isEnrolled(deviceIdentifier); - status = 1; + PrivilegedCarbonContext.startTenantFlow(); + 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(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if(dmService!=null){ + result = dmService.isEnrolled(deviceIdentifier); + status = 1; + }else{ + status = -1; + msg = "Device Manager service not available"; + } } catch (DeviceManagementException e) { msg = "Error occurred while checking enrollment of the device"; log.error(msg, e); @@ -113,19 +123,31 @@ public class Enrollment { } @PUT - @Consumes("application/json") @Path("{id}") - public Response modifyEnrollment(@PathParam("id") String id) { + public Response modifyEnrollment(@PathParam("id") String id,String jsonPayload) { boolean result = false; int status = 0; String msg = ""; - CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); - DeviceManagementService dmService = (DeviceManagementService) context - .getOSGiService(DeviceManagementService.class, null); - Device device = AndroidAPIUtil.convertToDeviceObject(null); + DeviceManagementService dmService; try { - result = dmService.modifyEnrollment(device); - status = 1; + PrivilegedCarbonContext.startTenantFlow(); + 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 = AndroidAPIUtils.convertToDeviceObject(jsonPayload); + try { + if(dmService!=null){ + result = dmService.modifyEnrollment(device); + status = 1; + }else{ + status = -1; + msg = "Device Manager service not available"; + } } catch (DeviceManagementException e) { msg = "Error occurred while modifying enrollment of the device"; log.error(msg, e); @@ -149,13 +171,26 @@ public class Enrollment { boolean result = false; int status = 0; String msg = ""; - CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); - DeviceManagementService dmService = (DeviceManagementService) context - .getOSGiService(DeviceManagementService.class, null); + DeviceManagementService dmService; try { - DeviceIdentifier deviceIdentifier = AndroidAPIUtil.convertToDeviceIdentifierObject(id); - result = dmService.disenrollDevice(deviceIdentifier); - status = 1; + PrivilegedCarbonContext.startTenantFlow(); + 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(); + } + DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); + try { + if(dmService!=null){ + result = dmService.disenrollDevice(deviceIdentifier); + status = 1; + }else{ + status = -1; + msg = "Device Manager service not available"; + } } catch (DeviceManagementException e) { msg = "Error occurred while disenrolling 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/AndroidAPIUtils.java similarity index 99% rename from product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java rename to product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtils.java index be6d1fcf8c0..f9f32a125f4 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/AndroidAPIUtils.java @@ -26,7 +26,7 @@ import java.util.*; /** * AndroidAPIUtil class provides utility function used by Android REST-API classes. */ -public class AndroidAPIUtil { +public class AndroidAPIUtils { public static Device convertToDeviceObject(String jsonString) { JsonObject obj = new Gson().fromJson(jsonString, JsonObject.class);