Improve app create response

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 7e4cc36977
commit 03dc9e8810

@ -118,6 +118,12 @@
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
</dependencies>
</project>

@ -68,6 +68,11 @@ public class Filter {
*/
private int deviceTypeId;
/***
* Package Name of the application release. i.e org.wso2.iot.agent etc
*/
private String packageName;
public int getLimit() {
return limit;
}
@ -133,4 +138,8 @@ public class Filter {
public int getDeviceTypeId() { return deviceTypeId; }
public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; }
public String getPackageName() { return packageName; }
public void setPackageName(String packageName) { this.packageName = packageName; }
}

@ -18,14 +18,14 @@
*/
package org.wso2.carbon.device.application.mgt.common.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@ApiModel(value = "ApplicationReleaseEntity", description = "This class holds the details when releasing an ApplicationEntity to application store")
@JsonIgnoreProperties({"appHashValue"})
public class ApplicationReleaseEntity {
@JsonIgnore
@ApiModelProperty(name = "id",
value = "ID of the application release")
private int id;

@ -20,6 +20,11 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.List;
public class Application {
@ApiModelProperty(name = "id",
value = "ID of the application",
required = true)
private int id;
@ApiModelProperty(name = "name",
value = "Name of the application",
required = true)
@ -68,12 +73,14 @@ public class Application {
example = "IoS, Android, Arduino, RaspberryPi etc")
private String deviceType;
@ApiModelProperty(name = "applicationReleases",
value = "List of application releases",
required = true)
private List<ApplicationRelease> applicationReleases;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() {
return name;

@ -28,6 +28,38 @@ public class ApplicationRelease {
value = "Description of the application release")
private String description;
@ApiModelProperty(name = "version",
value = "Version of the application release")
private String version;
@ApiModelProperty(name = "uuid",
value = "UUID of the application release")
private String uuid;
@ApiModelProperty(name = "installerPath",
value = "ApplicationEntity storing location")
private String installerPath;
@ApiModelProperty(name = "bannerPath",
value = "Banner file storing location")
private String bannerPath;
@ApiModelProperty(name = "iconPath",
value = "icon file storing location")
private String iconPath;
@ApiModelProperty(name = "screenshotPath1",
value = "Screenshot storing location")
private String screenshotPath1;
@ApiModelProperty(name = "screenshotPath2",
value = "Screenshot storing location")
private String screenshotPath2;
@ApiModelProperty(name = "screenshotPath3",
value = "Screenshot storing location")
private String screenshotPath3;
@ApiModelProperty(name = "releaseType",
value = "Release type of the application release",
required = true,
@ -101,6 +133,42 @@ public class ApplicationRelease {
public void setDescription(String description) { this.description = description; }
public String getVersion() { return version; }
public void setVersion(String version) { this.version = version; }
public String getUuid() { return uuid; }
public void setUuid(String uuid) { this.uuid = uuid; }
public String getInstallerPath() { return installerPath; }
public void setInstallerPath(String installerPath) { this.installerPath = installerPath; }
public String getBannerPath() { return bannerPath; }
public void setBannerPath(String bannerPath) { this.bannerPath = bannerPath; }
public String getIconPath() { return iconPath; }
public void setIconPath(String iconPath) { this.iconPath = iconPath; }
public String getScreenshotPath1() { return screenshotPath1; }
public void setScreenshotPath1(String screenshotPath1) { this.screenshotPath1 = screenshotPath1; }
public String getScreenshotPath2() { return screenshotPath2; }
public void setScreenshotPath2(String screenshotPath2) { this.screenshotPath2 = screenshotPath2; }
public String getScreenshotPath3() { return screenshotPath3; }
public void setScreenshotPath3(String screenshotPath3) { this.screenshotPath3 = screenshotPath3; }
public boolean isSharedWithAllTenants() { return isSharedWithAllTenants; }
public void setSharedWithAllTenants(boolean sharedWithAllTenants) { isSharedWithAllTenants = sharedWithAllTenants; }
public String getSupportedOsVersions() { return supportedOsVersions; }
public void setSupportedOsVersions(String supportedOsVersions) { this.supportedOsVersions = supportedOsVersions; }

@ -27,6 +27,7 @@ import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.entity.LifecycleStateEntity;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.response.Application;
import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper;
import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper;
@ -46,7 +47,7 @@ public interface ApplicationManager {
* @return Created application
* @throws ApplicationManagementException ApplicationEntity Management Exception
*/
ApplicationEntity createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact)
Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact)
throws ApplicationManagementException, RequestValidatingException;
/**

@ -46,6 +46,8 @@ public class Configuration {
private List<String> appCategories;
private String artifactDownloadEndpoint;
@XmlElement(name = "DatasourceName", required = true)
public String getDatasourceName() {
return datasourceName;
@ -89,6 +91,15 @@ public class Configuration {
this.uiConfiguration = uiConfiguration;
}
@XmlElement(name = "ArtifactDownloadEndpoint", required = true)
public String getArtifactDownloadEndpoint() {
return artifactDownloadEndpoint;
}
public void setArtifactDownloadEndpoint(String artifactDownloadEndpoint) {
this.artifactDownloadEndpoint = artifactDownloadEndpoint;
}
@XmlElementWrapper(name = "AppCategories")
@XmlElement(name = "Category")
public List<String> getAppCategories() {

@ -42,10 +42,13 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
import org.wso2.carbon.device.application.mgt.common.response.Application;
import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper;
import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO;
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO;
@ -63,6 +66,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagemen
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -117,75 +121,32 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @throws ApplicationManagementException Catch all other throwing exceptions and returns {@link ApplicationManagementException}
*/
@Override
public ApplicationEntity createApplication(ApplicationWrapper applicationWrapper,
public Application createApplication(ApplicationWrapper applicationWrapper,
ApplicationArtifact applicationArtifact) throws RequestValidatingException, ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
ApplicationEntity applicationEntity;
try {
applicationEntity = appWrapperToAppEntity(applicationWrapper);
} catch (UnexpectedServerErrorException e) {
throw new ApplicationManagementException(e.getMessage(), e);
}
Application application;
if (log.isDebugEnabled()) {
log.debug("Application create request is received for the tenant : " + tenantId + " From" + " the user : "
+ userName);
}
ApplicationReleaseEntity applicationReleaseEntity;
List<ApplicationReleaseEntity> applicationReleaseEntities = new ArrayList<>();
try {
applicationReleaseEntity = applicationEntity.getApplicationReleases().get(0);
// The application executable artifacts such as apks are uploaded.
if (!ApplicationType.ENTERPRISE.toString().equals(applicationWrapper.getType())) {
applicationReleaseEntity = applicationStorageManager
.uploadReleaseArtifact(applicationReleaseEntity, applicationEntity.getType(),
applicationEntity.getDeviceTypeName(), null);
} else {
applicationReleaseEntity.setInstallerName(applicationArtifact.getInstallerName());
applicationReleaseEntity = applicationStorageManager
.uploadReleaseArtifact(applicationReleaseEntity, applicationEntity.getType(),
applicationEntity.getDeviceTypeName(), applicationArtifact.getInstallerStream());
}
applicationReleaseEntity.setIconName(applicationArtifact.getIconName());
applicationReleaseEntity.setBannerName(applicationArtifact.getBannername());
Map<String, InputStream> screenshots = applicationArtifact.getScreenshots();
List<String> screenshotNames = new ArrayList<>(screenshots.keySet());
int counter = 1;
for (String scName : screenshotNames) {
if (counter == 1) {
applicationReleaseEntity.setScreenshotName1(scName);
} else if (counter == 2) {
applicationReleaseEntity.setScreenshotName2(scName);
} else if (counter == 3) {
applicationReleaseEntity.setScreenshotName3(scName);
}
counter++;
}
// Upload images
applicationReleaseEntity = applicationStorageManager
.uploadImageArtifacts(applicationReleaseEntity, applicationArtifact.getIconStream(),
applicationArtifact.getBannerStream(),
new ArrayList<>(screenshots.values()));
applicationReleaseEntity.setUuid(UUID.randomUUID().toString());
applicationReleaseEntities.add(applicationReleaseEntity);
applicationEntity.setApplicationReleases(applicationReleaseEntities);
} catch (ResourceManagementException e) {
applicationEntity = addApplicationReleaseArtifacts(appWrapperToAppEntity(applicationWrapper), applicationArtifact);
} catch (UnexpectedServerErrorException e) {
throw new ApplicationManagementException(e.getMessage(), e);
} catch (ResourceManagementException e) {
String msg = "Error Occured when uploading artifacts of the application.: " + applicationWrapper.getName();
log.error(msg);
throw new ApplicationManagementException(msg, e);
}
try {
List<ApplicationReleaseEntity> applicationReleaseEntities = new ArrayList<>();
ApplicationReleaseEntity applicationReleaseEntity;
Filter filter = new Filter();
filter.setFullMatch(true);
filter.setAppName(applicationEntity.getName().trim());
filter.setDeviceTypeId(applicationEntity.getDeviceTypeId());
@ -307,10 +268,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationReleaseEntity.setCurrentState(AppLifecycleState.CREATED.toString());
applicationReleaseEntities.add(applicationReleaseEntity);
applicationEntity.setApplicationReleases(applicationReleaseEntities);
application = appDtoToAppResponse(applicationEntity);
ConnectionManagerUtil.commitDBTransaction();
}
return applicationEntity;
return application;
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while adding lifecycle state. application name: " + applicationWrapper.getName()
@ -348,6 +309,60 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
private ApplicationEntity addApplicationReleaseArtifacts(ApplicationEntity applicationEntity,
ApplicationArtifact applicationArtifact) throws ResourceManagementException {
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
List<ApplicationReleaseEntity> applicationReleaseEntities = new ArrayList<>();
ApplicationReleaseEntity applicationReleaseEntity;
try {
applicationReleaseEntity = applicationEntity.getApplicationReleases().get(0);
// The application executable artifacts such as apks are uploaded.
if (!ApplicationType.ENTERPRISE.toString().equals(applicationEntity.getType())) {
applicationReleaseEntity = applicationStorageManager
.uploadReleaseArtifact(applicationReleaseEntity, applicationEntity.getType(),
applicationEntity.getDeviceTypeName(), null);
} else {
applicationReleaseEntity.setInstallerName(applicationArtifact.getInstallerName());
applicationReleaseEntity = applicationStorageManager
.uploadReleaseArtifact(applicationReleaseEntity, applicationEntity.getType(),
applicationEntity.getDeviceTypeName(), applicationArtifact.getInstallerStream());
}
applicationReleaseEntity.setIconName(applicationArtifact.getIconName());
applicationReleaseEntity.setBannerName(applicationArtifact.getBannername());
Map<String, InputStream> screenshots = applicationArtifact.getScreenshots();
List<String> screenshotNames = new ArrayList<>(screenshots.keySet());
int counter = 1;
for (String scName : screenshotNames) {
if (counter == 1) {
applicationReleaseEntity.setScreenshotName1(scName);
} else if (counter == 2) {
applicationReleaseEntity.setScreenshotName2(scName);
} else if (counter == 3) {
applicationReleaseEntity.setScreenshotName3(scName);
}
counter++;
}
// Upload images
applicationReleaseEntity = applicationStorageManager
.uploadImageArtifacts(applicationReleaseEntity, applicationArtifact.getIconStream(),
applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values()));
applicationReleaseEntity.setUuid(UUID.randomUUID().toString());
applicationReleaseEntities.add(applicationReleaseEntity);
applicationEntity.setApplicationReleases(applicationReleaseEntities);
return applicationEntity;
} catch (RequestValidatingException e) {
// todo remove this exception cath, do this validation in the request validate method
String msg = "Error Occured when uploading artifacts of the application.: " + applicationEntity.getName();
log.error(msg);
throw new ResourceManagementException(msg, e);
}
}
@Override
public ApplicationList getApplications(Filter filter, String deviceTypeName)
throws ApplicationManagementException {
@ -1652,6 +1667,55 @@ public class ApplicationManagerImpl implements ApplicationManager {
return applicationReleaseEntity;
}
private Application appDtoToAppResponse(ApplicationEntity applicationEntity) {
Application application = new Application();
application.setId(applicationEntity.getId());
application.setName(applicationEntity.getName());
application.setDescription(applicationEntity.getDescription());
application.setAppCategory(applicationEntity.getAppCategory());
application.setType(applicationEntity.getType());
application.setSubType(applicationEntity.getSubType());
application.setPaymentCurrency(applicationEntity.getPaymentCurrency());
application.setTags(applicationEntity.getTags());
application.setUnrestrictedRoles(applicationEntity.getUnrestrictedRoles());
application.setDeviceType(applicationEntity.getDeviceTypeName());
List<ApplicationRelease> applicationReleases = applicationEntity.getApplicationReleases()
.stream().map(this::releaseDtoToRelease).collect(Collectors.toList());
application.setApplicationReleases(applicationReleases);
return application;
}
private ApplicationRelease releaseDtoToRelease(ApplicationReleaseEntity applicationReleaseEntity){
String artifactDownloadEndpoint = ConfigurationManager.getInstance().getConfiguration().getArtifactDownloadEndpoint();
String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseEntity.getUuid();
ApplicationRelease applicationRelease = new ApplicationRelease();
applicationRelease.setDescription(applicationReleaseEntity.getDescription());
applicationRelease.setReleaseType(applicationReleaseEntity.getReleaseType());
applicationRelease.setPrice(applicationReleaseEntity.getPrice());
applicationRelease.setIsSharedWithAllTenants(applicationReleaseEntity.getIsSharedWithAllTenants());
applicationRelease.setMetaData(applicationReleaseEntity.getMetaData());
applicationRelease.setUrl(applicationReleaseEntity.getUrl());
applicationRelease.setSupportedOsVersions(applicationReleaseEntity.getSupportedOsVersions());
applicationRelease.setInstallerPath(basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getInstallerName());
applicationRelease.setIconPath(basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getIconName());
applicationRelease.setBannerPath(basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getBannerName());
if (!StringUtils.isEmpty(applicationReleaseEntity.getScreenshotName1())) {
applicationRelease.setScreenshotPath1(
basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getScreenshotName1());
}
if (!StringUtils.isEmpty(applicationReleaseEntity.getScreenshotName2())) {
applicationRelease.setScreenshotPath2(
basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getScreenshotName2());
}
if (!StringUtils.isEmpty(applicationReleaseEntity.getScreenshotName3())) {
applicationRelease.setScreenshotPath3(
basePath + Constants.FORWARD_SLASH + applicationReleaseEntity.getScreenshotName3());
}
return applicationRelease;
}
private <T> DeviceType getDevceTypeData( T deviceTypeAttr)
throws BadRequestException, UnexpectedServerErrorException {
List<DeviceType> deviceTypes;

@ -233,7 +233,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
artifactPath = artifactDirectoryPath + File.separator + applicationRelease.getInstallerName();
saveFile(new ByteArrayInputStream(content), artifactPath);
applicationRelease.setInstallerName(artifactPath);
applicationRelease.setAppHashValue(md5OfApp);
} catch (IOException e) {
String msg = "IO Exception while saving the release artifacts in the server for the application UUID "
@ -265,7 +264,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
}
return applicationRelease;
}
@Override
public void deleteApplicationReleaseArtifacts(String directoryPath) throws ApplicationStorageManagementException {
File artifact = new File(directoryPath);

@ -37,8 +37,7 @@ public class Constants {
public static final String CF_BUNDLE_VERSION = "CFBundleVersion";
public static final String APP_EXTENSION = ".app";
public static final String ANDROID_INSTALLER_EXT = ".apk";
public static final String IOS_INSTALLER_EXT = ".ipa";
public static final String FORWARD_SLASH = "/";
/**
* Database types supported by Application Management.

@ -28,6 +28,7 @@ import org.wso2.carbon.device.application.mgt.common.entity.ApplicationReleaseEn
import org.wso2.carbon.device.application.mgt.common.entity.LifecycleStateEntity;
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.response.Application;
import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
@ -153,7 +154,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
applicationManager
.validateAppCreatingRequest(applicationWrapper, binaryFile, iconFile, bannerFile, attachmentList);
// Created new application entry
ApplicationEntity createdApplication = applicationManager.createApplication(applicationWrapper,
Application createdApplication = applicationManager.createApplication(applicationWrapper,
constructApplicationArtifact(binaryFile, iconFile, bannerFile, attachmentList));
if (createdApplication != null) {
return Response.status(Response.Status.CREATED).entity(createdApplication).build();

@ -21,7 +21,7 @@ PRIMARY KEY (ID)
CREATE TABLE IF NOT EXISTS AP_APP_RELEASE(
ID INTEGER NOT NULL AUTO_INCREMENT,
DESCRIPTION CLOB NOT NULL,
VERSION VARCHAR(10) NOT NULL,
VERSION VARCHAR(20) NOT NULL,
TENANT_ID INTEGER NOT NULL,
UUID VARCHAR(200) NOT NULL,
RELEASE_TYPE VARCHAR(45) NOT NULL,

@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS `APP_MANAGER`.`AP_APP_RELEASE`
(
`ID` INT(11) NOT NULL,
`DESCRIPTION` TEXT NOT NULL,
`VERSION` VARCHAR(10) NOT NULL,
`VERSION` VARCHAR(20) NOT NULL,
`TENANT_ID` INT(11) NOT NULL,
`UUID` VARCHAR(200) NOT NULL,
`RELEASE_TYPE` VARCHAR(45) NOT NULL,

Loading…
Cancel
Save