From 20fd2ec7680cfa0f009e3b023e74a9b8e4bf9ef7 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 30 May 2016 17:46:37 +0530 Subject: [PATCH] Improving application installation/uninstallation handling --- .../device/mgt/jaxrs/beans/ActivityList.java | 1 - .../ApplicationManagementAdminService.java | 21 ++++--- ...ApplicationManagementAdminServiceImpl.java | 56 +++++++++++++++---- .../impl/util/RequestValidationUtil.java | 39 +++++++++++++ 4 files changed, 97 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java index 68240514ee5..18eb30c080d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ActivityList.java @@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import java.util.ArrayList; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java index df5c1220435..c7e2c1a26aa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/ApplicationManagementAdminService.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import javax.ws.rs.Consumes; import javax.ws.rs.POST; @@ -50,11 +51,12 @@ public interface ApplicationManagementAdminService { tags = "Application Management Administrative Service") @ApiResponses(value = { @ApiResponse( - code = 200, - message = "OK. \n Install application operations have been successfully scheduled upon given devices"), + code = 202, + message = "OK. \n Install application operation will be delivered to the given devices"), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Resource to be processed does not exist."), @@ -65,7 +67,8 @@ public interface ApplicationManagementAdminService { code = 500, message = "Internal Server ErrorResponse. \n " + "Server error occurred while bulk issuing application installation operations upon " + - "a given set of devices.") + "a given set of devices.", + response = ErrorResponse.class) }) Response installApplication( @ApiParam( @@ -84,11 +87,12 @@ public interface ApplicationManagementAdminService { tags = "Application Management Administrative Service") @ApiResponses(value = { @ApiResponse( - code = 200, - message = "OK. \n Uninstall application operations have been successfully scheduled upon given devices"), + code = 202, + message = "OK. \n Uninstall application operation will be delivered to the provided devices"), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Resource to be processed does not exist."), @@ -99,7 +103,8 @@ public interface ApplicationManagementAdminService { code = 500, message = "Internal Server ErrorResponse. \n " + "Server error occurred while bulk issuing application un-installation operations upon " + - "a given set of devices.") + "a given set of devices.", + response = ErrorResponse.class) }) Response uninstallApplication( @ApiParam( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java index 51d17a9770d..e669c73dcbc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java @@ -25,8 +25,12 @@ import org.wso2.carbon.device.mgt.common.Platform; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.exception.UnknownApplicationTypeException; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.ApplicationManagementAdminService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.MDMAndroidOperationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.MDMIOSOperationUtil; @@ -53,30 +57,45 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem public Response installApplication(ApplicationWrapper applicationWrapper) { ApplicationManager appManagerConnector; Operation operation = null; + + RequestValidationUtil.validateApplicationInstallationContext(applicationWrapper); try { appManagerConnector = DeviceMgtAPIUtils.getAppManagementService(); MobileApp mobileApp = applicationWrapper.getApplication(); if (applicationWrapper.getDeviceIdentifiers() != null) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { - if (deviceIdentifier.getType().equals(Platform.ANDROID.toString())) { + if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp); - } else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { + } else if (Platform.IOS.toString().equals(deviceIdentifier.getType())) { operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp); } } - appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); + if (applicationWrapper.getRoleNameList() != null && applicationWrapper.getRoleNameList().size() > 0) { + appManagerConnector.installApplicationForUserRoles(operation, applicationWrapper.getRoleNameList()); + } else if (applicationWrapper.getUserNameList() != null && + applicationWrapper.getUserNameList().size() > 0) { + appManagerConnector.installApplicationForUsers(operation, applicationWrapper.getUserNameList()); + } else if (applicationWrapper.getDeviceIdentifiers() != null && + applicationWrapper.getDeviceIdentifiers().size() > 0) { + appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); + } else { + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( + "No application installation criteria i.e. user/role/device is given").build()); + } } return Response.status(Response.Status.ACCEPTED).entity("Application installation request has been sent " + - "to the device").build(); + "to the device(s)").build(); } catch (ApplicationManagementException e) { String msg = "ErrorResponse occurred while processing application installation request"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } catch (UnknownApplicationTypeException e) { String msg = "The type of application requested to be installed is not supported"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -86,30 +105,45 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem public Response uninstallApplication(ApplicationWrapper applicationWrapper) { ApplicationManager appManagerConnector; org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation = null; + + RequestValidationUtil.validateApplicationInstallationContext(applicationWrapper); try { appManagerConnector = DeviceMgtAPIUtils.getAppManagementService(); MobileApp mobileApp = applicationWrapper.getApplication(); if (applicationWrapper.getDeviceIdentifiers() != null) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { - if (deviceIdentifier.getType().equals(Platform.ANDROID.toString())) { + if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp); } else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp); } } - appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); + if (applicationWrapper.getRoleNameList() != null && applicationWrapper.getRoleNameList().size() > 0) { + appManagerConnector.installApplicationForUserRoles(operation, applicationWrapper.getRoleNameList()); + } else if (applicationWrapper.getUserNameList() != null && + applicationWrapper.getUserNameList().size() > 0) { + appManagerConnector.installApplicationForUsers(operation, applicationWrapper.getUserNameList()); + } else if (applicationWrapper.getDeviceIdentifiers() != null && + applicationWrapper.getDeviceIdentifiers().size() > 0) { + appManagerConnector.installApplicationForDevices(operation, applicationWrapper.getDeviceIdentifiers()); + } else { + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( + "No application un-installation criteria i.e. user/role/device is given").build()); + } } return Response.status(Response.Status.ACCEPTED).entity("Application un-installation request has " + - "been sent to the device").build(); + "been sent to the given device(s)").build(); } catch (ApplicationManagementException e) { String msg = "ErrorResponse occurred while processing application un-installation request"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } catch (UnknownApplicationTypeException e) { String msg = "The type of application requested to be un-installed is not supported"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index 53da08ff526..5d06388c131 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; +import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import java.util.ArrayList; @@ -207,4 +208,42 @@ public class RequestValidationUtil { } } + public static void validateApplicationInstallationContext(ApplicationWrapper installationCtx) { + int count = 0; + + if (installationCtx.getDeviceIdentifiers() != null && installationCtx.getDeviceIdentifiers().size() > 0) { + count++; + } + if (installationCtx.getUserNameList() != null && installationCtx.getUserNameList().size() > 0) { + count++; + } + if (installationCtx.getRoleNameList() != null && installationCtx.getRoleNameList().size() > 0) { + count++; + } + if (count > 1) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("The incoming request has " + + "more than one application installation criteria defined").build()); + } + } + + public static void validateApplicationUninstallationContext(ApplicationWrapper installationCtx) { + int count = 0; + + if (installationCtx.getDeviceIdentifiers() != null && installationCtx.getDeviceIdentifiers().size() > 0) { + count++; + } + if (installationCtx.getUserNameList() != null && installationCtx.getUserNameList().size() > 0) { + count++; + } + if (installationCtx.getRoleNameList() != null && installationCtx.getRoleNameList().size() > 0) { + count++; + } + if (count > 1) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("The incoming request has " + + "more than one application un-installation criteria defined").build()); + } + } + }