From 2eb73213f3137aed5620f2de66b19da3ca67d2a1 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 25 May 2024 07:00:05 +0530 Subject: [PATCH] Add requested changes --- .../mgt/core/impl/ApplicationManagerImpl.java | 50 ++++++++++--------- .../impl/ApplicationStorageManagerImpl.java | 1 - .../core/impl/FileTransferServiceImpl.java | 10 +++- .../util/FileTransferServiceHelperUtil.java | 3 +- 4 files changed, 36 insertions(+), 28 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 60b61dab93..f560d8ce82 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 @@ -727,19 +727,20 @@ public class ApplicationManagerImpl implements ApplicationManager { * @throws ResourceManagementException if error occurred while uploading */ private ApplicationReleaseDTO uploadCustomAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, - String deviceType) + String deviceType) throws ResourceManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - String md5OfApp = applicationStorageManager.getMD5(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath()))); + String md5OfApp = applicationStorageManager. + getMD5(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath()))); validateReleaseBinaryFileHash(md5OfApp); releaseDTO.setUuid(UUID.randomUUID().toString()); releaseDTO.setAppHashValue(md5OfApp); releaseDTO.setInstallerName(applicationArtifact.getInstallerName()); - applicationStorageManager.uploadReleaseArtifact(releaseDTO, deviceType, - Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); + applicationStorageManager.uploadReleaseArtifact(releaseDTO, deviceType, + Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); } catch (IOException e) { String msg = "Error occurred when uploading release artifact into the server"; log.error(msg); @@ -748,6 +749,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String msg = "Error occurred while md5sum value retrieving process: application UUID " + releaseDTO.getUuid(); log.error(msg, e); + throw new ApplicationManagementException(msg, e); } return addImageArtifacts(releaseDTO, applicationArtifact, tenantId); } @@ -896,7 +898,8 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new ApplicationManagementException(msg); } - String md5OfApp = applicationStorageManager.getMD5(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath()))); + String md5OfApp = applicationStorageManager. + getMD5(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath()))); if (md5OfApp == null) { String msg = "Error occurred while md5sum value retrieving process: application UUID " + applicationReleaseDTO.getUuid(); @@ -910,10 +913,9 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new BadRequestException(msg); } applicationReleaseDTO.setAppHashValue(md5OfApp); - - applicationStorageManager - .uploadReleaseArtifact(applicationReleaseDTO, deviceType, - Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); + applicationStorageManager + .uploadReleaseArtifact(applicationReleaseDTO, deviceType, + Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); } catch (StorageManagementException e) { String msg = "Error occurred while md5sum value retrieving process: application UUID " + applicationReleaseDTO.getUuid(); @@ -969,9 +971,9 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!applicationReleaseDTO.getAppHashValue().equals(md5OfApp)) { applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); - ApplicationInstaller applicationInstaller = applicationStorageManager - .getAppInstallerData(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), deviceType); - String packageName = applicationInstaller.getPackageName(); + ApplicationInstaller applicationInstaller = applicationStorageManager + .getAppInstallerData(Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), deviceType); + String packageName = applicationInstaller.getPackageName(); try { ConnectionManagerUtil.getDBConnection(); @@ -981,13 +983,13 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new BadRequestException(msg); } - if (applicationReleaseDTO.getPackageName() == null){ + if (applicationReleaseDTO.getPackageName() == null) { String msg = "Found null value for application release package name for application " + "release which has UUID: " + applicationReleaseDTO.getUuid(); log.error(msg); throw new ApplicationManagementException(msg); } - if (!applicationReleaseDTO.getPackageName().equals(packageName)){ + if (!applicationReleaseDTO.getPackageName().equals(packageName)) { String msg = "Package name of the new artifact does not match with the package name of " + "the exiting application release. Package name of the existing app release " + applicationReleaseDTO.getPackageName() + " and package name of the new " @@ -1000,11 +1002,11 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setPackageName(packageName); String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); applicationReleaseDTO.setAppHashValue(md5OfApp); - applicationStorageManager.uploadReleaseArtifact(applicationReleaseDTO, deviceType, - Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), - tenantId); - applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, - applicationReleaseDTO, tenantId); + applicationStorageManager.uploadReleaseArtifact(applicationReleaseDTO, deviceType, + Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), + tenantId); + applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, + applicationReleaseDTO, tenantId); } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection for verifying application " + "release existing for new app hash value."; @@ -3625,11 +3627,11 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.get().setInstallerName(applicationArtifact.getInstallerName()); String deletingAppHashValue = applicationReleaseDTO.get().getAppHashValue(); applicationReleaseDTO.get().setAppHashValue(md5OfApp); - applicationStorageManager. - uploadReleaseArtifact(applicationReleaseDTO.get(), deviceTypeObj.getName(), - Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); - applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, - applicationReleaseDTO.get(), tenantId); + applicationStorageManager. + uploadReleaseArtifact(applicationReleaseDTO.get(), deviceTypeObj.getName(), + Files.newInputStream(Paths.get(applicationArtifact.getInstallerPath())), tenantId); + applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, + applicationReleaseDTO.get(), tenantId); } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection for verifying application" + " release existing for new app hash value."; 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/ApplicationStorageManagerImpl.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/ApplicationStorageManagerImpl.java index 167ebd84dc..ad1906ede1 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/ApplicationStorageManagerImpl.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/ApplicationStorageManagerImpl.java @@ -156,7 +156,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager public void uploadReleaseArtifact(ApplicationReleaseDTO applicationReleaseDTO, String deviceType, InputStream binaryFile, int tenantId) throws ResourceManagementException { try { - //byte [] content = IOUtils.toByteArray(binaryFile); String artifactDirectoryPath = storagePath + tenantId + File.separator + applicationReleaseDTO.getAppHashValue() + File.separator + Constants.APP_ARTIFACT; 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/FileTransferServiceImpl.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/FileTransferServiceImpl.java index 52789308fd..8af3324c67 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/FileTransferServiceImpl.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/FileTransferServiceImpl.java @@ -31,6 +31,7 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.NotFoundException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.FileSystems; @@ -103,8 +104,13 @@ public class FileTransferServiceImpl implements FileTransferService { @Override public boolean isExistsOnLocal(URL downloadUrl) throws FileTransferServiceException { try { - return FileTransferServiceHelperUtil.resolve(downloadUrl) != null; - } catch (FileTransferServiceHelperUtilException e) { + FileDescriptor fileDescriptor = FileTransferServiceHelperUtil.resolve(downloadUrl); + if (fileDescriptor != null && fileDescriptor.getFile() != null) { + fileDescriptor.getFile().close(); + return true; + } + return false; + } catch (FileTransferServiceHelperUtilException | IOException e) { String msg = "Error occurred while checking the existence of artifact on the local environment"; log.error(msg, e); throw new FileTransferServiceException(msg, e); 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/FileTransferServiceHelperUtil.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/FileTransferServiceHelperUtil.java index 1a44848b87..856faf4944 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/FileTransferServiceHelperUtil.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/FileTransferServiceHelperUtil.java @@ -244,9 +244,10 @@ public class FileTransferServiceHelperUtil { } private static FileDescriptor resolve(String []urlSegments) throws FileTransferServiceHelperUtilException { + // check the possibility of url is pointing to a file resides in the default storage path if (urlSegments.length < 4) { if (log.isDebugEnabled()) { - log.debug("URL path segments contain less than 2 segments"); + log.debug("URL path segments contain less than 4 segments"); } return null; }