Improve app creating method by validating supported OS types in the request.

feature/appm-store/pbac
lasanthaDLPDS 5 years ago
parent 7f541a07e8
commit a4f7984a0f

@ -53,7 +53,7 @@ public interface ApplicationManager {
* @throws ApplicationManagementException ApplicationDTO Management Exception * @throws ApplicationManagementException ApplicationDTO Management Exception
*/ */
Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact) Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact)
throws ApplicationManagementException, RequestValidatingException; throws ApplicationManagementException;
Application createWebClip(WebAppWrapper webAppWrapper, ApplicationArtifact applicationArtifact) Application createWebClip(WebAppWrapper webAppWrapper, ApplicationArtifact applicationArtifact)
throws ApplicationManagementException; throws ApplicationManagementException;

@ -54,7 +54,9 @@ public class ApplicationReleaseWrapper {
private String metaData; private String metaData;
@ApiModelProperty(name = "supportedOsVersions", @ApiModelProperty(name = "supportedOsVersions",
value = "ApplicationDTO release supported OS versions") value = "Application release supported OS versions",
required = true,
example = "4.0-10.0")
@NotNull @NotNull
private String supportedOsVersions; private String supportedOsVersions;

@ -66,7 +66,9 @@ public class PublicAppReleaseWrapper {
private String packageName; private String packageName;
@ApiModelProperty(name = "supportedOsVersions", @ApiModelProperty(name = "supportedOsVersions",
value = "ApplicationDTO release supported OS versions") value = "Application release supported OS versions",
required = true,
example = "4.0-10.0")
@NotNull @NotNull
private String supportedOsVersions; private String supportedOsVersions;

@ -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.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; 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.UserRealm;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; 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. * The responsbility of this method is the creating an application.
* @param applicationWrapper ApplicationDTO that need to be created. * @param applicationWrapper ApplicationDTO that need to be created.
* @return {@link ApplicationDTO} * @return {@link ApplicationDTO}
* @throws RequestValidatingException if application creating request is invalid,
* @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException} * @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException}
*/ */
@Override @Override
public Application createApplication(ApplicationWrapper applicationWrapper, public Application createApplication(ApplicationWrapper applicationWrapper,
ApplicationArtifact applicationArtifact) throws RequestValidatingException, ApplicationManagementException { ApplicationArtifact applicationArtifact) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (log.isDebugEnabled()) { 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); + userName);
} }
@ -154,6 +154,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
//uploading application artifacts //uploading application artifacts
try { try {
ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); 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(), applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(),
applicationWrapper.getDeviceType(), applicationReleaseDTO, applicationArtifact, false); applicationWrapper.getDeviceType(), applicationReleaseDTO, applicationArtifact, false);
applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact);
@ -425,7 +432,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(webAppWrapper); ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(webAppWrapper);
ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0);
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();
//todo check installer name exists or not, do it in the validation method
String md5 = DigestUtils.md5Hex(applicationReleaseDTO.getInstallerName()); String md5 = DigestUtils.md5Hex(applicationReleaseDTO.getInstallerName());
applicationReleaseDTO.setUuid(uuid); applicationReleaseDTO.setUuid(uuid);
applicationReleaseDTO.setAppHashValue(md5); applicationReleaseDTO.setAppHashValue(md5);
@ -455,6 +461,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ userName); + 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())) { if (DeviceTypes.ANDROID.toString().equals(publicAppWrapper.getDeviceType())) {
publicAppStorePath = Constants.GOOGLE_PLAY_STORE_URL; publicAppStorePath = Constants.GOOGLE_PLAY_STORE_URL;
} else if (DeviceTypes.IOS.toString().equals(publicAppWrapper.getDeviceType())) { } else if (DeviceTypes.IOS.toString().equals(publicAppWrapper.getDeviceType())) {
@ -758,8 +772,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(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, ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper,
applicationDTO, applicationArtifact); applicationDTO, applicationArtifact, deviceType.getName());
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
String initialstate = lifecycleStateManager.getInitialState(); String initialstate = lifecycleStateManager.getInitialState();
applicationReleaseDTO.setCurrentState(initialstate); applicationReleaseDTO.setCurrentState(initialstate);
@ -818,12 +839,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper, private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper,
ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact) ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact, String deviceTypeName)
throws ApplicationManagementException { throws ApplicationManagementException {
try { try {
DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId());
ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(),
deviceType.getName(), APIUtil.releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, deviceTypeName, APIUtil.releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact,
true); true);
return addImageArtifacts(applicationReleaseDTO, applicationArtifact); return addImageArtifacts(applicationReleaseDTO, applicationArtifact);
} catch (ResourceManagementException e) { } 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 @Override
public Application getApplicationById(int appId, String state) throws ApplicationManagementException { public Application getApplicationById(int appId, String state) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -2451,7 +2495,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<CategoryDTO> registeredCategories = this.applicationDAO.getAllCategories(tenantId); List<CategoryDTO> registeredCategories = this.applicationDAO.getAllCategories(tenantId);
if (registeredCategories.isEmpty()) { if (registeredCategories.isEmpty()) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Registered application category set is empty. Since it is mandatory to add application " 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."; + "category when adding new application, registered application category list shouldn't be null.";
log.error(msg); log.error(msg);
@ -2482,7 +2525,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when validating the unrestricted roles given for the web clip"; String msg = "Error occurred when validating the unrestricted roles given for the web clip";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);

@ -75,7 +75,7 @@ public class ApplicationManagementTest extends BaseTestCase {
releaseWrapper.setMetaData("Just meta data"); releaseWrapper.setMetaData("Just meta data");
releaseWrapper.setReleaseType("free"); releaseWrapper.setReleaseType("free");
releaseWrapper.setPrice(5.7); releaseWrapper.setPrice(5.7);
releaseWrapper.setSupportedOsVersions("5.7, 6.1"); releaseWrapper.setSupportedOsVersions("4.0-7.0");
applicationReleaseWrappers.add(releaseWrapper); applicationReleaseWrappers.add(releaseWrapper);
applicationWrapper.setApplicationReleaseWrappers(applicationReleaseWrappers); applicationWrapper.setApplicationReleaseWrappers(applicationReleaseWrappers);

Loading…
Cancel
Save