From d352daf626cd4df0142fe9110c7608390120bab8 Mon Sep 17 00:00:00 2001 From: harshanL Date: Fri, 12 Dec 2014 12:07:28 +0530 Subject: [PATCH] Refactored Android-REST API code --- .../java/cdm/api/android/Authentication.java | 12 +-- .../src/main/java/cdm/api/android/Device.java | 79 ++++++++---------- .../main/java/cdm/api/android/Enrollment.java | 82 +++++++------------ .../cdm/api/android/util/AndroidAPIUtils.java | 15 ++++ .../api/android/util/AndroidConstants.java | 8 ++ 5 files changed, 93 insertions(+), 103 deletions(-) diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java index 6bf19ed71d..9f04a31105 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java @@ -16,7 +16,6 @@ package cdm.api.android; - import com.google.gson.JsonObject; import javax.ws.rs.POST; @@ -31,9 +30,10 @@ public class Authentication { @POST @Path("/device/") @Produces("application/json") - public String authenticateDevice(@FormParam("username") String username, @FormParam("password") String password) { + public String authenticateDevice(@FormParam("username") String username, + @FormParam("password") String password) { JsonObject result = new JsonObject(); - result.addProperty("senderId","jwwfowrjwqporqwrpqworpq"); + result.addProperty("senderId", "jwwfowrjwqporqwrpqworpq"); return result.toString(); } @@ -42,10 +42,4 @@ public class Authentication { public String getLicense() { return "License Agreement"; } - - @POST - @Path("/device/enroll") - public Response enrollDevice() { - return Response.status(201).entity("Registration Successful").build(); - } } 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 4bf8c21685..ae2e805dd2 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 @@ -17,8 +17,9 @@ package cdm.api.android; import cdm.api.android.util.AndroidAPIUtils; +import cdm.api.android.util.AndroidConstants; import com.google.gson.Gson; -import com.google.gson.JsonObject; +import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -26,7 +27,6 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.ws.rs.*; import javax.ws.rs.core.Response; @@ -46,22 +46,18 @@ public class Device { String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } try { - if(dmService!=null){ - result = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + if (dmService != null) { + result = dmService.getAllDevices( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { @@ -71,12 +67,14 @@ public class Device { } switch (status) { case 1: - String response = new Gson().toJson(result); - return Response.status(200).entity(response).build(); + if(result!=null){ + String response = new Gson().toJson(result); + return Response.status(HttpStatus.SC_OK).entity(response).build(); + } case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(400).entity("Unable to fetch device list").build(); + return Response.status(HttpStatus.SC_NOT_FOUND).entity("Unable to fetch device list").build(); } @GET @@ -85,25 +83,21 @@ public class Device { int status = 0; String msg = ""; DeviceManagementService dmService; - org.wso2.carbon.device.mgt.common.Device device = new org.wso2.carbon.device.mgt.common.Device(); + org.wso2.carbon.device.mgt.common.Device device = + new org.wso2.carbon.device.mgt.common.Device(); try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); try { - if(dmService!=null){ + if (dmService != null) { device = dmService.getDevice(deviceIdentifier); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { @@ -113,12 +107,15 @@ public class Device { } switch (status) { case 1: - String response = new Gson().toJson(device); - return Response.status(200).entity(response).build(); + if(device!=null) { + String response = new Gson().toJson(device); + return Response.status(HttpStatus.SC_OK).entity(response).build(); + } + break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(400).entity("Unable to fetch device information").build(); + return Response.status(HttpStatus.SC_NOT_FOUND).entity("Unable to fetch device information").build(); } @PUT @@ -129,23 +126,19 @@ public class Device { String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } - org.wso2.carbon.device.mgt.common.Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); + org.wso2.carbon.device.mgt.common.Device device = + AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { - if(dmService!=null){ + if (dmService != null) { result = dmService.updateDeviceInfo(device); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { msg = "Error occurred while modifying the device information"; @@ -155,12 +148,12 @@ public class Device { switch (status) { case 1: if (result) { - return Response.status(200).entity("Device has modified").build(); + return Response.status(HttpStatus.SC_OK).entity("Device has modified").build(); } break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(400).entity("Update device has failed").build(); + return Response.status(HttpStatus.SC_NOT_MODIFIED).entity("Update device has failed").build(); } } 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 98501e3bdd..7a7b3bd29a 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,14 +17,15 @@ package cdm.api.android; import cdm.api.android.util.AndroidAPIUtils; +import cdm.api.android.util.AndroidConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.commons.httpclient.HttpStatus; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.ws.rs.*; import javax.ws.rs.core.Response; @@ -43,25 +44,19 @@ public class Enrollment { String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { - if(dmService!=null){ + if (dmService != null) { result = dmService.enrollDevice(device); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } - } catch (DeviceManagementException e) { msg = "Error occurred while enrolling the device"; log.error(msg, e); @@ -70,13 +65,13 @@ public class Enrollment { switch (status) { case 1: if (result) { - return Response.status(201).entity("Registration Successful").build(); + return Response.status(HttpStatus.SC_CREATED).entity("Device enrollment has succeeded").build(); } break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(400).entity("Registration Failed").build(); + return Response.status(HttpStatus.SC_BAD_REQUEST).entity("Device enrollment has Failed").build(); } @GET @@ -87,23 +82,18 @@ public class Enrollment { String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); try { - if(dmService!=null){ + if (dmService != null) { result = dmService.isEnrolled(deviceIdentifier); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { msg = "Error occurred while checking enrollment of the device"; @@ -113,40 +103,35 @@ public class Enrollment { switch (status) { case 1: if (result) { - return Response.status(200).entity(result).build(); + return Response.status(HttpStatus.SC_OK).entity(result).build(); } break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(404).entity(result).build(); + return Response.status(HttpStatus.SC_NOT_FOUND).entity(result).build(); } @PUT @Path("{id}") - public Response modifyEnrollment(@PathParam("id") String id,String jsonPayload) { + public Response modifyEnrollment(@PathParam("id") String id, String jsonPayload) { boolean result = false; int status = 0; String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } Device device = AndroidAPIUtils.convertToDeviceObject(jsonPayload); try { - if(dmService!=null){ + if (dmService != null) { result = dmService.modifyEnrollment(device); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { msg = "Error occurred while modifying enrollment of the device"; @@ -156,13 +141,13 @@ public class Enrollment { switch (status) { case 1: if (result) { - return Response.status(200).entity("Device information modified").build(); + return Response.status(HttpStatus.SC_OK).entity("Enrollment information has modified").build(); } break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(400).entity("Update enrollment failed").build(); + return Response.status(HttpStatus.SC_NOT_MODIFIED).entity("Update enrollment has failed").build(); } @DELETE @@ -173,23 +158,18 @@ public class Enrollment { String msg = ""; DeviceManagementService dmService; try { - 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); + dmService = AndroidAPIUtils.getDeviceManagementService(); } finally { PrivilegedCarbonContext.endTenantFlow(); } DeviceIdentifier deviceIdentifier = AndroidAPIUtils.convertToDeviceIdentifierObject(id); try { - if(dmService!=null){ + if (dmService != null) { result = dmService.disenrollDevice(deviceIdentifier); status = 1; - }else{ + } else { status = -1; - msg = "Device Manager service not available"; + msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE; } } catch (DeviceManagementException e) { msg = "Error occurred while disenrolling the device"; @@ -199,12 +179,12 @@ public class Enrollment { switch (status) { case 1: if (result) { - return Response.status(200).entity(result).build(); + return Response.status(HttpStatus.SC_OK).entity(result).build(); } break; case -1: - return Response.status(500).entity(msg).build(); + return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(404).entity("Device not found").build(); + return Response.status(HttpStatus.SC_NOT_FOUND).entity("Device not found").build(); } } diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtils.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtils.java index 94391d40ae..7d75214702 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtils.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtils.java @@ -19,7 +19,10 @@ package cdm.api.android.util; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.util.*; @@ -112,4 +115,16 @@ public class AndroidAPIUtils { identifier.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); return identifier; } + + + public static DeviceManagementService getDeviceManagementService() { + DeviceManagementService dmService; + 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); + return dmService; + } } 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 index b76820fc52..0dcb9e59de 100644 --- 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 @@ -45,4 +45,12 @@ public final class AndroidConstants { public static final String DEVICE_PROPERTIES_KEY = "properties"; public static final String DEVICE_FEATURES_KEY = "features"; } + + public final class Messages{ + private Messages(){ + throw new AssertionError(); + } + public static final String DEVICE_MANAGER_SERVICE_NOT_AVAILABLE = + "Device Manager service not available"; + } }