From 15edc3852cadd388085b50e06436e70dc8355aaf Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Thu, 23 Jun 2022 12:46:35 +0530 Subject: [PATCH] Add Windows app uninstallation feature --- .../core/impl/SubscriptionManagerImpl.java | 10 ++- .../device/mgt/common/MDMAppConstants.java | 1 + .../core/util/MDMWindowsOperationUtil.java | 78 ++++++++++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index cdd18c5678..aaded6c76d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1129,9 +1129,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { return MDMWindowsOperationUtil.createInstallAppOperation(app); } else { - String msg = "Invalid Action is found. Action: " + action; - log.error(msg); - throw new ApplicationManagementException(msg); + if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + return MDMWindowsOperationUtil.createUninstallAppOperation(app); + } else { + String msg = "Invalid Action is found. Action: " + action; + log.error(msg); + throw new ApplicationManagementException(msg); + } } } else { String msg = "Invalid device type is found. Device Type: " + deviceType; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java index 40b647d6ea..d3a90cfcc8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java @@ -57,6 +57,7 @@ public class MDMAppConstants { throw new AssertionError(); } public static final String INSTALL_ENTERPRISE_APPLICATION = "INSTALL_ENTERPRISE_APPLICATION"; + public static final String UNINSTALL_ENTERPRISE_APPLICATION = "UNINSTALL_ENTERPRISE_APPLICATION"; //App type constants related to window device type public static final String MSI = "MSI"; public static final String APPX = "APPX"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java index 48645bbc8d..18436e048d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMWindowsOperationUtil.java @@ -45,7 +45,7 @@ public class MDMWindowsOperationUtil { private static final Log log = LogFactory.getLog(MDMWindowsOperationUtil.class); /** - * This method is used to create Install Authentication operation. + * This method is used to create Install Application operation. * * @param application MobileApp application * @return operation object @@ -120,6 +120,82 @@ public class MDMWindowsOperationUtil { return operation; } + /** + * This method is used to create Uninstall Application operation. + * + * @param application MobileApp application + * @return operation object + * @throws UnknownApplicationTypeException + */ + public static Operation createUninstallAppOperation(App application) throws UnknownApplicationTypeException { + + ProfileOperation operation = new ProfileOperation(); + operation.setCode(MDMAppConstants.WindowsConstants.UNINSTALL_ENTERPRISE_APPLICATION); + operation.setType(Operation.Type.PROFILE); + String appType = windowsAppType(application.getName()); + String metaData = application.getMetaData(); + JsonArray metaJsonArray = jsonStringToArray(metaData); + + switch (application.getType()) { + case ENTERPRISE: + EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); + if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.APPX)) { + HostedAppxApplication hostedAppxApplication = new HostedAppxApplication(); + List dependencyPackageList = new ArrayList<>(); + for (int i = 0; i < metaJsonArray.size(); i++) { + JsonElement metaElement = metaJsonArray.get(i); + JsonObject metaObject = metaElement.getAsJsonObject(); + + if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_URI.equals(metaObject.get("key").getAsString())) { + hostedAppxApplication.setPackageUri(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_PACKAGE_FAMILY_NAME.equals(metaObject.get("key").getAsString())) { + hostedAppxApplication.setPackageFamilyName(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_DEPENDENCY_PACKAGE_URL.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + dependencyPackageList.add(metaObject.get("value").getAsString().trim()); + hostedAppxApplication.setDependencyPackageUri(dependencyPackageList); + } + else if (MDMAppConstants.WindowsConstants.APPX_CERTIFICATE_HASH.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + hostedAppxApplication.setCertificateHash(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.APPX_ENCODED_CERT_CONTENT.equals(metaObject.get("key").getAsString()) + && metaObject.has("value")) { + hostedAppxApplication.setEncodedCertificate(metaObject.get("value").getAsString().trim()); + } + } + enterpriseApplication.setHostedAppxApplication(hostedAppxApplication); + + } else if (appType.equalsIgnoreCase(MDMAppConstants.WindowsConstants.MSI)) { + HostedMSIApplication hostedMSIApplication = new HostedMSIApplication(); + for (int i = 0; i < metaJsonArray.size(); i++) { + JsonElement metaElement = metaJsonArray.get(i); + JsonObject metaObject = metaElement.getAsJsonObject(); + if (MDMAppConstants.WindowsConstants.MSI_PRODUCT_ID.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setProductId(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.MSI_CONTENT_URI.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setContentUrl(metaObject.get("value").getAsString().trim()); + } + else if (MDMAppConstants.WindowsConstants.MSI_FILE_HASH.equals(metaObject.get("key").getAsString())) { + hostedMSIApplication.setFileHash(metaObject.get("value").getAsString().trim()); + } + } + enterpriseApplication.setHostedMSIApplication(hostedMSIApplication); + } + operation.setPayLoad(enterpriseApplication.toJSON()); + break; + default: + String msg = "Application type " + application.getType() + " is not supported"; + log.error(msg); + throw new UnknownApplicationTypeException(msg); + } + + return operation; + } + /** * Method to get the installer file extension type for windows type apps(either appx or msi) *