adding application release creation functioanlity (NC)

feature/appm-store/pbac
lasantha 7 years ago
parent f8c5ff95b9
commit 486aac1cdc

@ -68,11 +68,13 @@ public class ApplicationRelease {
private ImageArtifact icon;
private String iconLoc;
private ImageArtifact banner;
private String currentState;
private String previouseState;
private String previousState;
private String stateModifiedBy;
@ -162,12 +164,12 @@ public class ApplicationRelease {
this.currentState = currentState;
}
public String getPreviouseState() {
return previouseState;
public String getPreviousState() {
return previousState;
}
public void setPreviouseState(String previouseState) {
this.previouseState = previouseState;
public void setPreviousState(String previousState) {
this.previousState = previousState;
}
public String getStateModifiedBy() {
@ -305,4 +307,12 @@ public class ApplicationRelease {
public void setModifiedAt(Timestamp modifiedAt) {
this.modifiedAt = modifiedAt;
}
public String getIconLoc() {
return iconLoc;
}
public void setIconLoc(String iconLoc) {
this.iconLoc = iconLoc;
}
}

@ -96,4 +96,13 @@ public interface ApplicationManager {
* @throws ApplicationManagementException Application Management Exception.
*/
Application getApplication(String appType, String appName) throws ApplicationManagementException;
/**
* To get Application with the given UUID.
*
* @param appId ID of the Application
* @return the boolean value, whether application exist or not
* @throws ApplicationManagementException Application Management Exception.
*/
Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException;
}

@ -19,6 +19,7 @@
package org.wso2.carbon.device.application.mgt.common.services;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ImageArtifact;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
@ -33,23 +34,24 @@ public interface ApplicationStorageManager {
/**
* To upload image artifacts related with an Application.
*
* @param applicationUUID UUID of the application
* @param applicationId ID of the application
* @param applicationRelease ApplicationRelease Object
* @param iconFile Icon File input stream
* @param bannerFile Banner File input stream
* @throws ResourceManagementException Resource Management Exception.
*/
void uploadImageArtifacts(String applicationUUID, InputStream iconFile, InputStream bannerFile,
List<InputStream> screenshots) throws ResourceManagementException;
ApplicationRelease uploadImageArtifacts(int applicationId, ApplicationRelease applicationRelease,
InputStream iconFile, InputStream bannerFile, List<InputStream> screenshots) throws ResourceManagementException;
/**
* To upload release artifacts for an Application.
*
* @param applicationUUID UUID of the application related with the release.
* @param versionName Name of version of the Applcation Release.
* @param applicationId UUID of the application related with the release.
* @param applicationRelease Application Release Object.
* @param binaryFile Binary File for the release.
* @throws ResourceManagementException Resource Management Exception.
*/
void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile)
ApplicationRelease uploadReleaseArtifacts(int applicationId, ApplicationRelease applicationRelease, InputStream binaryFile)
throws ResourceManagementException;
/**

@ -84,6 +84,15 @@ public interface ApplicationDAO {
*/
Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException;
/**
* To get the application with the given uuid
*
* @param appId ID of the application
* @return the boolean value
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException;
/**
* To get the application id of the application specified by the UUID
*

@ -343,6 +343,46 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()){
log.debug("Getting application with the application ID(" + appId + " ) from the database");
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
Boolean isAppExist = false;
try {
conn = this.getDBConnection();
String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY "
+ "AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID FROM "
+ "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved basic details of the application with the application ID " + appId);
}
if (rs.next()){
isAppExist = true;
}
return isAppExist;
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error occurred while getting application details with app ID " + appId + " While executing query ", e);
}
catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public Application editApplication(Application application, int tenantId) throws ApplicationManagementException {
Connection conn;

@ -152,7 +152,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
applicationRelease.setPublishedAt(resultSet.getTimestamp("PUBLISHED_AT"));
applicationRelease.setStarts(resultSet.getInt("STARS"));
applicationRelease.setCurrentState(resultSet.getString("CURRENT_STATE"));
applicationRelease.setPreviouseState(resultSet.getString("PREVIOUSE_STATE"));
applicationRelease.setPreviousState(resultSet.getString("PREVIOUSE_STATE"));
applicationRelease.setStateModifiedBy(resultSet.getString("UPDATED_BY"));
applicationRelease.setStateModifiedAt(resultSet.getTimestamp("UPDATED_AT"));
}

@ -406,6 +406,18 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException{
try {
Boolean isAppExist;
ConnectionManagerUtil.openDBConnection();
isAppExist = ApplicationManagementDAOFactory.getApplicationDAO().verifyApplicationExistenceById(appId);
return isAppExist;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
/**
* To check whether current user is application owner or admin.
*

@ -19,8 +19,11 @@
package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ImageArtifact;
@ -28,7 +31,9 @@ import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManage
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
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.application.mgt.core.util.StorageManagementUtil;
@ -62,109 +67,93 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
}
@Override
public void uploadImageArtifacts(String applicationUUID, InputStream iconFileStream, InputStream bannerFileStream,
List<InputStream> screenShotStreams) throws ResourceManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// Application application = validateApplication(applicationUUID);
// String artifactDirectoryPath = storagePath + application.getId();
// if (log.isDebugEnabled()) {
// log.debug("Artifact Directory Path for saving the artifacts related with application " + applicationUUID
// + " is " + artifactDirectoryPath);
// }
// StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
// if (iconFileStream != null) {
// try {
// saveFile(iconFileStream, artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0]);
// } catch (IOException e) {
// throw new ApplicationStorageManagementException(
// "IO Exception while saving the icon file in the server for " + "the application "
// + applicationUUID, e);
// }
// }
// if (bannerFileStream != null) {
// try {
// saveFile(bannerFileStream, artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[1]);
// } catch (IOException e) {
// throw new ApplicationStorageManagementException(
// "IO Exception while saving the banner file in the server for" + " the application "
// + applicationUUID, e);
// }
// }
// if (screenShotStreams != null) {
// int count = application.getScreenShotCount() + 1;
// boolean maxCountReached = false;
//
// if (count > screenShotMaxCount) {
// log.error("Maximum limit for the screen-shot is " + screenShotMaxCount
// + " Cannot upload another screenshot for the application with the UUID " + applicationUUID);
// maxCountReached = true;
// }
// String screenshotName;
//
// if (maxCountReached) {
// return;
// }
// for (InputStream screenshotStream : screenShotStreams) {
// try {
// screenshotName = Constants.IMAGE_ARTIFACTS[2] + count;
// saveFile(screenshotStream, artifactDirectoryPath + File.separator + screenshotName);
// count++;
// if (count > screenShotMaxCount) {
// log.error("Maximum limit for the screen-shot is " + screenShotMaxCount
// + " Cannot upload another screenshot for the application with the UUID "
// + applicationUUID);
// break;
// }
// } catch (IOException e) {
// throw new ApplicationStorageManagementException(
// "IO Exception while saving the screens hots for the " + "application " + applicationUUID,
// e);
// }
// }
// try {
// ConnectionManagerUtil.beginDBTransaction();
// ApplicationManagementDAOFactory.getApplicationDAO().updateScreenShotCount(applicationUUID, tenantId, count - 1);
// ConnectionManagerUtil.commitDBTransaction();
// } catch (TransactionManagementException e) {
// ConnectionManagerUtil.rollbackDBTransaction();
// throw new ApplicationStorageManagementException("Transaction Management exception while trying to "
// + "update the screen-shot count of the application " + applicationUUID + " for the tenant "
// + tenantId, e);
// } catch (DBConnectionException e) {
// ConnectionManagerUtil.rollbackDBTransaction();
// throw new ApplicationStorageManagementException("Database connection management exception while "
// + "trying to update the screen-shot count for the application " + applicationUUID + " for the"
// + " tenant " + tenantId, e);
// } catch (ApplicationManagementDAOException e) {
// ConnectionManagerUtil.rollbackDBTransaction();
// throw new ApplicationStorageManagementException("Application Management DAO exception while trying to"
// + " update the screen-shot count for the application " + applicationUUID + " for the tenant "
// + tenantId, e);
// } finally {
// ConnectionManagerUtil.closeDBConnection();
// }
// }
public ApplicationRelease uploadImageArtifacts(int applicationId, ApplicationRelease applicationRelease,
InputStream iconFileStream, InputStream bannerFileStream, List<InputStream> screenShotStreams) throws ResourceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String artifactDirectoryPath = null;
String iconStoredLocation;
String bannerStoredLocation;
String scStoredLocation;
try {
if (validateApplication(applicationId)) {
artifactDirectoryPath = storagePath + applicationRelease.getAppHashValue();
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
}
if (artifactDirectoryPath != null) {
iconStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0];
bannerStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[1];
saveFile(iconFileStream, iconStoredLocation);
saveFile(bannerFileStream, bannerStoredLocation);
applicationRelease.setIconLoc(iconStoredLocation);
applicationRelease.setBannerLoc(bannerStoredLocation);
if (screenShotStreams.size() > screenShotMaxCount) {
throw new ApplicationStorageManagementException("Maximum limit for the screen-shot exceeds");
}
int count = 1;
for (InputStream screenshotStream : screenShotStreams) {
scStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[2] + count;
if (count == 1) {
applicationRelease.setScreenshotLoc1(scStoredLocation);
}
if (count == 2) {
applicationRelease.setScreenshotLoc2(scStoredLocation);
}
if (count == 3) {
applicationRelease.setScreenshotLoc3(scStoredLocation);
}
saveFile(screenshotStream, scStoredLocation);
count++;
}
}
return applicationRelease;
} catch (IOException e) {
throw new ApplicationStorageManagementException(
"IO Exception while saving the screens hots for the " + "application " + applicationId, e);
} catch (ApplicationStorageManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationStorageManagementException("Application Management DAO exception while trying to"
+ " update the screen-shot count for the application " + applicationId + " for the tenant "
+ tenantId, e);
}
}
@Override
public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile)
public ApplicationRelease uploadReleaseArtifacts(int applicationId, ApplicationRelease applicationRelease , InputStream binaryFile)
throws ResourceManagementException {
Application application = validateApplication(applicationUUID);
String artifactDirectoryPath = storagePath + application.getId();
String artifactDirectoryPath;
String md5OfApp;
md5OfApp = getMD5(binaryFile);
if(validateApplication(applicationId) && md5OfApp != null){
artifactDirectoryPath = storagePath + md5OfApp;
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
if (log.isDebugEnabled()) {
log.debug("Artifact Directory Path for saving the application release related artifacts related with "
+ "application " + applicationUUID + " is " + artifactDirectoryPath);
+ "application ID " + applicationId + " is " + artifactDirectoryPath);
}
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
if (binaryFile != null) {
try {
saveFile(binaryFile, artifactDirectoryPath + File.separator + versionName);
saveFile(binaryFile, artifactDirectoryPath);
applicationRelease.setAppStoredLoc(artifactDirectoryPath);
applicationRelease.setAppHashValue(md5OfApp);
} catch (IOException e) {
throw new ApplicationStorageManagementException(
"IO Exception while saving the release artifacts in the server for the application "
+ applicationUUID, e);
+ applicationId, e);
}
}else{
log.error("Verify application existence and md5sum value retrieving process");
}
return applicationRelease;
}
@Override
@ -278,24 +267,32 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
/**
* To validate the Application before storing and retrieving the artifacts of a particular application.
*
* @param uuid UUID of the Application
* @param appId ID of the Application
* @return {@link Application} if it is validated
* @throws ApplicationStorageManagementException Application Storage Management Exception will be thrown if a
* valid application related with the specific UUID
* could not be found.
*/
private Application validateApplication(String uuid) throws ApplicationStorageManagementException {
Application application;
private Boolean validateApplication(int appId) throws ApplicationStorageManagementException {
Boolean isAppExist;
try {
application = DataHolder.getInstance().getApplicationManager().getApplication(uuid);
isAppExist = DataHolder.getInstance().getApplicationManager().verifyApplicationExistenceById(appId);
} catch (ApplicationManagementException e) {
throw new ApplicationStorageManagementException(
"Exception while retrieving the application details for the application with UUID "
+ uuid);
"Exception while verifing the application existence for the application with ID "+ appId);
}
return isAppExist;
}
if (application == null) {
throw new ApplicationStorageManagementException("Application with UUID " + uuid + " does not exist.");
private String getMD5(InputStream binaryFile) throws ApplicationStorageManagementException {
String md5;
try {
md5 = DigestUtils.md5Hex(IOUtils.toByteArray(binaryFile));
} catch (IOException e) {
throw new ApplicationStorageManagementException
("IO Exception while trying to get the md5sum value of application");
}
return application;
return md5;
}
}

@ -44,13 +44,11 @@ public class StorageManagementUtil {
public static void createArtifactDirectory(String artifactDirectoryPath) throws ResourceManagementException {
File artifactDirectory = new File(artifactDirectoryPath);
if (!artifactDirectory.exists()) {
if (!artifactDirectory.mkdirs()) {
if (!artifactDirectory.exists() && !artifactDirectory.mkdirs()) {
throw new ResourceManagementException(
"Cannot create directories in the path to save the application related artifacts");
}
}
}
/**
* To delete a directory recursively
@ -81,9 +79,9 @@ public class StorageManagementUtil {
outStream = new FileOutputStream(new File(path));
outStream.write(buffer);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outStream != null) {
outStream.close();
}

@ -23,12 +23,9 @@ import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ -136,9 +133,7 @@ public interface ApplicationReleaseManagementAPI {
code = 500,
message = "Internal Server Error. \n Error occurred while releasing the application.",
response = ErrorResponse.class)
})
Response createApplicationRelease(
}) Response createApplicationRelease(
@Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease,
@ApiParam(
name = "binaryFile",

@ -26,25 +26,19 @@ import org.wso2.carbon.device.application.mgt.common.*;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.device.application.mgt.publisher.api.APIUtil;
import org.wso2.carbon.device.application.mgt.publisher.api.FileStreamingOutput;
import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementAPI;
import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationReleaseManagementAPI;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@ -82,7 +76,7 @@ public class ApplicationReleaseManagementAPIImpl implements ApplicationReleaseMa
throw new ApplicationManagementException(
"Icon file is not uploaded for the application release of " + applicationId); }
if (bannerFile != null) {
if (bannerFile == null) {
throw new ApplicationManagementException(
"Banner file is not uploaded for the application release of " + applicationId); }
@ -102,12 +96,16 @@ public class ApplicationReleaseManagementAPIImpl implements ApplicationReleaseMa
attachments.add(screenshot.getDataHandler().getInputStream());
}
applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(),
applicationRelease = applicationStorageManager.uploadReleaseArtifacts(applicationId, applicationRelease,
binaryFile.getDataHandler().getInputStream());
if(applicationRelease.getAppStoredLoc() == null || applicationRelease.getAppHashValue() == null){
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
applicationRelease = applicationStorageManager.uploadImageArtifacts(applicationId, applicationRelease,
iconFileStream, bannerFileStream, attachments);
// ToDo
applicationRelease.setUuid(UUID.randomUUID().toString());
applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease);
@ -131,42 +129,6 @@ public class ApplicationReleaseManagementAPIImpl implements ApplicationReleaseMa
}
}
@Override
@POST
@Path("/release/{uuid}")
public Response createApplicationRelease(@PathParam("uuid") String applicationUUID,
@Multipart("applicationRelease") ApplicationRelease applicationRelease,
@Multipart("binaryFile") Attachment binaryFile) {
ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try {
applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease);
if (binaryFile != null) {
applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(),
binaryFile.getDataHandler().getInputStream());
}
return Response.status(Response.Status.CREATED).entity(applicationRelease).build();
} catch (ApplicationManagementException e) {
log.error("Error while creating an application release for the application with UUID " + applicationUUID,
e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
String errorMessage =
"Error while uploading binary file for the application release of the application with UUID "
+ applicationUUID;
log.error(errorMessage, e);
return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e),
Response.Status.INTERNAL_SERVER_ERROR);
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the releases artifacts of the application with the uuid "
+ applicationUUID + " for the release " + applicationRelease.getVersion(), e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Override
@POST
@Path("/upload-image-artifacts/{uuid}")

Loading…
Cancel
Save