diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java index 1b44fb9e7f..a8ae90ff58 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java @@ -1,4 +1,5 @@ -package org.wso2.carbon.device.application.mgt.common;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. +package org.wso2.carbon.device.application.mgt.common; +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index eb5f11a022..1a9cc58b6c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -172,19 +172,16 @@ public interface ApplicationManager { /*** * - * @param applicationId ID of the application - * @param releaseUuid UUID of the application release - * @param deviceType Supported device type of the application - * @param applicationRelease {@link ApplicationReleaseDTO} - * @param binaryFileStram {@link InputStream} of the binary file - * @param iconFileStream {@link InputStream} of the icon - * @param bannerFileStream {@link InputStream} of the banner - * @param attachments {@link List} of {@link InputStream} of attachments + * @param deviceType Device type which is supported for the Application. + * @param applicationType Application Type + * @param releaseUuid UUID of the application release. + * @param applicationReleaseWrapper {@link ApplicationReleaseDTO} + * @param applicationArtifact {@link ApplicationArtifact} * @return If the application release is updated correctly True returns, otherwise retuen False */ - boolean updateRelease(int applicationId, String releaseUuid, String deviceType, ApplicationReleaseDTO applicationRelease, - InputStream binaryFileStram, InputStream iconFileStream, InputStream bannerFileStream, - List attachments) throws ApplicationManagementException; + boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException; /*** * To validate the application creating request 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 26e426227e..759e613628 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 @@ -22,7 +22,6 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import java.io.InputStream; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java index cef7d31e0c..3d99177647 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java index 88e3f95907..81d0f1714f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java index b83bbc6a7d..3a02f1b3dc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index efd1bdec20..d43819d949 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -207,5 +207,7 @@ public interface ApplicationDAO { * @throws ApplicationManagementDAOException if unable to fetch the ApplicationDTO from the data store. */ ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException; + + String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index f003862396..64dc0bd984 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -1095,4 +1095,41 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Util.cleanupResources(stmt, rs); } } + + @Override + public String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException { + Connection conn; + String sql; + try { + conn = this.getDBConnection(); + sql = "SELECT AP_APP.SUB_TYPE AS SUB_TYPE " + + "FROM AP_APP " + + "WHERE " + + "AP_APP.ID = (SELECT AP_APP_RELEASE.AP_APP_ID " + + " FROM AP_APP_RELEASE " + + " WHERE AP_APP_RELEASE.UUID = ?) " + + "AND AP_APP.TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + try(ResultSet rs = stmt.executeQuery()){ + if (rs.next()){ + return rs.getString("SUB_TYPE"); + } + return null; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get subscription type of the application " + + "for given application release uuid. UUID:." + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting application subscribe type for given application release UUID: " + + uuid + " from database."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } + } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index d8d473932c..ad5f520570 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -241,7 +241,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "AR.APP_META_INFO AS RELEASE_META_INFO, " + "AR.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + "AR.RATING AS RELEASE_RATING, " - + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, AR.RATED_USERS AS RATED_USER_COUNT " + + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AR.RATED_USERS AS RATED_USER_COUNT " + "FROM AP_APP_RELEASE AS AR " + "WHERE AR.UUID = ? AND AR.TENAT_ID = ?"; 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 e5aa75acd7..4a161c6a4e 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 @@ -147,7 +147,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDTO.getApplicationReleaseDTOs().clear(); ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact); + applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact, false); applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact)); } catch (UnexpectedServerErrorException e) { String msg = "Error occurred when getting Device Type data."; @@ -243,7 +243,6 @@ public class ApplicationManagerImpl implements ApplicationManager { categoryIds.add(category.get().getId()); this.applicationDAO.addCategoryMapping(categoryIds,appId,tenantId); - //adding application tags List tags = applicationWrapper.getTags(); if (!tags.isEmpty()) { @@ -334,7 +333,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } private ApplicationReleaseDTO addApplicationReleaseArtifacts(String applicationType, String deviceType, - ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact, boolean isNewRelease) throws ResourceManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -355,7 +354,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String packagename = applicationInstaller.getPackageName(); ConnectionManagerUtil.getDBConnection(); - if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + if (!isNewRelease && applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, lifecycleStateManager.getEndState())) { String msg = "Application release is already exist for the package name: " + packagename + ". Either you can delete all application releases for package " + packagename + " or " @@ -378,7 +377,7 @@ public class ApplicationManagerImpl implements ApplicationManager { .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { throw new BadRequestException( "Application release exists for the uploaded binary file. Application Type: " - + applicationType + " Device Tyep: " + deviceType); + + applicationType + " Device Type: " + deviceType); } applicationReleaseDTO.setAppHashValue(md5OfApp); @@ -416,6 +415,94 @@ public class ApplicationManagerImpl implements ApplicationManager { return applicationReleaseDTO; } + private ApplicationReleaseDTO updateApplicationReleaseArtifacts(String applicationType, String deviceType, + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + throws ResourceManagementException, ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + // The application executable artifacts such as apks are uploaded. + if (ApplicationType.ENTERPRISE.toString().equals(applicationType)) { + try { + byte[] content = IOUtils.toByteArray(applicationArtifact.getInstallerStream()); + + try(ByteArrayInputStream binaryClone = new ByteArrayInputStream(content)){ + String md5OfApp = StorageManagementUtil.getMD5(binaryClone); + + if (md5OfApp == null) { + String msg = "Error occurred while md5sum value retrieving process: application UUID " + + applicationReleaseDTO.getUuid(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } + if (!applicationReleaseDTO.getAppHashValue().equals(md5OfApp)){ + applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); + + try (ByteArrayInputStream binary = new ByteArrayInputStream(content)) { + ApplicationInstaller applicationInstaller = applicationStorageManager + .getAppInstallerData(binary, deviceType); + String packagename = applicationInstaller.getPackageName(); + + ConnectionManagerUtil.getDBConnection(); + if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + lifecycleStateManager.getEndState())) { + String msg = "Application release is already exist for the package name: " + packagename + + ". Either you can delete all application releases for package " + packagename + " or " + + "you can add this app release as an new application release, under the existing " + + "application."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + applicationReleaseDTO.setVersion(applicationInstaller.getVersion()); + applicationReleaseDTO.setPackageName(packagename); + + if (this.applicationReleaseDAO + .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { + throw new BadRequestException( + "Application release exists for the uploaded binary file. Application Type: " + + applicationType + " Device Tyep: " + deviceType); + } + + applicationReleaseDTO.setAppHashValue(md5OfApp); + String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); + try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { + applicationReleaseDTO = applicationStorageManager + .uploadReleaseArtifact(applicationReleaseDTO,applicationType, + deviceType, binaryDuplicate); + applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, + applicationReleaseDTO); + } + } + } + + } + + + } catch (IOException e) { + String msg = + "Error occurred when getting byte array of binary file. Installer name: " + applicationArtifact + .getInstallerName(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } catch (DBConnectionException e) { + String msg = "Error occurred when getting database connection for verifying application package existence."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when executing the query for verifying application release existence for " + + "the package."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } else if (ApplicationType.WEB_CLIP.toString().equals(applicationType)) { + applicationReleaseDTO.setVersion(Constants.DEFAULT_VERSION); + applicationReleaseDTO.setInstallerName(applicationReleaseDTO.getUrl()); + } + return applicationReleaseDTO; + } + private ApplicationReleaseDTO addImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) throws ResourceManagementException { ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -432,7 +519,40 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setScreenshotName1(scName); } else if (counter == 2) { applicationReleaseDTO.setScreenshotName2(scName); + } else if (counter == 3) { + applicationReleaseDTO.setScreenshotName3(scName); + } + counter++; + } + + // Upload images + applicationReleaseDTO = applicationStorageManager + .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), + applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values())); + return applicationReleaseDTO; + } + + private ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, + ApplicationArtifact applicationArtifact) throws ResourceManagementException{ + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); + + applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); + applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); + applicationReleaseDTO.setScreenshotName1(null); + applicationReleaseDTO.setScreenshotName2(null); + applicationReleaseDTO.setScreenshotName3(null); + Map screenshots = applicationArtifact.getScreenshots(); + List screenshotNames = new ArrayList<>(screenshots.keySet()); + + int counter = 1; + for (String scName : screenshotNames) { + if (counter == 1) { + applicationReleaseDTO.setScreenshotName1(scName); + } else if (counter == 2) { + applicationReleaseDTO.setScreenshotName2(scName); } else if (counter == 3) { applicationReleaseDTO.setScreenshotName3(scName); } @@ -566,36 +686,25 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationRelease createRelease(int applicationId, - ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException { + public ApplicationRelease createRelease(int applicationId, ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; if (log.isDebugEnabled()) { log.debug("ApplicationDTO release request is received for the application id: " + applicationId); } - try { + ApplicationDTO applicationDTO = getApplication(applicationId); + ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper, + applicationDTO, applicationArtifact); ConnectionManagerUtil.beginDBTransaction(); - ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find application for the application Id: " + applicationId; - log.error(msg); - throw new NotFoundException(msg); - } - DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), - applicationArtifact); - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); - String initialstate = lifecycleStateManager.getInitialState(); applicationReleaseDTO.setCurrentState(initialstate); LifecycleStateDTO lifecycleState = getLifecycleStateInstance(initialstate, initialstate); - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationReleaseDTO = this.applicationReleaseDAO .createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationRelease = releaseDtoToRelease(applicationReleaseDTO); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -617,13 +726,48 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while adding new application release for application " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationDTO getApplication(int applicationId) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); + if (applicationDTO == null) { + String msg = "Couldn't find application for the application Id: " + applicationId; + log.error(msg); + throw new NotFoundException(msg); + } + return applicationDTO; + } catch (DBConnectionException e) { + throw new ApplicationManagementException( + "Error occurred while obtaining the database connection for getting application for the application ID: " + + applicationId, e); + } catch (ApplicationManagementDAOException e) { + throw new ApplicationManagementException( + "Error occurred while getting application data for application ID: " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { + try { + DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), + deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, + true); + return addImageArtifacts(applicationReleaseDTO, applicationArtifact); } catch (ResourceManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); String msg = - "Error occurred while uploading application release artifacts. Application ID: " + applicationId; + "Error occurred while uploading application release artifacts. Application ID: " + applicationDTO + .getId(); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -634,14 +778,9 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO; boolean isVisibleApp = false; try { - ConnectionManagerUtil.openDBConnection(); - applicationDTO = this.applicationDAO.getApplicationById(appId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find an application for application Id: " + appId; - log.error(msg); - throw new NotFoundException(msg); - } + applicationDTO = getApplication(appId); + ConnectionManagerUtil.openDBConnection(); List filteredApplicationReleaseDTOs = new ArrayList<>(); for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && ( @@ -1149,12 +1288,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - //todo with this implementation user has to provide all image artifacts in order to update one image artifact as - // well. Therefore enhance this later. public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationReleaseDTO applicationReleaseDTO; try { @@ -1174,8 +1310,6 @@ public class ApplicationManagerImpl implements ApplicationManager { + " and it is not an release updatable state. Hence please move the application release" + " into updatable state and retry the operation."); } - applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); - applicationReleaseDTO = this.applicationReleaseDAO .updateRelease(addImageArtifacts(applicationReleaseDTO, applicationArtifact), tenantId); if (applicationReleaseDTO == null) { @@ -1212,7 +1346,6 @@ public class ApplicationManagerImpl implements ApplicationManager { public void updateApplicationArtifact(String deviceType, String appType, String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); boolean isValidDeviceType = false; List deviceTypes; try { @@ -1245,10 +1378,8 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new NotFoundException(msg); } - String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); - applicationReleaseDTO = addApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, + applicationReleaseDTO = updateApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, applicationArtifact); - applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, applicationReleaseDTO); applicationReleaseDTO = this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId); if (applicationReleaseDTO == null) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -1579,6 +1710,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new ApplicationManagementException(msg); } + ConnectionManagerUtil.commitDBTransaction(); } catch (UserStoreException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( @@ -1682,132 +1814,91 @@ public class ApplicationManagerImpl implements ApplicationManager { return lifecycleState; } - //todo check whether package names are same @Override - public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, - ApplicationReleaseDTO updateRelease, InputStream binaryFileStram, InputStream iconFileStream, - InputStream bannerFileStream, List attachments) throws ApplicationManagementException { + public boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationReleaseDTO release; - ApplicationDTO app = null; - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); - DeviceType deviceTypeObj; - boolean isAdminUser; - try { - // Getting the device type details to get device type ID for internal mappings - deviceTypeObj = Util.getDeviceManagementService().getDeviceType(deviceType); - isAdminUser = isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION); - ConnectionManagerUtil.beginDBTransaction(); - app = this.applicationDAO.getApplicationById(applicationId, tenantId); - release = this.applicationReleaseDAO.getReleaseByIds(applicationId, releaseUuid, tenantId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); - if (app == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application for updating. ApplicationDTO id: " + applicationId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't found an application release for updating. Application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - if (deviceTypeObj == null || deviceTypeObj.getId() != app.getDeviceTypeId()) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new BadRequestException( - "Request to update application release for Invalid device type. Device type: " + deviceType - + " application ID " + applicationId + " ApplicationDTO Release UUID " + releaseUuid); - } - if (release == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application realise for updating. ApplicationDTO id: " + applicationId - + " and application release UUID: " + releaseUuid); + if (!lifecycleStateManager.isUpdatableState(applicationReleaseDTO.getCurrentState())) { + String msg = "Application release in " + applicationReleaseDTO.getCurrentState() + + " state. Therefore you are not allowed to update the application release. Hence, " + + "please move application release from " + applicationReleaseDTO.getCurrentState() + + " to updatable state."; + log.error(msg); + throw new ForbiddenException(msg); } - String releaseType = updateRelease.getReleaseType(); - Double price = updateRelease.getPrice(); - String metaData = updateRelease.getMetaData(); + DeviceType deviceTypeObj = getDeviceTypeData(deviceType); + Double price = applicationReleaseWrapper.getPrice(); - if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(app.getSubType())) - || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(app.getSubType()))) { - ConnectionManagerUtil.rollbackDBTransaction(); + String applicationSubType = this.applicationDAO.getApplicationSubTypeByUUID(releaseUuid, tenantId); + if (applicationSubType == null) { + String msg = "Couldn't find an application subscription type for the application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg); + } + + if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(applicationSubType)) + || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(applicationSubType))) { throw new BadRequestException( "Invalid app release payload for updating application release. ApplicationDTO price is " + price - + " for " + app.getSubType() + " application. ApplicationDTO ID: " + applicationId - + ", ApplicationDTO Release UUID " + releaseUuid + " and supported device type is " - + deviceType); + + " for " + applicationSubType + " application., Application Release UUID " + + releaseUuid + " and supported device type is " + deviceType); } - release.setPrice(price); - if (releaseType != null) { - release.setReleaseType(releaseType); + applicationReleaseDTO.setPrice(price); + applicationReleaseDTO.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants()); + + String supportedOSVersions = applicationReleaseWrapper.getSupportedOsVersions(); + if (!StringUtils.isEmpty(supportedOSVersions)) { + //todo check OS versions are supported or not + applicationReleaseDTO.setSupportedOsVersions(supportedOSVersions); } - if (metaData != null) { - release.setMetaData(metaData); + if (!StringUtils.isEmpty(applicationReleaseWrapper.getDescription())) { + applicationReleaseDTO.setDescription(applicationReleaseWrapper.getDescription()); } - - List unrestrictedRoles = app.getUnrestrictedRoles(); - - String applicationReleaseCreatedUser = lifecycleStateDAO - .getAppReleaseCreatedUsername(applicationId, releaseUuid, tenantId); - - if (!isAdminUser && !(!unrestrictedRoles.isEmpty() && hasUserRole(unrestrictedRoles, userName)) - && !userName.equals(applicationReleaseCreatedUser)) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException("You are not authorized user to update application"); + if (!StringUtils.isEmpty(applicationReleaseWrapper.getReleaseType())) { + applicationReleaseDTO.setReleaseType(applicationReleaseWrapper.getReleaseType()); + } + if (!StringUtils.isEmpty(applicationReleaseWrapper.getMetaData())) { + applicationReleaseDTO.setMetaData(applicationReleaseWrapper.getMetaData()); } - //todo try to remove this DB call and get it when getting application release - LifecycleStateDTO lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); - if (!AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.IN_REVIEW.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.REJECTED.toString().equals(lifecycleState.getCurrentState())) { + applicationReleaseDTO = updateApplicationReleaseArtifacts(applicationType, deviceTypeObj.getName(), + applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = updateImageArtifacts(applicationReleaseDTO, applicationArtifact); + + boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) != null; + if (!updateStatus) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException( - "You can't update application release which is in " + lifecycleState.getCurrentState() - + " State"); } - -// release = applicationStorageManager -// .deleteImageArtifacts(release, iconFileStream, bannerFileStream, attachments); -// release = applicationStorageManager -// .copyImageArtifactsAndDeleteInstaller(release, app.getType(), deviceType, binaryFileStram); - return applicationReleaseDAO.updateRelease(release, tenantId) != null; - } catch (DeviceManagementException e) { - throw new ApplicationManagementException("Error occured when validating the device type " + deviceType, e); - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "Error occured while verifying whether user is admin user or not. Username " + userName - + " tenant id " + tenantId, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error Occured when getting lifecycle state of the application release of application UUID: " - + releaseUuid, e); + ConnectionManagerUtil.commitDBTransaction(); + return updateStatus; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error occured when updating ApplicationDTO release. ApplicationDTO ID " + applicationId - + " ApplicationDTO Release UUID: " + releaseUuid, e); + String msg = "Error occured when updating Application release. ApplicationDTO ID ApplicationDTO Release " + + "UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ResourceManagementException e) { + String msg = "Error occured when updating application release artifact in the file system. Application " + + "release UUID:" + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } -// catch (ApplicationStorageManagementException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating application release artifact. ApplicationDTO ID " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid, e); -// } -// catch (ResourceManagementException e) { -// // updating images -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating image artifact of the application release. ApplicationDTO ID: " -// + applicationId + " ApplicationDTO release UUID: " + releaseUuid, e); -// } catch (RequestValidatingException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when validating application release artifact for device type " + deviceType -// + " And application type " + app.getType() + ". Applicationn ID: " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid); -// } } @@ -1985,6 +2076,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid(); ApplicationRelease applicationRelease = new ApplicationRelease(); applicationRelease.setDescription(applicationReleaseDTO.getDescription()); + applicationRelease.setVersion(applicationReleaseDTO.getVersion()); applicationRelease.setUuid(applicationReleaseDTO.getUuid()); applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType()); applicationRelease.setPrice(applicationReleaseDTO.getPrice()); 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 b695405bc1..053aa3bf73 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 @@ -78,7 +78,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager public ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String artifactDirectoryPath; String iconStoredLocation; String bannerStoredLocation; @@ -122,10 +121,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } catch (IOException e) { throw new ApplicationStorageManagementException("IO Exception while saving the screens hots for " + "the application " + applicationRelease.getUuid(), e); - } catch (ApplicationStorageManagementException e) { - throw new ApplicationStorageManagementException("ApplicationDTO Management DAO exception while trying to " - + "update the screen-shot count for the application " + applicationRelease.getUuid() + - " for the tenant id " + tenantId, e); } } 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 5ffaa35752..30dbfb2fb4 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 @@ -535,7 +535,7 @@ public interface ApplicationManagementPublisherAPI { ); @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( @@ -567,21 +567,56 @@ public interface ApplicationManagementPublisherAPI { response = ErrorResponse.class) }) Response updateApplicationRelease( - @ApiParam(name = "deviceType", value = "Supported device type of the application", required = true) + @ApiParam( + name = "deviceType", + value = "Supported device type of the application", + required = true) @PathParam("deviceType") String deviceType, - @ApiParam(name = "appId", value = "Identifier of the ApplicationDTO", required = true) - @PathParam("appId") int applicationId, - @ApiParam(name = "UUID", value = "Unique identifier of the ApplicationDTO Release", required = true) + @ApiParam( + name = "appType", + value = "Type of the application", + required = true) + @PathParam("appType") String appType, + @ApiParam( + name = "UUID", + value = "Unique identifier of the ApplicationDTO Release", + required = true) @PathParam("uuid") String applicationUUID, - @Multipart(value = "applicationRelease", required = false, type = "application/json") ApplicationReleaseDTO applicationRelease, - @Multipart(value = "binaryFile", required = false) Attachment binaryFile, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @ApiParam(name = "screenshot1", value = "Screen Shots of the uploading application", required = true) + @ApiParam( + name = "applicationReleaseWrapper", + value = "Application release wrapper which is going to update.", + required = true) + @Multipart( + value = "applicationReleaseWrapper", + type = "application/json") + ApplicationReleaseWrapper applicationReleaseWrapper, + @ApiParam( + name = "binaryFile", + value = "Application installer file.", + required = true) + @Multipart(value = "binaryFile") Attachment binaryFile, + @ApiParam( + name = "icon", + value = "Icon file of the application release.", + required = true) + @Multipart(value = "icon") Attachment iconFile, + @ApiParam( + name = "banner", + value = "banner file of the application release.", + required = true) + @Multipart(value = "banner") Attachment bannerFile, + @ApiParam( + name = "screenshot1", + value = "First screenshot of the uploading application", + required = true) @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam(name = "screenshot2", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot2", + value = "Second screenshot 2 of the uploading application") @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam(name = "screenshot3", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot3", + value = "Third screenshot of the uploading application") @Multipart(value = "screenshot3") Attachment screenshot3); @GET 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/impl/ApplicationManagementPublisherAPIImpl.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/impl/ApplicationManagementPublisherAPIImpl.java index be37e9e3b2..a72767a6ed 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/impl/ApplicationManagementPublisherAPIImpl.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/impl/ApplicationManagementPublisherAPIImpl.java @@ -288,6 +288,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); + applicationManager.validateBinaryArtifact(binaryFile, appType); if (!ApplicationType.ENTERPRISE.toString().equals(appType)) { String msg = "If ApplicationDTO type is " + appType + ", therefore you don't have application release artifact to update for application release UUID: " @@ -295,7 +296,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } - applicationManager.validateBinaryArtifact(binaryFile, appType); + applicationManager.updateApplicationArtifact(deviceType, appType, applicationReleaseUuid, constructApplicationArtifact(binaryFile, null, null, null)); return Response.status(Response.Status.OK) @@ -341,18 +342,14 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - /* - //todo ---------------------- - */ - @Override @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") public Response updateApplicationRelease( @PathParam("deviceType") String deviceType, - @PathParam("appId") int applicationId, + @PathParam("appType") String appType, @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") ApplicationReleaseDTO applicationRelease, + @Multipart("applicationRelease") ApplicationReleaseWrapper applicationReleaseWrapper, @Multipart("binaryFile") Attachment binaryFile, @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, @@ -360,50 +357,35 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Multipart("screenshot2") Attachment screenshot2, @Multipart("screenshot3") Attachment screenshot3) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - InputStream iconFileStream; - InputStream bannerFileStream; - InputStream binaryFileStram; - List attachments = new ArrayList<>(); - List attachmentList = new ArrayList<>(); + List screenshots = new ArrayList<>(); if (screenshot1 != null){ - attachmentList.add(screenshot1); + screenshots.add(screenshot1); } if (screenshot2 != null) { - attachmentList.add(screenshot2); + screenshots.add(screenshot2); } if (screenshot3 != null) { - attachmentList.add(screenshot3); + screenshots.add(screenshot3); } - if (iconFile == null || bannerFile == null || binaryFile == null || attachmentList.isEmpty()){ - String msg = "Invalid data is received for application release updating. application id: " + applicationId - + " and application release UUID: " + applicationUUID; - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - try { - binaryFileStram = binaryFile.getDataHandler().getInputStream(); - iconFileStream = iconFile.getDataHandler().getInputStream(); - bannerFileStream = bannerFile.getDataHandler().getInputStream(); - for (Attachment screenshot : attachmentList) { - attachments.add(screenshot.getDataHandler().getInputStream()); - } - boolean status = applicationManager - .updateRelease(applicationId, applicationUUID, deviceType, applicationRelease, binaryFileStram, - iconFileStream, bannerFileStream, attachments); - if (!status){ - log.error("ApplicationDTO release updating is failed. Please contact the administrator. ApplicationDTO id: " - + applicationId + ", ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " - + deviceType); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationRelease).build(); + applicationManager.validateBinaryArtifact(binaryFile, appType); + applicationManager.validateImageArtifacts(iconFile, bannerFile, screenshots); + if (!applicationManager.updateRelease(deviceType, appType, applicationUUID, applicationReleaseWrapper, + constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots))) { + log.error("Application release updating is failed. Please contact the administrator. " + + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationReleaseWrapper).build(); } - return Response.status(Response.Status.OK).entity("ApplicationDTO release is successfully updated.").build(); - } catch(BadRequestException e){ - String msg = "Invalid request to update application release for application release UUID " + applicationUUID; + + return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); + } catch (BadRequestException e) { + String msg = + "Invalid request to update application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (NotFoundException e) { - String msg = "Couldn't found application or application release for application id: " + applicationId - + " and application release UUID " + applicationUUID; + String msg = + "Couldn't found application or application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (ForbiddenException e) { @@ -411,18 +393,20 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem + applicationUUID; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - } - catch (ApplicationManagementException e) { + } catch (ApplicationManagementException e) { String msg = "Error while updating the application release of the application with UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IOException e) { - String msg = "Error while updating the release artifacts of the application with UUID " + applicationUUID; - log.error(msg, e); + } catch (RequestValidatingException e) { + String msg = "Error occurred while updating the application release in the file system"; + log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + /* +//todo ---------------------- +*/ @DELETE @Path("/{appid}") public Response deleteApplication(