From 45db043902ddb7057f5eae2d732e845ec0b3308c Mon Sep 17 00:00:00 2001 From: ashvini Date: Thu, 22 Feb 2024 23:26:53 +0530 Subject: [PATCH] Refactor code --- .../mgt/core/util/ApplicationManagementUtil.java | 9 +++++---- .../mgt/core/application/mgt/core/util/Constants.java | 6 ++++++ 2 files changed, 11 insertions(+), 4 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/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 3216ea7dbf..e8bd05c81a 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 @@ -544,9 +544,9 @@ public class ApplicationManagementUtil { * @return Sanitized and shortened file name */ public static String sanitizeName(String originalName, String type) { - String sanitizedName = originalName.replaceAll("[^a-zA-Z0-9.\\s-]", ""); - if (type.equals(Constants.ApplicationProperties.NAME) && sanitizedName.length() > 350) { - sanitizedName = sanitizedName.substring(0, 350); + String sanitizedName = originalName.replaceAll(Constants.APP_NAME_REGEX, ""); + if (Constants.ApplicationProperties.NAME.equals(type) && sanitizedName.length() > Constants.MAX_APP_NAME_CHARACTERS) { + sanitizedName = sanitizedName.substring(0, Constants.MAX_APP_NAME_CHARACTERS); return sanitizedName; } else if (Constants.ICON_NAME.equals(type) || Constants.SCREENSHOT_NAME.equals(type)) { // Shortening icon/screenshot names @@ -556,7 +556,8 @@ public class ApplicationManagementUtil { fileExtension = originalName.substring(dotIndex); } return type + fileExtension; - } else + } else { return sanitizedName; + } } } 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 767b5e2bd2..b96fd66058 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 @@ -214,4 +214,10 @@ public class Constants { public static final String ASSOCIATION_DEVICE = "ASSOCIATION_DEVICE"; public static final String ASSOCIATION_USER = "ASSOCIATION_USER"; } + + /** + * App name sanitization related constants + */ + public static final int MAX_APP_NAME_CHARACTERS = 350; + public static final String APP_NAME_REGEX = "[^a-zA-Z0-9.\\s-]"; }