From cba9f7ce080de366543b594be3b13206296db85a Mon Sep 17 00:00:00 2001 From: nipuni Date: Tue, 15 Oct 2024 19:04:43 +0530 Subject: [PATCH] Fix the validation issue when the APK file and screenshot names contain spaces --- .../application/mgt/core/util/APIUtil.java | 45 ++++++++++--------- .../application/mgt/core/util/Constants.java | 5 +++ 2 files changed, 29 insertions(+), 21 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/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 4a0687052a..2ac54352fe 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 @@ -48,6 +48,8 @@ import org.apache.commons.validator.routines.UrlValidator; import org.wso2.carbon.context.PrivilegedCarbonContext; import javax.ws.rs.core.Response; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -503,31 +505,29 @@ public class APIUtil { applicationRelease.setSupportedOsVersions(applicationReleaseDTO.getSupportedOsVersions()); applicationRelease.setRating(applicationReleaseDTO.getRating()); applicationRelease.setIconPath( - basePath + Constants.ICON_ARTIFACT + Constants.FORWARD_SLASH + applicationReleaseDTO.getIconName()); - - if (!StringUtils.isEmpty(applicationReleaseDTO.getBannerName())){ + basePath + Constants.ICON_ARTIFACT + Constants.FILE_NAME_PARAM + + URLEncoder.encode(applicationReleaseDTO.getIconName(), StandardCharsets.UTF_8)); + if (!StringUtils.isEmpty(applicationReleaseDTO.getBannerName())) { applicationRelease.setBannerPath( - basePath + Constants.BANNER_ARTIFACT + Constants.FORWARD_SLASH + applicationReleaseDTO - .getBannerName()); + basePath + Constants.BANNER_ARTIFACT + Constants.FILE_NAME_PARAM + + URLEncoder.encode(applicationReleaseDTO.getBannerName(), StandardCharsets.UTF_8)); } - - applicationRelease.setInstallerPath(constructInstallerPath(applicationReleaseDTO.getInstallerName(), - applicationReleaseDTO.getAppHashValue())); - + applicationRelease.setInstallerPath( + constructInstallerPath(applicationReleaseDTO.getInstallerName(), applicationReleaseDTO.getAppHashValue())); if (!StringUtils.isEmpty(applicationReleaseDTO.getScreenshotName1())) { - screenshotPaths - .add(basePath + Constants.SCREENSHOT_ARTIFACT + 1 + Constants.FORWARD_SLASH + applicationReleaseDTO - .getScreenshotName1()); + screenshotPaths.add( + basePath + Constants.SCREENSHOT_ARTIFACT + 1 + Constants.FILE_NAME_PARAM + + URLEncoder.encode(applicationReleaseDTO.getScreenshotName1(), StandardCharsets.UTF_8)); } if (!StringUtils.isEmpty(applicationReleaseDTO.getScreenshotName2())) { - screenshotPaths - .add(basePath + Constants.SCREENSHOT_ARTIFACT + 2 + Constants.FORWARD_SLASH + applicationReleaseDTO - .getScreenshotName2()); + screenshotPaths.add( + basePath + Constants.SCREENSHOT_ARTIFACT + 2 + Constants.FILE_NAME_PARAM + + URLEncoder.encode(applicationReleaseDTO.getScreenshotName2(), StandardCharsets.UTF_8)); } if (!StringUtils.isEmpty(applicationReleaseDTO.getScreenshotName3())) { - screenshotPaths - .add(basePath + Constants.SCREENSHOT_ARTIFACT + 3 + Constants.FORWARD_SLASH + applicationReleaseDTO - .getScreenshotName3()); + screenshotPaths.add( + basePath + Constants.SCREENSHOT_ARTIFACT + 3 + Constants.FILE_NAME_PARAM + + URLEncoder.encode(applicationReleaseDTO.getScreenshotName3(), StandardCharsets.UTF_8)); } applicationRelease.setScreenshots(screenshotPaths); return applicationRelease; @@ -543,9 +543,12 @@ public class APIUtil { 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; + String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + + appHash + Constants.FORWARD_SLASH; + return urlValidator.isValid(installerName) + ? installerName + : basePath + Constants.APP_ARTIFACT + Constants.FILE_NAME_PARAM + + URLEncoder.encode(installerName, StandardCharsets.UTF_8); } public static String getArtifactDownloadBaseURL() throws ApplicationManagementException { 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/Constants.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/Constants.java index 81e5bef788..57ed6b7189 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/Constants.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/Constants.java @@ -140,6 +140,11 @@ public class Constants { public static final String DB_TYPE_POSTGRESQL = "PostgreSQL"; } + /** + * Query parameter for specifying the filename in the App artifact URL. + */ + public static final String FILE_NAME_PARAM = "?fileName="; + /** * Directory name of the icon artifact that are saved in the file system. */