Fix app installing issue for same package name

If we add same installer as a new app  by giving different name, it was able to add as new application. But, if there is application release for same package name, the application adding should be blocked. Therefore verify if there is an application release for same package name before adding an application.
feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent b06a908f5f
commit 071ef71c86

@ -24,7 +24,7 @@ public class ApplicationArtifact {
private InputStream installerStream; private InputStream installerStream;
private String bannername; private String bannerName;
private InputStream bannerStream; private InputStream bannerStream;
@ -50,12 +50,12 @@ public class ApplicationArtifact {
this.installerStream = installerStream; this.installerStream = installerStream;
} }
public String getBannername() { public String getBannerName() {
return bannername; return bannerName;
} }
public void setBannername(String bannername) { public void setBannerName(String bannerName) {
this.bannername = bannername; this.bannerName = bannerName;
} }
public InputStream getBannerStream() { public InputStream getBannerStream() {

@ -0,0 +1,45 @@
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
* 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.
*/
public class ApplicationInstaller {
/***
* Package name of the Installer
*/
private String packageName;
/***
* Version of the Installer.
*/
private String version;
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}

@ -19,6 +19,7 @@
package org.wso2.carbon.device.application.mgt.common.services; 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.dto.ApplicationReleaseDTO;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; 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.RequestValidatingException;
@ -54,6 +55,10 @@ public interface ApplicationStorageManager {
ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFile, ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFile,
InputStream bannerFile, List<InputStream> screenshots) throws ResourceManagementException; InputStream bannerFile, List<InputStream> screenshots) throws ResourceManagementException;
ApplicationInstaller getAppInstallerData(InputStream binaryFile, String deviceType)
throws ApplicationStorageManagementException;
/** /**
* To upload release artifacts for an ApplicationDTO. * To upload release artifacts for an ApplicationDTO.
* *

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.impl; package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.UrlValidator; import org.apache.commons.validator.routines.UrlValidator;
import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Attachment;
@ -28,6 +29,7 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.AppLifecycleState; import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO;
import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO;
@ -75,6 +77,8 @@ 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;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -317,25 +321,70 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
private ApplicationDTO addApplicationReleaseArtifacts(ApplicationDTO applicationDTO, private ApplicationDTO addApplicationReleaseArtifacts(ApplicationDTO applicationDTO,
ApplicationArtifact applicationArtifact) throws ResourceManagementException { ApplicationArtifact applicationArtifact) throws ResourceManagementException,
ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
List<ApplicationReleaseDTO> applicationReleaseEntities = new ArrayList<>(); List<ApplicationReleaseDTO> applicationReleaseEntities = new ArrayList<>();
ApplicationReleaseDTO applicationReleaseDTO; ApplicationReleaseDTO applicationReleaseDTO;
applicationReleaseDTO = applicationDTO.getApplicationReleases().get(0); applicationReleaseDTO = applicationDTO.getApplicationReleases().get(0);
String uuid = UUID.randomUUID().toString();
applicationReleaseDTO.setUuid(uuid);
// The application executable artifacts such as apks are uploaded. // The application executable artifacts such as apks are uploaded.
if (!ApplicationType.ENTERPRISE.toString().equals(applicationDTO.getType())) { if (ApplicationType.ENTERPRISE.toString().equals(applicationDTO.getType())) {
applicationReleaseDTO = applicationStorageManager try {
.uploadReleaseArtifact(applicationReleaseDTO, applicationDTO.getType(), byte[] content = IOUtils.toByteArray(applicationArtifact.getInstallerStream());
applicationDTO.getDeviceTypeName(), null);
} else {
applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName());
try (ByteArrayInputStream binary = new ByteArrayInputStream(content)) {
ApplicationInstaller applicationInstaller = applicationStorageManager
.getAppInstallerData(binary, applicationDTO.getDeviceTypeName());
String packagename = applicationInstaller.getPackageName();
ConnectionManagerUtil.getDBConnection();
if (applicationReleaseDAO.isAppExisitForPackageName(packagename, tenantId)) {
String msg = "Application release is already exist for the package name: " + packagename;
log.error(msg);
throw new ApplicationManagementException(msg);
}
applicationReleaseDTO.setVersion(applicationInstaller.getVersion());
applicationReleaseDTO.setPackageName(packagename);
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
applicationReleaseDTO = applicationStorageManager applicationReleaseDTO = applicationStorageManager
.uploadReleaseArtifact(applicationReleaseDTO, applicationDTO.getType(), .uploadReleaseArtifact(applicationReleaseDTO, applicationDTO.getType(),
applicationDTO.getDeviceTypeName(), applicationArtifact.getInstallerStream()); applicationDTO.getDeviceTypeName(), binaryDuplicate);
}
}
} 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(applicationDTO.getType())) {
applicationReleaseDTO.setVersion(Constants.DEFAULT_VERSION);
applicationReleaseDTO.setInstallerName(applicationReleaseDTO.getUrl());
// Since WEB CLIP doesn't have an installer, set uuid as has value for WEB CLIP
applicationReleaseDTO.setAppHashValue(uuid);
} }
applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); applicationReleaseDTO.setIconName(applicationArtifact.getIconName());
applicationReleaseDTO.setBannerName(applicationArtifact.getBannername()); applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName());
Map<String, InputStream> screenshots = applicationArtifact.getScreenshots(); Map<String, InputStream> screenshots = applicationArtifact.getScreenshots();
List<String> screenshotNames = new ArrayList<>(screenshots.keySet()); List<String> screenshotNames = new ArrayList<>(screenshots.keySet());
@ -357,7 +406,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationReleaseDTO = applicationStorageManager applicationReleaseDTO = applicationStorageManager
.uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(),
applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values())); applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values()));
applicationReleaseDTO.setUuid(UUID.randomUUID().toString());
applicationReleaseEntities.add(applicationReleaseDTO); applicationReleaseEntities.add(applicationReleaseDTO);
applicationDTO.setApplicationReleases(applicationReleaseEntities); applicationDTO.setApplicationReleases(applicationReleaseEntities);
return applicationDTO; return applicationDTO;

@ -30,6 +30,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.routines.UrlValidator; import org.apache.commons.validator.routines.UrlValidator;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
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.dto.ApplicationReleaseDTO;
import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.DeviceTypes; import org.wso2.carbon.device.application.mgt.common.DeviceTypes;
@ -175,17 +176,37 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
} }
} }
public ApplicationInstaller getAppInstallerData(InputStream binaryFile, String deviceType)
throws ApplicationStorageManagementException {
ApplicationInstaller applicationInstaller = new ApplicationInstaller();
try {
if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) {
ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(binaryFile);
applicationInstaller.setVersion(apkMeta.getVersionName());
applicationInstaller.setPackageName(apkMeta.getPackageName());
} else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) {
NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile);
applicationInstaller
.setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString());
applicationInstaller
.setPackageName(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_IDENTIFIER_KEY).toString());
} else {
String msg = "Application Type doesn't match with supporting application types " + deviceType;
log.error(msg);
throw new ApplicationStorageManagementException(msg);
}
} catch (ParsingException e){
String msg = "Application Type doesn't match with supporting application types " + deviceType;
log.error(msg);
throw new ApplicationStorageManagementException(msg);
}
return applicationInstaller;
}
@Override @Override
public ApplicationReleaseDTO uploadReleaseArtifact(ApplicationReleaseDTO applicationReleaseDTO, String appType, public ApplicationReleaseDTO uploadReleaseArtifact(ApplicationReleaseDTO applicationReleaseDTO, String appType,
String deviceType, InputStream binaryFile) throws ResourceManagementException { String deviceType, InputStream binaryFile) throws ResourceManagementException {
try { try {
// move version and package getting code into separate method and check whether package exist in db
if (ApplicationType.WEB_CLIP.toString().equals(appType)) {
applicationReleaseDTO.setVersion(Constants.DEFAULT_VERSION);
applicationReleaseDTO.setInstallerName(applicationReleaseDTO.getUrl());
applicationReleaseDTO.setAppHashValue(null);
return applicationReleaseDTO;
}
String artifactDirectoryPath; String artifactDirectoryPath;
String md5OfApp; String md5OfApp;
String artifactPath; String artifactPath;
@ -199,29 +220,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
throw new ApplicationStorageManagementException(msg); throw new ApplicationStorageManagementException(msg);
} }
artifactDirectoryPath = storagePath + md5OfApp;
if (DeviceTypes.ANDROID.toString().equalsIgnoreCase(deviceType)) {
ApkMeta apkMeta = ArtifactsParser.readAndroidManifestFile(new ByteArrayInputStream(content));
applicationReleaseDTO.setVersion(apkMeta.getVersionName());
applicationReleaseDTO.setPackageName(apkMeta.getPackageName());
} else if (DeviceTypes.IOS.toString().equalsIgnoreCase(deviceType)) {
NSDictionary plistInfo = ArtifactsParser.readiOSManifestFile(binaryFile);
applicationReleaseDTO
.setVersion(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_VERSION_KEY).toString());
applicationReleaseDTO
.setPackageName(plistInfo.objectForKey(ArtifactsParser.IPA_BUNDLE_IDENTIFIER_KEY).toString());
} else {
String msg = "Application Type doesn't match with supporting application types " + applicationReleaseDTO
.getUuid();
log.error(msg);
throw new ApplicationStorageManagementException(msg);
}
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Artifact Directory Path for saving the application release related artifacts related with " log.debug("Artifact Directory Path for saving the application release is " + artifactDirectoryPath
+ "application UUID " + applicationReleaseDTO.getUuid() + " is " + artifactDirectoryPath); + ". ApplicationUUID: " + applicationReleaseDTO.getUuid());
} }
artifactDirectoryPath = storagePath + md5OfApp;
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
artifactPath = artifactDirectoryPath + File.separator + applicationReleaseDTO.getInstallerName(); artifactPath = artifactDirectoryPath + File.separator + applicationReleaseDTO.getInstallerName();
saveFile(new ByteArrayInputStream(content), artifactPath); saveFile(new ByteArrayInputStream(content), artifactPath);
@ -231,19 +234,13 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
+ applicationReleaseDTO.getUuid(); + applicationReleaseDTO.getUuid();
log.error(msg); log.error(msg);
throw new ApplicationStorageManagementException( msg, e); throw new ApplicationStorageManagementException( msg, e);
} catch (ParsingException e) {
String msg = "Error occurred while parsing the artifact file. Application release UUID is "
+ applicationReleaseDTO.getUuid();
log.error(msg);
throw new ApplicationStorageManagementException(msg, e);
} }
return applicationReleaseDTO; return applicationReleaseDTO;
} }
@Override @Override
public ApplicationReleaseDTO updateReleaseArtifacts(ApplicationReleaseDTO applicationRelease, String appType, public ApplicationReleaseDTO updateReleaseArtifacts(ApplicationReleaseDTO applicationRelease,
String deviceType, InputStream binaryFile) throws ApplicationStorageManagementException, String appType, String deviceType, InputStream binaryFile) throws ApplicationStorageManagementException {
RequestValidatingException {
try { try {
deleteApplicationReleaseArtifacts(applicationRelease.getInstallerName()); deleteApplicationReleaseArtifacts(applicationRelease.getInstallerName());

@ -655,7 +655,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
applicationArtifact.setBannername(bannerFileName); applicationArtifact.setBannerName(bannerFileName);
applicationArtifact.setBannerStream(bannerStream); applicationArtifact.setBannerStream(bannerStream);
Map<String, InputStream> scrrenshotData = new HashMap<>(); Map<String, InputStream> scrrenshotData = new HashMap<>();

Loading…
Cancel
Save