From bd021ff8357cfd47c311d2f54ec3ffc51eeaebce Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 5 Apr 2024 14:41:35 +0530 Subject: [PATCH 1/2] Add Content and Package URI changes --- .../mgt/core/impl/ApplicationManagerImpl.java | 1 + .../application/mgt/core/util/APIUtil.java | 25 +++++--- .../core/util/ApplicationManagementUtil.java | 58 +++++++++++++++++++ .../management/ApplicationManagementTest.java | 3 +- 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index 3daa959aee..cfa8404375 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -509,6 +509,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); throw new ApplicationManagementException(msg, e); } + ApplicationManagementUtil.addInstallerPathToMetadata(releaseDTO); applicationDTO.getApplicationReleaseDTOs().clear(); applicationDTO.getApplicationReleaseDTOs().add(releaseDTO); return applicationDTO; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.java index 789ac3de0a..7c2034eb11 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/APIUtil.java @@ -496,7 +496,6 @@ public class APIUtil { List screenshotPaths = new ArrayList<>(); ApplicationRelease applicationRelease = new ApplicationRelease(); - UrlValidator urlValidator = new UrlValidator(); applicationRelease.setDescription(applicationReleaseDTO.getDescription()); applicationRelease.setVersion(applicationReleaseDTO.getVersion()); @@ -519,13 +518,8 @@ public class APIUtil { .getBannerName()); } - if (urlValidator.isValid(applicationReleaseDTO.getInstallerName())) { - applicationRelease.setInstallerPath(applicationReleaseDTO.getInstallerName()); - } else { - applicationRelease.setInstallerPath( - basePath + Constants.APP_ARTIFACT + Constants.FORWARD_SLASH + applicationReleaseDTO - .getInstallerName()); - } + applicationRelease.setInstallerPath(constructInstallerPath(applicationReleaseDTO.getInstallerName(), + applicationReleaseDTO.getAppHashValue())); if (!StringUtils.isEmpty(applicationReleaseDTO.getScreenshotName1())) { screenshotPaths @@ -546,6 +540,21 @@ public class APIUtil { return applicationRelease; } + /** + * Construct installer path + * @param installerName Installer name + * @param appHash Application hash + * @return Constructed installer path value + * @throws ApplicationManagementException Throws when error encountered while constructing installer path + */ + public static String constructInstallerPath(String installerName, String appHash) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + UrlValidator urlValidator = new UrlValidator(); + String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + appHash + Constants.FORWARD_SLASH; + return urlValidator.isValid(installerName) ? installerName + : basePath + Constants.APP_ARTIFACT + Constants.FORWARD_SLASH + installerName; + } + public static String getArtifactDownloadBaseURL() throws ApplicationManagementException { String host = System.getProperty(Constants.IOT_CORE_HOST); MDMConfig mdmConfig = ConfigurationManager.getInstance().getConfiguration().getMdmConfig(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java index b3b2d65f0a..390e706d8e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java @@ -17,11 +17,16 @@ */ package io.entgra.device.mgt.core.application.mgt.core.util; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact; import io.entgra.device.mgt.core.application.mgt.common.FileDataHolder; import io.entgra.device.mgt.core.application.mgt.common.FileDescriptor; import io.entgra.device.mgt.core.application.mgt.common.LifecycleChanger; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ItuneAppDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.FileDownloaderServiceException; @@ -60,6 +65,7 @@ import io.entgra.device.mgt.core.device.mgt.core.common.util.FileUtil; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.MetadataManagementServiceImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.NotNull; import java.io.ByteArrayOutputStream; import java.io.File; @@ -80,6 +86,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; /** @@ -649,4 +656,55 @@ public class ApplicationManagementUtil { } return releaseWrappers; } + + /** + * Add installer path metadata value to windows applications + * @param applicationReleaseDTO {@link ApplicationReleaseDTO} + * @throws ApplicationManagementException Throws when error encountered while updating the app metadata + */ + public static void addInstallerPathToMetadata(ApplicationReleaseDTO applicationReleaseDTO) + throws ApplicationManagementException { + if (applicationReleaseDTO.getMetaData() == null) return; + Gson gson = new Gson(); + String installerPath = APIUtil.constructInstallerPath(applicationReleaseDTO.getInstallerName(), applicationReleaseDTO.getAppHashValue()); + String[] fileNameSegments = extractNameSegments(applicationReleaseDTO, installerPath); + String extension = fileNameSegments[fileNameSegments.length - 1]; + if (!Objects.equals(extension, "appx") && !Objects.equals(extension, "msi")) { + return; + } + + String installerPaths = "[ {" + + "\"key\": \"Content_Uri\", " + + "\"value\" : \"" + installerPath + "\"" + + "}]"; + + if (Objects.equals(extension, "appx")) { + installerPaths = "[ {" + + "\"key\": \"Package_Uri\", " + + "\"value\" : \"" + installerPath + "\"" + + "}]"; + } + + JsonArray parsedMetadataList = gson.fromJson(applicationReleaseDTO.getMetaData(), JsonArray.class); + JsonArray installerPathsArray = gson.fromJson(installerPaths, JsonArray.class); + parsedMetadataList.addAll(installerPathsArray); + applicationReleaseDTO.setMetaData(gson.toJson(parsedMetadataList)); + } + + private static String[] extractNameSegments(ApplicationReleaseDTO applicationReleaseDTO, String installerPath) + throws ApplicationManagementException { + String []installerPathSegments = installerPath.split("/"); + if (installerPathSegments.length == 0) { + throw new ApplicationManagementException("Received malformed url for installer path of the app : " + + applicationReleaseDTO.getInstallerName()); + } + String fullQualifiedName = installerPathSegments[installerPathSegments.length - 1]; + String []fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)"); + if (fileNameSegments.length != 2) { + throw new ApplicationManagementException("Received malformed url for installer path of the app : " + + applicationReleaseDTO.getInstallerName()); + } + return fileNameSegments; + } + } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java index e78b6196d0..2e37d49410 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java @@ -107,7 +107,7 @@ public class ApplicationManagementTest extends BaseTestCase { EntAppReleaseWrapper releaseWrapper = new EntAppReleaseWrapper(); releaseWrapper.setDescription("First release"); releaseWrapper.setIsSharedWithAllTenants(false); - releaseWrapper.setMetaData("Just meta data"); + releaseWrapper.setMetaData("[{\"key\": \"Just a metadata\"}]"); releaseWrapper.setReleaseType("free"); releaseWrapper.setPrice(5.7); releaseWrapper.setSupportedOsVersions("4.0-7.0"); @@ -174,6 +174,7 @@ public class ApplicationManagementTest extends BaseTestCase { releaseWrapper.setArtifactLink(apkTransferLink.getDirectTransferLink() + "/sample.apk"); releaseWrapper.setRemoteStatus(false); + entAppReleaseWrappers.add(releaseWrapper); applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers); From 49577411739da88f1dca068ed470ba550ec3688b Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 5 Apr 2024 15:28:50 +0530 Subject: [PATCH 2/2] Refactored the code --- .../mgt/core/util/ApplicationManagementUtil.java | 7 +++++++ .../mgt/core/management/ApplicationManagementTest.java | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java index 390e706d8e..b8a5d98596 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/ApplicationManagementUtil.java @@ -691,6 +691,13 @@ public class ApplicationManagementUtil { applicationReleaseDTO.setMetaData(gson.toJson(parsedMetadataList)); } + /** + * Extract name segments from installer path + * @param applicationReleaseDTO {@link ApplicationReleaseDTO} + * @param installerPath Installer path + * @return Extracted file name segments + * @throws ApplicationManagementException Throws when error encountered while extracting name segments from installer path + */ private static String[] extractNameSegments(ApplicationReleaseDTO applicationReleaseDTO, String installerPath) throws ApplicationManagementException { String []installerPathSegments = installerPath.split("/"); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java index 2e37d49410..793b28d0f9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/test/java/io/entgra/device/mgt/core/application/mgt/core/management/ApplicationManagementTest.java @@ -174,7 +174,6 @@ public class ApplicationManagementTest extends BaseTestCase { releaseWrapper.setArtifactLink(apkTransferLink.getDirectTransferLink() + "/sample.apk"); releaseWrapper.setRemoteStatus(false); - entAppReleaseWrappers.add(releaseWrapper); applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers);