From d9a04e099e748814b2f54d2c324521931681ef3e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Tue, 8 Oct 2019 16:48:00 +0530 Subject: [PATCH] Improve APPM app saving functionality --- .../services/ApplicationStorageManager.java | 73 +++++++++----- .../mgt/core/impl/ApplicationManagerImpl.java | 95 ++++++++++--------- .../impl/ApplicationStorageManagerImpl.java | 59 ++++++------ .../mgt/core/impl/AppmDataHandlerImpl.java | 2 +- .../ApplicationManagementPublisherAPI.java | 2 +- 5 files changed, 132 insertions(+), 99 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 24879c0e6da..1d79db95a87 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -28,65 +28,88 @@ import java.io.InputStream; import java.util.List; /** - * This manages all the storage related requirements of ApplicationDTO. + * This manages all the storage related requirements of Application. */ public interface ApplicationStorageManager { + /** - * To upload image artifacts related with an ApplicationDTO. + * To upload image artifacts related with an Application. * - * @param applicationRelease ApplicationReleaseDTO Object - * @param iconFile Icon File input stream - * @param bannerFile Banner File input stream - * @throws ResourceManagementException Resource Management Exception. + * @param applicationRelease Application Release Object + * @param iconFile InputStream of the icon file. + * @param bannerFile InputStream of the banner file. + * @return {@link ApplicationReleaseDTO} + * @throws ResourceManagementException if it finds an empty screenshot array or IOException throws while handling + * input streams. */ - ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationRelease, - InputStream iconFile, InputStream bannerFile, List screenshots) throws ResourceManagementException; + ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFile, + InputStream bannerFile, List screenshots, int tenantId) throws ResourceManagementException; + /** + * To get App Installer data such as version, package name etc. + * + * @param binaryFile Binary file of the application. + * @param deviceType Compatible device type of the application. + * @return {@link ApplicationInstaller} + * @throws ApplicationStorageManagementException if device type is incorrect or error occurred while parsing binary + * data. + */ ApplicationInstaller getAppInstallerData(InputStream binaryFile, String deviceType) throws ApplicationStorageManagementException; - /** * To upload release artifacts for an Application. * - * @param applicationRelease ApplicationDTO Release Object. + * @param applicationRelease Application Release Object. * @param deviceType Compatible device type of the application. * @param binaryFile Binary File for the release. + * @param tenantId Tenant Id * @throws ResourceManagementException if IO Exception occured while saving the release artifacts in the server. */ - void uploadReleaseArtifact(ApplicationReleaseDTO applicationRelease, String deviceType, InputStream binaryFile) - throws ResourceManagementException; + void uploadReleaseArtifact(ApplicationReleaseDTO applicationRelease, String deviceType, InputStream binaryFile, + int tenantId) throws ResourceManagementException; /** - * To upload release artifacts for an ApplicationDTO. + * To upload release artifacts for an Application. * - * @param applicationReleaseDTO applicationRelease ApplicationDTO release of a particular application. + * @param applicationReleaseDTO application Release of a particular application. * @param deletingAppHashValue Hash value of the deleting application release. - * @throws ApplicationStorageManagementException Resource Management Exception. + * @param tenantId Tenant Id + * @throws ApplicationStorageManagementException if IO Exception occurs while copying image artifacts and deleting + * application release installer file. */ void copyImageArtifactsAndDeleteInstaller(String deletingAppHashValue, - ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationStorageManagementException; + ApplicationReleaseDTO applicationReleaseDTO, int tenantId) throws ApplicationStorageManagementException; /** - * To delete the artifacts related with particular ApplicationDTO Release. + * To delete the artifacts related with particular Application Release. * - * @throws ApplicationStorageManagementException Not Found Exception. + * @param appReleaseHashVal Hash value of the application release. + * @param folderName Folder name of the application stored. + * @param fileName Name of the application release artifact. + * @throws ApplicationStorageManagementException if artifact doesn't exist. */ - void deleteAppReleaseArtifact(String appReleaseHashVal, String folderName, String fileName) throws ApplicationStorageManagementException; + void deleteAppReleaseArtifact(String appReleaseHashVal, String folderName, String fileName, int tenantId) + throws ApplicationStorageManagementException; /** - * To delete all release artifacts related with particular ApplicationDTO Release. + * To delete all release artifacts related with particular Application Release. * - * @param directoryPaths Hash values of the ApplicationDTO. - * @throws ApplicationStorageManagementException ApplicationDTO Storage Management Exception + * @param directoryPaths Hash values of the Application. + * @param tenantId Tenant Id + * @throws ApplicationStorageManagementException if artifact doesn't exist or IO exception occurred while deleting + * application artifact. */ - void deleteAllApplicationReleaseArtifacts(List directoryPaths) throws ApplicationStorageManagementException; + void deleteAllApplicationReleaseArtifacts(List directoryPaths, int tenantId) + throws ApplicationStorageManagementException; - /*** + /** * Get the InputStream of the file which is located in filePath + * * @param hashVal Hash Value of the application release. * @return {@link InputStream} * @throws ApplicationStorageManagementException throws if an error occurs when accessing the file. */ - InputStream getFileStream(String hashVal, String folderName, String fileName) throws ApplicationStorageManagementException; + InputStream getFileStream(String hashVal, String folderName, String fileName, int tenantId) + throws ApplicationStorageManagementException; } 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 f75b604b97b..e4b186bd3ab 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 @@ -139,14 +139,15 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Ent. Application create request is received. Application name: " + applicationWrapper.getName() + " Device type: " + applicationWrapper.getDeviceType()); } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(applicationWrapper); //uploading application artifacts ApplicationReleaseDTO applicationReleaseDTO = uploadEntAppReleaseArtifacts( applicationDTO.getApplicationReleaseDTOs().get(0), applicationArtifact, - applicationWrapper.getDeviceType(), false); + applicationWrapper.getDeviceType(), tenantId, false); applicationDTO.getApplicationReleaseDTOs().clear(); applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); - return addAppDataIntoDB(applicationDTO); + return addAppDataIntoDB(applicationDTO, tenantId); } @Override @@ -156,6 +157,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Web clip create request is received. App name: " + webAppWrapper.getName() + " Device type: " + Constants.ANY); } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(webAppWrapper); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); applicationReleaseDTO.setUuid(UUID.randomUUID().toString()); @@ -163,14 +165,15 @@ public class ApplicationManagerImpl implements ApplicationManager { //uploading application artifacts try { applicationDTO.getApplicationReleaseDTOs().clear(); - applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact)); + applicationDTO.getApplicationReleaseDTOs() + .add(addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId)); } catch (ResourceManagementException e) { String msg = "Error Occured when uploading artifacts of the web clip: " + webAppWrapper.getName(); log.error(msg); throw new ApplicationManagementException(msg, e); } //insert application data into database - return addAppDataIntoDB(applicationDTO); + return addAppDataIntoDB(applicationDTO, tenantId); } @Override @@ -184,7 +187,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } String publicAppStorePath = ""; - if (DeviceTypes.ANDROID.toString().toLowerCase().equals(publicAppWrapper.getDeviceType())) { + if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(publicAppWrapper.getDeviceType())) { publicAppStorePath = Constants.GOOGLE_PLAY_STORE_URL; } else if (DeviceTypes.IOS.toString().equals(publicAppWrapper.getDeviceType())) { publicAppStorePath = Constants.APPLE_STORE_URL; @@ -210,13 +213,13 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.closeDBConnection(); } - if (exitingRelease != null && exitingRelease.size() > 0) { + if (exitingRelease != null && !exitingRelease.isEmpty()) { applicationDTO.getApplicationReleaseDTOs().clear(); applicationReleaseDTO.setUuid(exitingRelease.get(0).getUuid()); applicationReleaseDTO.setCurrentState(exitingRelease.get(0).getCurrentState()); try { - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); ConnectionManagerUtil.beginDBTransaction(); applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId); @@ -237,7 +240,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } else { //uploading application artifacts try { - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); applicationDTO.getApplicationReleaseDTOs().clear(); applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); } catch (ResourceManagementException e) { @@ -246,7 +249,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException(msg, e); } //insert application data into database - return addAppDataIntoDB(applicationDTO); + return addAppDataIntoDB(applicationDTO, tenantId); } } @@ -290,16 +293,16 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { applicationStorageManager.uploadReleaseArtifact(applicationReleaseDTO, customAppWrapper.getDeviceType(), - binaryDuplicate); + binaryDuplicate, tenantId); } catch (IOException e) { String msg = "Error occurred when uploading release artifact into the server."; log.error(msg); throw new ApplicationManagementException(msg); } - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); applicationDTO.getApplicationReleaseDTOs().clear(); applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); - return addAppDataIntoDB(applicationDTO); + return addAppDataIntoDB(applicationDTO, tenantId); } catch (ResourceManagementException e) { String msg = "Error occurred while uploading application artifact into the server. Application name: " + customAppWrapper.getName() + " Device type: " + customAppWrapper.getDeviceType(); @@ -313,10 +316,10 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - private void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException { + private void deleteApplicationArtifacts(List directoryPaths, int tenantId) throws ApplicationManagementException { ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); try { - applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths); + applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths, tenantId); } catch (ApplicationStorageManagementException e) { String msg = "Error occurred when deleting application artifacts. directory paths: ." + directoryPaths .toString(); @@ -374,7 +377,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { applicationStorageManager - .uploadReleaseArtifact(applicationReleaseDTO, deviceType, binaryDuplicate); + .uploadReleaseArtifact(applicationReleaseDTO, deviceType, binaryDuplicate, tenantId); } } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection for verifying app release data."; @@ -455,9 +458,10 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setAppHashValue(md5OfApp); try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { applicationStorageManager - .uploadReleaseArtifact(applicationReleaseDTO, deviceType, binaryDuplicate); + .uploadReleaseArtifact(applicationReleaseDTO, deviceType, binaryDuplicate, + tenantId); applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, - applicationReleaseDTO); + applicationReleaseDTO, tenantId); } } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection for verifying application " @@ -486,7 +490,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } private ApplicationReleaseDTO addImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, - ApplicationArtifact applicationArtifact) throws ResourceManagementException { + ApplicationArtifact applicationArtifact, int tenantId) throws ResourceManagementException { ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); @@ -510,24 +514,24 @@ public class ApplicationManagerImpl implements ApplicationManager { // Upload images applicationReleaseDTO = applicationStorageManager .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), - applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values())); + applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values()), tenantId); return applicationReleaseDTO; } private ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, - ApplicationArtifact applicationArtifact) throws ResourceManagementException{ + ApplicationArtifact applicationArtifact, int tenantId) throws ResourceManagementException{ ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); if (!StringUtils.isEmpty(applicationArtifact.getIconName())) { applicationStorageManager .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), Constants.ICON_ARTIFACT, - applicationReleaseDTO.getIconName()); + applicationReleaseDTO.getIconName(), tenantId); applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); } if (!StringUtils.isEmpty(applicationArtifact.getBannerName())){ applicationStorageManager .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), Constants.BANNER_ARTIFACT, - applicationReleaseDTO.getBannerName()); + applicationReleaseDTO.getBannerName(), tenantId); applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); } @@ -544,17 +548,17 @@ public class ApplicationManagerImpl implements ApplicationManager { if (counter == 1) { applicationStorageManager .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, - applicationReleaseDTO.getScreenshotName1()); + applicationReleaseDTO.getScreenshotName1(), tenantId); applicationReleaseDTO.setScreenshotName1(scName); } else if (counter == 2) { applicationStorageManager .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, - applicationReleaseDTO.getScreenshotName2()); + applicationReleaseDTO.getScreenshotName2(), tenantId); applicationReleaseDTO.setScreenshotName2(scName); } else if (counter == 3) { applicationStorageManager .deleteAppReleaseArtifact(applicationReleaseDTO.getAppHashValue(), folderPath, - applicationReleaseDTO.getScreenshotName3()); + applicationReleaseDTO.getScreenshotName3(), tenantId); applicationReleaseDTO.setScreenshotName3(scName); } counter++; @@ -564,7 +568,7 @@ public class ApplicationManagerImpl implements ApplicationManager { // Upload images applicationReleaseDTO = applicationStorageManager .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), - applicationArtifact.getBannerStream(), screenshotStreams); + applicationArtifact.getBannerStream(), screenshotStreams, tenantId); return applicationReleaseDTO; } @@ -694,11 +698,11 @@ public class ApplicationManagerImpl implements ApplicationManager { * required to do the validation of request and check the existence of application releaseDTO. * * @param applicationDTO Application DTO object. + * @param tenantId Tenant Id * @return {@link Application} * @throws ApplicationManagementException which throws if error occurs while during application management. */ - private Application addAppDataIntoDB(ApplicationDTO applicationDTO) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + private Application addAppDataIntoDB(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementException { ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); List unrestrictedRoles = applicationDTO.getUnrestrictedRoles(); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); @@ -712,7 +716,8 @@ public class ApplicationManagerImpl implements ApplicationManager { if (appId == -1) { log.error("Application data storing is Failed."); ConnectionManagerUtil.rollbackDBTransaction(); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), + tenantId); return null; } else { if (log.isDebugEnabled()) { @@ -783,7 +788,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); try { applicationStorageManager.deleteAllApplicationReleaseArtifacts( - Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); } catch (ApplicationStorageManagementException ex) { String errorLog = "Error occurred when deleting application artifacts. Application artifacts are tried to " @@ -797,7 +802,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String msg = "Error occurred while adding application or application release. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } catch (LifecycleManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -805,7 +810,7 @@ public class ApplicationManagerImpl implements ApplicationManager { "Error occurred when getting initial lifecycle state. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } catch (DBConnectionException e) { String msg = "Error occurred while getting database connection."; @@ -816,7 +821,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String msg = "Error occurred while adding unrestricted roles. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while disabling AutoCommit."; @@ -853,7 +858,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } ApplicationReleaseDTO applicationReleaseDTO = uploadEntAppReleaseArtifacts( APIUtil.releaseWrapperToReleaseDTO(entAppReleaseWrapper), applicationArtifact, deviceType.getName(), - true); + tenantId, true); try { ConnectionManagerUtil.beginDBTransaction(); String initialstate = lifecycleStateManager.getInitialState(); @@ -918,12 +923,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } private ApplicationReleaseDTO uploadEntAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, - ApplicationArtifact applicationArtifact, String deviceTypeName, boolean isNewRelease) + ApplicationArtifact applicationArtifact, String deviceTypeName, int tenantId, boolean isNewRelease) throws ApplicationManagementException { try { ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(deviceTypeName, releaseDTO, applicationArtifact, isNewRelease); - return addImageArtifacts(applicationReleaseDTO, applicationArtifact); + return addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); } catch (ResourceManagementException e) { String msg = "Error occurred while uploading application release artifacts."; log.error(msg, e); @@ -1243,7 +1248,7 @@ public class ApplicationManagerImpl implements ApplicationManager { deletingAppHashVals.add(applicationReleaseDTO.getAppHashValue()); deletingAppReleaseIds.add(applicationReleaseDTO.getId()); } - applicationStorageManager.deleteAllApplicationReleaseArtifacts(deletingAppHashVals); + applicationStorageManager.deleteAllApplicationReleaseArtifacts(deletingAppHashVals, tenantId); this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds); this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds); this.applicationDAO.deleteApplicationTags(applicationId, tenantId); @@ -1355,7 +1360,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ForbiddenException(msg); } applicationStorageManager.deleteAllApplicationReleaseArtifacts( - Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); ConnectionManagerUtil.commitDBTransaction(); @@ -1412,7 +1417,7 @@ public class ApplicationManagerImpl implements ApplicationManager { + " into updatable state and retry the operation."); } applicationReleaseDTO = this.applicationReleaseDAO - .updateRelease(updateImageArtifacts(applicationReleaseDTO, applicationArtifact), tenantId); + .updateRelease(updateImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId), tenantId); if (applicationReleaseDTO == null) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Application release updating count is 0 for application release UUID: " + uuid; @@ -2396,7 +2401,7 @@ public class ApplicationManagerImpl implements ApplicationManager { .set(updateEntAppReleaseArtifact(deviceTypeObj.getName(), applicationReleaseDTO.get(), applicationArtifact)); } - applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact)); + applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact, tenantId)); boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO.get(), tenantId) != null; if (!updateStatus) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -2463,7 +2468,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.get().setMetaData(publicAppReleaseWrapper.getMetaData()); } - applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact)); + applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact, tenantId)); boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO.get(), tenantId) != null; if (!updateStatus) { @@ -2528,7 +2533,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.get().setMetaData(webAppReleaseWrapper.getMetaData()); } - applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact)); + applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact, tenantId)); boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO.get(), tenantId) != null; if (!updateStatus) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -2625,9 +2630,9 @@ public class ApplicationManagerImpl implements ApplicationManager { try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { applicationStorageManager .uploadReleaseArtifact(applicationReleaseDTO.get(), deviceTypeObj.getName(), - binaryDuplicate); + binaryDuplicate, tenantId); applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, - applicationReleaseDTO.get()); + applicationReleaseDTO.get(), tenantId); } } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection for verifying application" @@ -2652,7 +2657,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationStorageManagementException(msg, e); } } - applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact)); + applicationReleaseDTO.set(updateImageArtifacts(applicationReleaseDTO.get(), applicationArtifact, tenantId)); boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO.get(), tenantId) != null; if (!updateStatus) { ConnectionManagerUtil.rollbackDBTransaction(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 64802261430..69aa1711ac5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -59,14 +59,15 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, - InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) + InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams, int tenantId) throws ResourceManagementException { String iconStoredLocation; String bannerStoredLocation; String scStoredLocation = null; try { - String artifactStoringBaseDirPath = storagePath + applicationReleaseDTO.getAppHashValue(); + String artifactStoringBaseDirPath = + storagePath + tenantId + File.separator + applicationReleaseDTO.getAppHashValue(); StorageManagementUtil.createArtifactDirectory(artifactStoringBaseDirPath); if (iconFileStream != null) { @@ -144,11 +145,12 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void uploadReleaseArtifact(ApplicationReleaseDTO applicationReleaseDTO, - String deviceType, InputStream binaryFile) throws ResourceManagementException { + String deviceType, InputStream binaryFile, int tenantId) throws ResourceManagementException { try { byte [] content = IOUtils.toByteArray(binaryFile); String artifactDirectoryPath = - storagePath + applicationReleaseDTO.getAppHashValue() + File.separator + Constants.APP_ARTIFACT; + storagePath + tenantId + File.separator + applicationReleaseDTO.getAppHashValue() + File.separator + + Constants.APP_ARTIFACT; StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); String artifactPath = artifactDirectoryPath + File.separator + applicationReleaseDTO.getInstallerName(); saveFile(new ByteArrayInputStream(content), artifactPath); @@ -162,7 +164,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void copyImageArtifactsAndDeleteInstaller(String deletingAppHashValue, - ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationStorageManagementException { + ApplicationReleaseDTO applicationReleaseDTO, int tenantId) throws ApplicationStorageManagementException { try { String appHashValue = applicationReleaseDTO.getAppHashValue(); @@ -171,42 +173,41 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager String screenshot1 = applicationReleaseDTO.getScreenshotName1(); String screenshot2 = applicationReleaseDTO.getScreenshotName2(); String screenshot3 = applicationReleaseDTO.getScreenshotName3(); + String basePath = storagePath + tenantId + File.separator; if (bannerName != null) { - StorageManagementUtil - .copy(storagePath + deletingAppHashValue + File.separator + Constants.BANNER_ARTIFACT - + File.separator + bannerName, - storagePath + appHashValue + File.separator + Constants.BANNER_ARTIFACT + File.separator - + bannerName); + StorageManagementUtil.copy(basePath + deletingAppHashValue + File.separator + Constants.BANNER_ARTIFACT + + File.separator + bannerName, + basePath + appHashValue + File.separator + Constants.BANNER_ARTIFACT + File.separator + + bannerName); } if (iconName != null) { - StorageManagementUtil.copy(storagePath + deletingAppHashValue + File.separator + Constants.ICON_ARTIFACT + StorageManagementUtil.copy(basePath + deletingAppHashValue + File.separator + Constants.ICON_ARTIFACT + File.separator + iconName, - storagePath + appHashValue + File.separator + Constants.ICON_ARTIFACT + File.separator - + iconName); + basePath + appHashValue + File.separator + Constants.ICON_ARTIFACT + File.separator + iconName); } if (screenshot1 != null) { StorageManagementUtil - .copy(storagePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 1 + .copy(basePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 1 + File.separator + screenshot1, - storagePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 1 + basePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 1 + File.separator + screenshot1); } if (screenshot2 != null) { StorageManagementUtil - .copy(storagePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 2 + .copy(basePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 2 + File.separator + screenshot2, - storagePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 2 + basePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 2 + File.separator + screenshot2); } if (screenshot3 != null) { StorageManagementUtil - .copy(storagePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 3 + .copy(basePath + deletingAppHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 3 + File.separator + screenshot3, - storagePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 3 + basePath + appHashValue + File.separator + Constants.SCREENSHOT_ARTIFACT + 3 + File.separator + screenshot3); } - deleteAppReleaseArtifact( storagePath + deletingAppHashValue); + deleteAppReleaseArtifact( basePath + deletingAppHashValue); } catch (IOException e) { String msg = "Application installer updating is failed because of I/O issue"; log.error(msg, e); @@ -217,24 +218,28 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override - public void deleteAppReleaseArtifact(String appReleaseHashVal, String folderName, String fileName) + public void deleteAppReleaseArtifact(String appReleaseHashVal, String folderName, String fileName, int tenantId) throws ApplicationStorageManagementException { - String artifactPath = storagePath + appReleaseHashVal + File.separator + folderName + File.separator + fileName; + String artifactPath = storagePath + tenantId + File.separator + appReleaseHashVal + File.separator + folderName + + File.separator + fileName; deleteAppReleaseArtifact(artifactPath); } @Override - public void deleteAllApplicationReleaseArtifacts(List directoryPaths) + public void deleteAllApplicationReleaseArtifacts(List directoryPaths, int tenantId) throws ApplicationStorageManagementException { - for (String directoryBasePath : directoryPaths) { - deleteAppReleaseArtifact(storagePath + directoryBasePath); + String basePath = storagePath + tenantId + File.separator; + for (String directoryPath : directoryPaths) { + deleteAppReleaseArtifact(basePath + directoryPath); } } @Override - public InputStream getFileStream(String hashVal, String folderName, String fileName) + public InputStream getFileStream(String hashVal, String folderName, String fileName, int tenantId) throws ApplicationStorageManagementException { - String filePath = storagePath + hashVal + File.separator + folderName + File.separator + fileName; + String filePath = + storagePath + tenantId + File.separator + hashVal + File.separator + folderName + File.separator + + fileName; try { return StorageManagementUtil.getInputStream(filePath); } catch (IOException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index d1c941e0453..95b747a3f47 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -74,7 +74,7 @@ public class AppmDataHandlerImpl implements AppmDataHandler { throw new NotFoundException(msg); } InputStream inputStream = applicationStorageManager - .getFileStream(appReleaseHashValue, folderName, artifactName); + .getFileStream(appReleaseHashValue, folderName, artifactName, tenantId); if (inputStream == null) { String msg = "Couldn't file the file in the file system."; log.error(msg); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java index aade01bfea4..a89aeb37660 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java @@ -840,7 +840,7 @@ public interface ApplicationManagementPublisherAPI { @Multipart(value = "screenshot3") Attachment screenshot3); @PUT - @Path("/pub-app-release/{uuid}") + @Path("/public-app-release/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation(