From 29b2c41c7358b876b8adbc509adcfa79b88608a7 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Wed, 12 Jun 2019 16:28:04 +0530 Subject: [PATCH] Add iOS enterprise app install --- .../mgt/api/services/ArtifactDownloadAPI.java | 29 ++++++++++++ .../impl/ArtifactDownloadAPIImpl.java | 21 +++++++++ .../common/services/ApplicationManager.java | 9 ++++ .../mgt/core/impl/ApplicationManagerImpl.java | 44 +++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 15 +++++++ .../device/mgt/common/MDMAppConstants.java | 1 + .../mgt/core/util/MDMIOSOperationUtil.java | 3 -- 7 files changed, 119 insertions(+), 3 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java index 7a7f4a74ee6..f6d221792b8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java @@ -92,4 +92,33 @@ public interface ArtifactDownloadAPI { required = true) @PathParam("fileName") String fileName); + @GET + @Path("/plist/{uuid}") + @Produces(MediaType.TEXT_XML) + @ApiOperation( + produces = MediaType.TEXT_XML, + httpMethod = "GET", + value = "Get plist artifact content of an application", + notes = "Get plist artifact content of an application" + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved plist artifact content.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. Plist artifact content not found for the application."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while retrieving plist artifact content.", + response = ErrorResponse.class) + }) + Response getPlistArtifact( + @ApiParam( + name = "uuid", + value = "UUID of the application release.", + required = true) + @PathParam("uuid") String uuid); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java index 98ae97ed80b..23fd133f596 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; @@ -72,4 +73,24 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + + @GET + @Override + @Produces(MediaType.TEXT_XML) + @Path("/plist/{uuid}") + public Response getPlistArtifact(@PathParam("uuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + String plistContent = applicationManager.getPlistArtifact(uuid); + return Response.status(Response.Status.OK).entity(plistContent).build(); + } catch (NotFoundException e) { + String msg = "Couldn't find an application release for UUID: " + uuid; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while getting the application plist artifact file."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 7f34760d233..ffc0620581d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -258,4 +258,13 @@ public interface ApplicationManager { String getInstallableLifecycleState() throws ApplicationManagementException; + /** + * Get plist content to download and install the application. + * + * @param uuid Release UUID of the application. + * @return plist string + * @throws ApplicationManagementException Application management exception + */ + String getPlistArtifact(String uuid) throws ApplicationManagementException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 9cfe93dc38f..0282768a8d1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.validator.routines.UrlValidator; import org.apache.cxf.jaxrs.ext.multipart.Attachment; @@ -2603,4 +2604,47 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new UnexpectedServerErrorException(msg); } } + + public String getPlistArtifact(String releaseUuid) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getApplicationByUUID(releaseUuid, tenantId); + if (applicationDTO == null) { + String msg = "Couldn't find application for the release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); + } + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + String artifactDownloadEndpoint = ConfigurationManager.getInstance().getConfiguration() + .getArtifactDownloadEndpoint(); + String artifactDownloadURL = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid() + + Constants.FORWARD_SLASH + applicationReleaseDTO.getInstallerName(); + String plistContent = "<!DOCTYPE plist PUBLIC "-//Apple//DTDPLIST1.0//EN" "" + + "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="" + + "1.0"><dict><key>items</key><array><dict><" + + "key>assets</key><array><dict><key>kind</key><" + + "string>software-package</string><key>url</key><string>" + + "$downloadURL</string></dict></array><key>metadata<" + + "/key><dict><key>bundle-identifier</key><string>" + + "$packageName</string><key>bundle-version</key><string>" + + "$bundleVersion</string><key>kind</key><string>" + + "software</string><key>title</key><string>$appName<" + + "/string></dict></dict></array></dict></plist>"; + plistContent = plistContent.replace("$downloadURL", artifactDownloadURL) + .replace("$packageName", applicationReleaseDTO.getPackageName()) + .replace("$bundleVersion", applicationReleaseDTO.getVersion()) + .replace("$appName", applicationDTO.getName()); + return StringEscapeUtils.unescapeXml(plistContent); + } catch (DBConnectionException e) { + throw new ApplicationManagementException( + "Error occurred while obtaining the database connection for getting application for the release UUID: " + + releaseUuid, e); + } catch (ApplicationManagementDAOException e) { + throw new ApplicationManagementException( + "Error occurred while getting application data for release UUID: " + releaseUuid, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } 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 479d809d365..dc7a80be1a3 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 @@ -35,6 +35,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManageme import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.response.Application; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; +import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; @@ -46,9 +47,11 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.HelperUtil; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.MDMAppConstants; import org.wso2.carbon.device.mgt.common.app.mgt.MobileApp; import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; @@ -70,6 +73,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.stream.Collectors; /** @@ -474,6 +478,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { + String artifactDownloadEndpoint = ConfigurationManager.getInstance().getConfiguration() + .getArtifactDownloadEndpoint(); + String plistDownloadEndpoint = artifactDownloadEndpoint + Constants.FORWARD_SLASH + + MDMAppConstants.IOSConstants.PLIST + Constants.FORWARD_SLASH + + application.getApplicationReleases().get(0).getUuid(); + mobileApp.setType(mobileAppType); + mobileApp.setLocation(plistDownloadEndpoint); + Properties properties = new Properties(); + properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); + properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); + mobileApp.setProperties(properties); return MDMIOSOperationUtil.createInstallAppOperation(mobileApp); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { return MDMIOSOperationUtil.createAppUninstallOperation(mobileApp); 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 8dc62592a46..dbb4a97e337 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 @@ -32,6 +32,7 @@ public class MDMAppConstants { public static final String IS_PREVENT_BACKUP = "isPreventBackup"; public static final String I_TUNES_ID = "iTunesId"; public static final String LABEL = "label"; + public static final String PLIST = "plist"; public static final String OPCODE_INSTALL_ENTERPRISE_APPLICATION = "INSTALL_ENTERPRISE_APPLICATION"; public static final String OPCODE_INSTALL_STORE_APPLICATION = "INSTALL_STORE_APPLICATION"; public static final String OPCODE_INSTALL_WEB_APPLICATION = "WEB_CLIP"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMIOSOperationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMIOSOperationUtil.java index c057a01de7b..fb14f72ed6c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMIOSOperationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMIOSOperationUtil.java @@ -48,10 +48,7 @@ public class MDMIOSOperationUtil { case ENTERPRISE: EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); - enterpriseApplication.setBundleId(application.getId()); - enterpriseApplication.setIdentifier(application.getIdentifier()); enterpriseApplication.setManifestURL(application.getLocation()); - Properties properties = application.getProperties(); enterpriseApplication.setPreventBackupOfAppData((Boolean) properties. get(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP));