From 545ca7e2a234328f0f8d89a88b0268c2d8b6b69c Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 23 Sep 2019 23:36:34 +0530 Subject: [PATCH] Changing status related APIs to honour tenant ID --- .../service/api/DeviceManagementService.java | 59 ++++++++++++------- .../impl/DeviceManagementServiceImpl.java | 39 ++++++++---- .../DeviceManagementProviderService.java | 6 +- .../DeviceManagementProviderServiceImpl.java | 11 ++-- 4 files changed, 75 insertions(+), 40 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index aba6d9819ce..1aa36ca78f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -1627,7 +1627,7 @@ public interface DeviceManagementService { @Valid OperationRequest operationRequest); @GET - @Path("/status/count/{type}/{status}") + @Path("/status/count/{tenantDomain}/{type}/{status}") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -1653,16 +1653,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), + " of the requested resource.\n"), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -1674,10 +1674,17 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", + "Server error occurred while retrieving the device details.", response = ErrorResponse.class) }) Response getDeviceCountByStatus( + @ApiParam( + name = "tenantDomain", + value = "The tenant doamin.", + required = true) + @PathParam("tenantDomain") + @Size(max = 45) + String tenantDomain, @ApiParam( name = "type", value = "The device type name, such as ios, android, windows or fire-alarm.", @@ -1695,7 +1702,7 @@ public interface DeviceManagementService { @GET - @Path("/status/ids/{type}/{status}") + @Path("/status/ids/{tenantDomain}/{type}/{status}") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -1722,16 +1729,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest version" + - " of the requested resource.\n"), + " of the requested resource.\n"), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -1743,10 +1750,17 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving the device details.", + "Server error occurred while retrieving the device details.", response = ErrorResponse.class) }) Response getDeviceIdentifiersByStatus( + @ApiParam( + name = "tenantDomain", + value = "The tenant domain.", + required = true) + @PathParam("tenantDomain") + @Size(max = 45) + String tenantDomain, @ApiParam( name = "type", value = "The device type name, such as ios, android, windows or fire-alarm.", @@ -1763,7 +1777,7 @@ public interface DeviceManagementService { String status); @PUT - @Path("/status/update/{type}/{status}") + @Path("/status/update/{tenantDomain}/{type}/{status}") @ApiOperation( produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON, @@ -1790,16 +1804,16 @@ public interface DeviceManagementService { @ResponseHeader( name = "ETag", description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", description = "Date and time the resource has been modified the last time.\n" + - "Used by caches, or in conditional requests."), + "Used by caches, or in conditional requests."), }), @ApiResponse( code = 304, message = "Not Modified. Empty body because the client already has the latest " + - "version of the requested resource."), + "version of the requested resource."), @ApiResponse( code = 400, message = "Bad Request. \n Invalid request or validation error.", @@ -1811,13 +1825,16 @@ public interface DeviceManagementService { @ApiResponse( code = 500, message = "Internal Server Error. \n " + - "Server error occurred while retrieving information requested device.", + "Server error occurred while retrieving information requested device.", response = ErrorResponse.class) }) - Response bulkUpdateDeviceStatus(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true) - @PathParam("type") String type, - @ApiParam(name = "status", value = "The device type, such as ios, android or windows.", required = true) - @PathParam("status") String status, - @ApiParam(name = "deviceList", value = "The payload containing the new name of the device.", required = true) - @Valid List deviceList); + Response bulkUpdateDeviceStatus( + @ApiParam(name = "tenantDomain", value = "The tenant domain.", required = true) + @PathParam("tenantDomain") String tenantDomain, + @ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true) + @PathParam("type") String type, + @ApiParam(name = "status", value = "The device type, such as ios, android or windows.", required = true) + @PathParam("status") String status, + @ApiParam(name = "deviceList", value = "The payload containing the new name of the device.", required = true) + @Valid List deviceList); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 91770d28ea7..2cc710d7389 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -84,6 +84,7 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import javax.validation.Valid; @@ -918,48 +919,66 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @GET @Override - @Path("/status/count/{type}/{status}") - public Response getDeviceCountByStatus(@PathParam("type") String type, @PathParam("status") String status) { + @Path("/status/count/{tenantDomain}/{type}/{status}") + public Response getDeviceCountByStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, @PathParam("status") String status) { int deviceCount; try { - deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCountOfTypeByStatus(type, status); + int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); + deviceCount = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceCountOfTypeByStatus(tenantId, type, status); return Response.status(Response.Status.OK).entity(deviceCount).build(); } catch (DeviceManagementException e) { String errorMessage = "Error while retrieving device count."; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + } catch (UserStoreException e) { + String errorMessage = "Error resolving tenant Domain"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } } @GET @Override - @Path("/status/ids/{type}/{status}") - public Response getDeviceIdentifiersByStatus(@PathParam("type") String type, @PathParam("status") String status) { + @Path("/status/ids/{tenantDomain}/{type}/{status}") + public Response getDeviceIdentifiersByStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, @PathParam("status") String status) { List deviceIds; try { - deviceIds = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceIdentifiersByStatus(type, status); + int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); + deviceIds = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceIdentifiersByStatus(tenantId, type, status); return Response.status(Response.Status.OK).entity(deviceIds.toArray(new String[0])).build(); } catch (DeviceManagementException e) { String errorMessage = "Error while obtaining list of devices"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + } catch (UserStoreException e) { + String errorMessage = "Error resolving tenant Domain"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } } @PUT @Override - @Path("/status/update/{type}/{status}") - public Response bulkUpdateDeviceStatus(@PathParam("type") String type, @PathParam("status") String status, - @Valid List deviceList) { + @Path("/status/update/{tenantDomain}/{type}/{status}") + public Response bulkUpdateDeviceStatus(@PathParam("tenantDomain") String tenantDomain, @PathParam("type") String type, + @PathParam("status") String status, @Valid List deviceList) { try { - DeviceMgtAPIUtils.getDeviceManagementService().bulkUpdateDeviceStatus(type, deviceList, status); + int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); + DeviceMgtAPIUtils.getDeviceManagementService().bulkUpdateDeviceStatus(tenantId, type, deviceList, status); } catch (DeviceManagementException e) { String errorMessage = "Error while updating device status in bulk."; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + } catch (UserStoreException e) { + String errorMessage = "Error resolving tenant Domain"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); } return Response.status(Response.Status.OK).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 3b2012ac637..a9e3ed3d547 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 @@ -726,11 +726,11 @@ public interface DeviceManagementProviderService { List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength) throws DeviceManagementException; - int getDeviceCountOfTypeByStatus(String deviceType, String deviceStatus) throws DeviceManagementException; + int getDeviceCountOfTypeByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException; - List getDeviceIdentifiersByStatus(String deviceType, String deviceStatus) throws DeviceManagementException; + List getDeviceIdentifiersByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException; - boolean bulkUpdateDeviceStatus(String deviceType, List deviceList, String status) throws DeviceManagementException; + boolean bulkUpdateDeviceStatus(int tenantId, String deviceType, List deviceList, String status) throws DeviceManagementException; boolean updateEnrollment(String owner, List deviceIdentifiers) throws DeviceManagementException, UserNotFoundException, InvalidDeviceException; 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 7fec5a63337..b7109f55dec 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 @@ -3035,8 +3035,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public int getDeviceCountOfTypeByStatus(String deviceType, String deviceStatus) throws DeviceManagementException { - int tenantId = this.getTenantId(); + public int getDeviceCountOfTypeByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException { try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceCount(deviceType, deviceStatus, tenantId); @@ -3054,8 +3053,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDeviceIdentifiersByStatus(String deviceType, String deviceStatus) throws DeviceManagementException { - int tenantId = this.getTenantId(); + public List getDeviceIdentifiersByStatus(int tenantId, String deviceType, String deviceStatus) throws DeviceManagementException { List deviceIds; try { DeviceManagementDAOFactory.openConnection(); @@ -3075,8 +3073,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean bulkUpdateDeviceStatus(String deviceType, List deviceList, String status) throws DeviceManagementException { - int tenantId = this.getTenantId(); + public boolean bulkUpdateDeviceStatus(int tenantId, String deviceType, + List deviceList, String status) + throws DeviceManagementException { boolean success; try { DeviceManagementDAOFactory.openConnection();