From a4f7984a0f406c5f21c6fa4b2c41e94531cce8df Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 20 Jun 2019 15:43:58 +0530 Subject: [PATCH] Improve app creating method by validating supported OS types in the request. --- .../common/services/ApplicationManager.java | 2 +- .../wrapper/ApplicationReleaseWrapper.java | 4 +- .../wrapper/PublicAppReleaseWrapper.java | 4 +- .../mgt/core/impl/ApplicationManagerImpl.java | 62 ++++++++++++++++--- .../management/ApplicationManagementTest.java | 2 +- 5 files changed, 60 insertions(+), 14 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/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 90f65a44a55..ed6d77a61ca 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 @@ -53,7 +53,7 @@ public interface ApplicationManager { * @throws ApplicationManagementException ApplicationDTO Management Exception */ Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException, RequestValidatingException; + throws ApplicationManagementException; Application createWebClip(WebAppWrapper webAppWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException; 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 fb7def99631..4314be9659e 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 @@ -54,7 +54,9 @@ public class ApplicationReleaseWrapper { private String metaData; @ApiModelProperty(name = "supportedOsVersions", - value = "ApplicationDTO release supported OS versions") + value = "Application release supported OS versions", + required = true, + example = "4.0-10.0") @NotNull private String supportedOsVersions; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/PublicAppReleaseWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/PublicAppReleaseWrapper.java index 46f274dd2d3..badbdf30d5c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/PublicAppReleaseWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/PublicAppReleaseWrapper.java @@ -66,7 +66,9 @@ public class PublicAppReleaseWrapper { private String packageName; @ApiModelProperty(name = "supportedOsVersions", - value = "ApplicationDTO release supported OS versions") + value = "Application release supported OS versions", + required = true, + example = "4.0-10.0") @NotNull private String supportedOsVersions; 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 dab6fdf57f2..c940fa7ab4e 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 @@ -88,6 +88,7 @@ import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; @@ -135,17 +136,16 @@ public class ApplicationManagerImpl implements ApplicationManager { * The responsbility of this method is the creating an application. * @param applicationWrapper ApplicationDTO that need to be created. * @return {@link ApplicationDTO} - * @throws RequestValidatingException if application creating request is invalid, * @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException} */ @Override public Application createApplication(ApplicationWrapper applicationWrapper, - ApplicationArtifact applicationArtifact) throws RequestValidatingException, ApplicationManagementException { + ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (log.isDebugEnabled()) { - log.debug("Application create request is received for the tenant : " + tenantId + " From" + " the user : " + log.debug("Application create request is received for the tenant : " + tenantId + " and" + " the user : " + userName); } @@ -154,6 +154,13 @@ public class ApplicationManagerImpl implements ApplicationManager { //uploading application artifacts try { ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + if (!isValidOsVersions(applicationReleaseDTO.getSupportedOsVersions(), applicationWrapper.getDeviceType())){ + String msg = "You are trying to create application which has an application release contains invalid or " + + "unsupported OS versions in the supportedOsVersions section. Hence, please re-evaluate the " + + "request payload."; + log.error(msg); + throw new BadRequestException(msg); + } applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), applicationWrapper.getDeviceType(), applicationReleaseDTO, applicationArtifact, false); applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); @@ -425,7 +432,6 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(webAppWrapper); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); String uuid = UUID.randomUUID().toString(); - //todo check installer name exists or not, do it in the validation method String md5 = DigestUtils.md5Hex(applicationReleaseDTO.getInstallerName()); applicationReleaseDTO.setUuid(uuid); applicationReleaseDTO.setAppHashValue(md5); @@ -455,6 +461,14 @@ public class ApplicationManagerImpl implements ApplicationManager { + userName); } + if (!isValidOsVersions(publicAppWrapper.getPublicAppReleaseWrappers().get(0).getSupportedOsVersions(), + publicAppWrapper.getDeviceType())) { + String msg = "You are trying to add application release which has invalid or unsupported OS versions in " + + "the supportedOsVersions section. Hence, please re-evaluate the request payload."; + log.error(msg); + throw new BadRequestException(msg); + } + if (DeviceTypes.ANDROID.toString().equals(publicAppWrapper.getDeviceType())) { publicAppStorePath = Constants.GOOGLE_PLAY_STORE_URL; } else if (DeviceTypes.IOS.toString().equals(publicAppWrapper.getDeviceType())) { @@ -758,8 +772,15 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new BadRequestException(msg); } + DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); + if (!isValidOsVersions(applicationReleaseWrapper.getSupportedOsVersions(), deviceType.getName())){ + String msg = "You are trying to add application release which has invalid or unsupported OS versions in " + + "the supportedOsVersions section. Hence, please re-evaluate the request payload."; + log.error(msg); + throw new BadRequestException(msg); + } ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper, - applicationDTO, applicationArtifact); + applicationDTO, applicationArtifact, deviceType.getName()); ConnectionManagerUtil.beginDBTransaction(); String initialstate = lifecycleStateManager.getInitialState(); applicationReleaseDTO.setCurrentState(initialstate); @@ -818,12 +839,11 @@ public class ApplicationManagerImpl implements ApplicationManager { } private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper, - ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact) + ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact, String deviceTypeName) throws ApplicationManagementException { try { - DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - deviceType.getName(), APIUtil.releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, + deviceTypeName, APIUtil.releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, true); return addImageArtifacts(applicationReleaseDTO, applicationArtifact); } catch (ResourceManagementException e) { @@ -834,6 +854,30 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + private boolean isValidOsVersions(String osRange, String deviceTypeName) + throws ApplicationManagementException { + String lowestSupportingOsVersion; + String highestSupportingOsVersion = null; + String[] supportedOsVersionValues = osRange.split("-"); + lowestSupportingOsVersion = supportedOsVersionValues[0].trim(); + if (!"ALL".equals(supportedOsVersionValues[1].trim())) { + highestSupportingOsVersion = supportedOsVersionValues[1].trim(); + } + + try { + DeviceManagementProviderService deviceManagementProviderService = DAOUtil.getDeviceManagementService(); + return deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, lowestSupportingOsVersion) + != null && (highestSupportingOsVersion == null + || deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, highestSupportingOsVersion) + != null); + } catch (DeviceManagementException e) { + String msg = + "Error occurred while getting supported device type versions for device type : " + deviceTypeName; + log.error(msg); + throw new ApplicationManagementException(msg); + } + } + @Override public Application getApplicationById(int appId, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -2451,7 +2495,6 @@ public class ApplicationManagerImpl implements ApplicationManager { List registeredCategories = this.applicationDAO.getAllCategories(tenantId); if (registeredCategories.isEmpty()) { - ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Registered application category set is empty. Since it is mandatory to add application " + "category when adding new application, registered application category list shouldn't be null."; log.error(msg); @@ -2482,7 +2525,6 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new ApplicationManagementException(msg, e); } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred when validating the unrestricted roles given for the web clip"; log.error(msg); throw new ApplicationManagementException(msg, e); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java index 04607486a7d..9ec92e18201 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java @@ -75,7 +75,7 @@ public class ApplicationManagementTest extends BaseTestCase { releaseWrapper.setMetaData("Just meta data"); releaseWrapper.setReleaseType("free"); releaseWrapper.setPrice(5.7); - releaseWrapper.setSupportedOsVersions("5.7, 6.1"); + releaseWrapper.setSupportedOsVersions("4.0-7.0"); applicationReleaseWrappers.add(releaseWrapper); applicationWrapper.setApplicationReleaseWrappers(applicationReleaseWrappers);