From 426287fdc18303c7c0e2e509146751bc51f5f3fc Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Thu, 19 Dec 2019 11:44:32 +0530 Subject: [PATCH] Update service to check device type validation at service level and return a Not Found request, if device type doesn't exist --- .../DeviceTypeManagementAdminService.java | 6 +++--- .../DeviceTypeManagementAdminServiceImpl.java | 21 ++++++++++++------- .../DeviceManagementProviderService.java | 5 +++-- .../DeviceManagementProviderServiceImpl.java | 19 +++++++---------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java index efeaa393633..22432abe94c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java @@ -594,7 +594,7 @@ public interface DeviceTypeManagementAdminService { @DELETE - @Path("{deviceType}/delete") + @Path("{deviceTypeName}/delete") @ApiOperation( httpMethod = "DELETE", value = "Delete device type.", @@ -628,8 +628,8 @@ public interface DeviceTypeManagementAdminService { }) Response deleteDeviceType( @ApiParam( - name = "deviceType", + name = "deviceTypeName", value = "Device type name.", required = true) - @PathParam("deviceType") String deviceType); + @PathParam("deviceTypeName") String deviceTypeName); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java index e3bedfd816b..909f998d527 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java @@ -320,20 +320,27 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen @Override @DELETE - @Path("{deviceType}/delete") - public Response deleteDeviceType(@PathParam("deviceType") String deviceType) { + @Path("{deviceTypeName}/delete") + public Response deleteDeviceType(@PathParam("deviceTypeName") String deviceTypeName) { try { DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); - if (!deviceManagementProviderService.deleteDeviceType(deviceType)){ - String msg = "Error occurred while deleting device of type: " + deviceType; + DeviceType deviceType = deviceManagementProviderService.getDeviceType(deviceTypeName); + if (deviceType == null) { + String msg = "Error, device of type: " + deviceTypeName + " does not exist"; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } + if (!deviceManagementProviderService.deleteDeviceType(deviceTypeName, deviceType)){ + String msg = "Error occurred while deleting device of type: " + deviceTypeName; log.error(msg); return Response.serverError().entity(msg).build(); } - return Response.status(Response.Status.ACCEPTED).entity( - "Device of type: " + deviceType + " permanently deleted.").build(); + return Response.status(Response.Status.ACCEPTED) + .entity("Device of type: " + deviceTypeName + " permanently deleted.") + .build(); } catch (DeviceManagementException e) { - String msg = "Error occurred while deleting device of type: " + deviceType; + String msg = "Error occurred while deleting device of type: " + deviceTypeName; log.error(msg, e); return Response.serverError().entity(msg).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 9246d2fc974..20873499405 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -775,11 +775,12 @@ public interface DeviceManagementProviderService { /** * Permanently delete a device type with all it's devices * - * @param deviceTypeName device type name + * @param deviceTypeName Device type name + * @param deviceType Device type object * @return True if device type successfully removed * @throws DeviceManagementException Will be thrown if any service level or DAO level error occurs */ - boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException; + boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType) throws DeviceManagementException; /** * Retrieves a list of configurations of a specific device diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index b7190c03aa9..0fb4f086a14 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -3589,25 +3589,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException { - int tenantId = getTenantId(); + public boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType) + throws DeviceManagementException { List deviceIdentifiers; List deviceTypeVersions; boolean result; + int tenantId = getTenantId(); try { - if (deviceTypeName.isEmpty()) { + if (deviceTypeName.isEmpty() || deviceType == null) { String msg = "Error, device type cannot be null or empty"; log.error(msg); return false; } DeviceManagementDAOFactory.beginTransaction(); - DeviceType deviceTypeObj = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId); - if (deviceTypeObj == null) { - String msg = "Error, device of type: " + deviceTypeName + " does not exist"; - log.error(msg); - return false; - } List devices = deviceDAO.getDevices(deviceTypeName, this.getTenantId()); if (devices.isEmpty()) { if (log.isDebugEnabled()) { @@ -3637,14 +3632,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); } // remove device type versions - deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions(deviceTypeObj.getId(), deviceTypeName); + deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions(deviceType.getId(), deviceTypeName); if (deviceTypeVersions.isEmpty()) { if (log.isDebugEnabled()) { log.debug("Device of type: " + deviceTypeName + "doesn't have any type versions"); } } else { for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) { - result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceTypeObj.getId() + result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId() , deviceTypeVersion.getVersionName(), tenantId); if (!result) { String msg = "Device type of: " + deviceTypeName + "is unauthorized to modify " + @@ -3662,7 +3657,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } // delete device type - deviceTypeDAO.deleteDeviceType(tenantId, deviceTypeObj.getId()); + deviceTypeDAO.deleteDeviceType(tenantId, deviceType.getId()); DeviceManagementDAOFactory.commitTransaction(); } catch (InvalidDeviceException e) { String msg = "Error occurred while deleting devices of type: " + deviceTypeName;