Add create new app release API

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 425938e3a8
commit c1a23bfed8

@ -146,7 +146,17 @@ public interface ApplicationReleaseDAO {
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
boolean verifyReleaseExistenceByHash(int appId, String hashVal, int tenantId) throws ApplicationManagementDAOException;
boolean verifyReleaseExistenceByHash(int appId, String hashVal, int tenantId)
throws ApplicationManagementDAOException;
/**
* To verify whether application release exist or not for the given app release version.
*
* @param appId ID of the application.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
String getPackageName(int appId, int tenantId) throws ApplicationManagementDAOException;
/**
* To verify whether application release exist or not for given application release uuid.

@ -468,6 +468,43 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
}
}
@Override
public String getPackageName(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting package name of the application release by application id:" + appId);
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getDBConnection();
String sql = "SELECT AR.PACKAGE_NAME AS PACKAGE_NAME FROM AP_APP_RELEASE AS AR WHERE AR.AP_APP_ID = ? "
+ "AND AR.TENANT_ID = ? LIMIT 1;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved package name of the application release with the application ID "
+ appId);
}
if (rs.next()){
return rs.getString("PACKAGE_NAME");
}
return null;
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error occurred while getting package name of the application release with app ID: " + appId, e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection to get application release package name.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {

@ -35,8 +35,10 @@ import org.wso2.carbon.device.application.mgt.common.Tag;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.User;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
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.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
@ -247,7 +249,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
try {
ConnectionManagerUtil.openDBConnection();
if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)){
Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (existingApplication == null){
throw new NotFoundException(
"Couldn't found application for the application Id: " + applicationId);
}
@ -256,13 +259,31 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new BadRequestException("Application release exists for the application Id: " + applicationId
+ " and uploaded binary file");
}
String packageName = this.applicationReleaseDAO.getPackageName(applicationId, tenantId);
if (packageName != null && !packageName.equals(applicationRelease.getPackageName())) {
throw new BadRequestException(
"Package name in the payload is different from the existing package name of other application releases.");
}
ConnectionManagerUtil.beginDBTransaction();
applicationRelease = this.applicationReleaseDAO
.createRelease(applicationRelease, application.getId(), tenantId);
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, false);
ConnectionManagerUtil.commitDBTransaction();
return applicationRelease;
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Error occurred while staring application release creating transaction for application Id: "
+ applicationId, e);
} catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Error occurred while adding application release into IoTS app management Application id of the "
+ "application release: " + applicationId, e);
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException(
"Error occurred while adding application release into IoTS app management Application id of the "
@ -930,6 +951,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
"Couldn't found application release for the application Id: " + applicationId
+ " application release uuid: " + releaseUuid);
}
LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid);
if (currentState == null){
throw new ApplicationManagementException(
"Couldn't found latest lifecycle state for the appId: " + applicationId
+ " and application release UUID: " + releaseUuid);
}
state.setPreviousState(currentState.getCurrentState());
}
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();

@ -313,6 +313,79 @@ public interface ApplicationManagementAPI {
@Multipart(value = "screenshot3") Attachment screenshot3
);
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes("multipart/mixed")
@Path("/{deviceType}/{appType}/{appId}")
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Create an application",
notes = "This will create a new application",
tags = "Application Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:update")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 201,
message = "OK. \n Successfully created an application.",
response = Application.class),
@ApiResponse(
code = 400,
message = "Bad Request. \n " +
"Application creating payload contains unacceptable or vulnerable data"),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while creating the application.",
response = ErrorResponse.class)
})
Response createRelease(
@PathParam("deviceType") String deviceType,
@PathParam("appId") String appType,
@PathParam("appId") int appId,
@ApiParam(
name = "applicationRelease",
value = "The application release that need to be created.",
required = true)
@Multipart("applicationRelease") ApplicationRelease applicationRelease,
@ApiParam(
name = "binaryFile",
value = "Binary file of uploading application",
required = true)
@Multipart(value = "binaryFile") Attachment binaryFile,
@ApiParam(
name = "icon",
value = "Icon of the uploading application",
required = true)
@Multipart(value = "icon") Attachment iconFile,
@ApiParam(
name = "banner",
value = "Banner of the uploading application",
required = true)
@Multipart(value = "banner") Attachment bannerFile,
@ApiParam(
name = "screenshot1",
value = "Screen Shots of the uploading application",
required = true)
@Multipart(value = "screenshot1") Attachment screenshot1,
@ApiParam(
name = "screenshot2",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot2") Attachment screenshot2,
@ApiParam(
name = "screenshot3",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot3") Attachment screenshot3
);
@DELETE
@Consumes("application/json")
@Path("/{appid}")
@ -557,6 +630,10 @@ public interface ApplicationManagementAPI {
code = 201,
message = "OK. \n Successfully add a lifecycle state.",
response = Application.class),
@ApiResponse(
code = 400,
message = "Bad Request. \n " +
"Lifecycle State changing request contains unacceptable or vulnerable data"),
@ApiResponse(
code = 404,
message = "NOT FOUND. \n Error occurred while adding new lifecycle state.",
@ -566,7 +643,21 @@ public interface ApplicationManagementAPI {
message = "Internal Server Error. \n Error occurred adding a lifecycle state.",
response = ErrorResponse.class)
})
Response addLifecycleState(@PathParam("appId") int applicationId,
Response addLifecycleState(
@ApiParam(
name = "appId",
value = "Identifier of the Application",
required = true)
@PathParam("appId") int applicationId,
@ApiParam(
name = "uuid",
value = "UUID of the Application Release",
required = true)
@PathParam("uuid") String applicationUuid,
LifecycleState state);
@ApiParam(
name = "action",
value = "Changing lifecycle state",
required = true)
@QueryParam("action") String action
);
}

@ -96,8 +96,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
}
ApplicationList applications = applicationManager.getApplications(filter);
if (applications.getApplications().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).entity
("Couldn't find any application for requested query.").build();
return Response.status(Response.Status.NOT_FOUND)
.entity("Couldn't find any application for requested query.").build();
}
return Response.status(Response.Status.OK).entity(applications).build();
} catch (ApplicationManagementException e) {
@ -183,8 +183,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
}
// Upload images
applicationRelease = applicationStorageManager.uploadImageArtifacts(applicationRelease, iconFileStream,
bannerFileStream, attachments);
applicationRelease = applicationStorageManager
.uploadImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments);
applicationRelease.setUuid(UUID.randomUUID().toString());
applicationReleases.add(applicationRelease);
application.setApplicationReleases(applicationReleases);
@ -202,8 +202,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the releases artifacts of the application "
+ application.getName(), e);
log.error(
"Error occurred while uploading the releases artifacts of the application " + application.getName(),
e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
String errorMessage =
@ -218,24 +219,109 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
}
}
@Override
@PUT
@POST
@Consumes("multipart/mixed")
@Produces(MediaType.APPLICATION_JSON)
@Path("/image-artifacts/{appId}/{uuid}")
public Response updateApplicationImageArtifacts(
@Path("/{deviceType}/{appType}/{appId}")
public Response createRelease(
@PathParam("deviceType") String deviceType,
@PathParam("appId") String appType,
@PathParam("appId") int appId,
@PathParam("uuid") String applicationUuid,
@Multipart("applicationRelease") ApplicationRelease applicationRelease,
@Multipart("binaryFile") Attachment binaryFile,
@Multipart("icon") Attachment iconFile,
@Multipart("banner") Attachment bannerFile,
@Multipart("screenshot1") Attachment screenshot1,
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
InputStream iconFileStream;
InputStream bannerFileStream;
List<InputStream> attachments = new ArrayList<>();
List<Attachment> attachmentList = new ArrayList<>();
attachmentList.add(screenshot1);
if (screenshot2 != null) {
attachmentList.add(screenshot2);
}
if (screenshot3 != null) {
attachmentList.add(screenshot3);
}
try {
if (!isValidReleaseCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, appType)) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
// The application executable artifacts such as apks are uploaded.
if (!ApplicationType.ENTERPRISE.toString().equals(appType)) {
applicationRelease = applicationStorageManager
.uploadReleaseArtifact(applicationRelease, appType, deviceType, null);
} else {
applicationRelease = applicationStorageManager
.uploadReleaseArtifact(applicationRelease, appType, deviceType,
binaryFile.getDataHandler().getInputStream());
if (applicationRelease.getAppStoredLoc() == null || applicationRelease.getAppHashValue() == null) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
iconFileStream = iconFile.getDataHandler().getInputStream();
bannerFileStream = bannerFile.getDataHandler().getInputStream();
for (Attachment screenshot : attachmentList) {
attachments.add(screenshot.getDataHandler().getInputStream());
}
// Upload images
applicationRelease = applicationStorageManager
.uploadImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments);
applicationRelease.setUuid(UUID.randomUUID().toString());
// Created new application release entry
ApplicationRelease release = applicationManager.createRelease(appId, applicationRelease);
if (release != null) {
return Response.status(Response.Status.CREATED).entity(release).build();
} else {
log.error("Application Creation Failed");
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (ResourceManagementException e) {
log.error(
"Error occurred while uploading the releases artifacts of the application ID: " + appId,
e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
String errorMessage =
"Error while uploading binary file and resources for the application release of the application ID: "
+ appId;
log.error(errorMessage, e);
return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e),
Response.Status.INTERNAL_SERVER_ERROR);
} catch (RequestValidatingException e) {
log.error("Error occurred while handling the application creating request");
return APIUtil.getResponse(e, Response.Status.BAD_REQUEST);
}
}
@Override
@PUT
@Consumes("multipart/mixed")
@Produces(MediaType.APPLICATION_JSON)
@Path("/image-artifacts/{appId}/{uuid}")
public Response updateApplicationImageArtifacts(
@PathParam("appId") int appId, @PathParam("uuid") String applicationUuid,
@Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile,
@Multipart("screenshot1") Attachment screenshot1, @Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
try {
InputStream iconFileStream = null;
InputStream bannerFileStream = null;
List<InputStream> attachments = new ArrayList<>();;
List<InputStream> attachments = new ArrayList<>();
if (iconFile != null) {
iconFileStream = iconFile.getDataHandler().getInputStream();
@ -252,11 +338,11 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
attachments.add(screenshot3.getDataHandler().getInputStream());
}
ApplicationManager applicationManager = APIUtil.getApplicationManager();
applicationManager.updateApplicationImageArtifact(appId,
applicationUuid, iconFileStream, bannerFileStream, attachments);
applicationManager.updateApplicationImageArtifact(appId, applicationUuid, iconFileStream, bannerFileStream,
attachments);
return Response.status(Response.Status.OK).entity("Successfully uploaded artifacts for the application "
+ applicationUuid).build();
return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application " + applicationUuid).build();
} catch (NotFoundException e) {
log.error(e.getMessage(), e);
return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
@ -267,8 +353,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} catch (IOException e) {
String msg = "Exception while trying to read icon, banner files for the application " + applicationUuid;
log.error(msg);
return APIUtil.getResponse(new ApplicationManagementException(msg, e),
Response.Status.INTERNAL_SERVER_ERROR);
return APIUtil
.getResponse(new ApplicationManagementException(msg, e), Response.Status.INTERNAL_SERVER_ERROR);
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the image artifacts of the application with the uuid "
+ applicationUuid, e);
@ -296,14 +382,13 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
APIUtil.getApplicationManager().updateApplicationArtifact(applicationId, applicationUuid,
binaryFile.getDataHandler().getInputStream());
return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application release. UUID is " + applicationUuid).build();
.entity("Successfully uploaded artifacts for the application release. UUID is " + applicationUuid)
.build();
} catch (IOException e) {
String msg =
"Error occurred while trying to read icon, banner files for the application release" +
applicationUuid;
String msg = "Error occurred while trying to read icon, banner files for the application release"
+ applicationUuid;
log.error(msg);
return APIUtil.getResponse(new ApplicationManagementException(msg, e),
Response.Status.BAD_REQUEST);
return APIUtil.getResponse(new ApplicationManagementException(msg, e), Response.Status.BAD_REQUEST);
} catch (ResourceManagementException e) {
log.error("Error occurred while uploading the image artifacts of the application with the uuid "
+ applicationUuid, e);
@ -313,7 +398,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
+ applicationUuid, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (RequestValidatingException e) {
log.error("Error occured while handling the application artifact updating request. application release UUID: "
log.error(
"Error occured while handling the application artifact updating request. application release UUID: "
+ applicationUuid);
return APIUtil.getResponse(e, Response.Status.BAD_REQUEST);
} catch (DeviceManagementException e) {
@ -360,8 +446,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
try {
Application application = applicationManager.getApplicationIfAccessible(applicationId);
if (!applicationManager.isAcceptableAppReleaseUpdate(application.getId(),
applicationRelease.getUuid())) {
if (!applicationManager.isAcceptableAppReleaseUpdate(application.getId(), applicationRelease.getUuid())) {
String msg = "Application release is in the " + applicationRelease.getLifecycleState().getCurrentState()
+ " state. Hence updating is not acceptable when application in this state";
log.error(msg);
@ -402,7 +487,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
+ applicationUUID + " for the release " + applicationRelease.getVersion(), e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} catch (RequestValidatingException e) {
log.error("Error occured while handling the application release updating request. application release UUID: "
log.error(
"Error occured while handling the application release updating request. application release UUID: "
+ applicationUUID);
return APIUtil.getResponse(e, Response.Status.BAD_REQUEST);
}
@ -467,8 +553,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
+ " and app release UUID: " + applicationUuid;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).build();
}
catch (ApplicationManagementException e) {
} catch (ApplicationManagementException e) {
String msg = "Error occurred while getting lifecycle state.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
@ -481,9 +566,16 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
public Response addLifecycleState(
@PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuid,
LifecycleState state) {
@QueryParam("action") String action) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
if (action == null || action.isEmpty()) {
String msg = "The Action is null or empty. Please check the request";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build();
}
LifecycleState state = new LifecycleState();
state.setCurrentState(action);
applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true);
} catch (NotFoundException e) {
String msg = "Could,t find application release for application id: " + applicationId
@ -502,34 +594,59 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
List<Attachment> attachmentList, Application application) {
if (application.getApplicationReleases().size() > 1) {
log.error(
"Invalid application creating request. Application creating request must have single application "
+ "release. Application name:" + application.getName() + " and type: " +
application.getType());
log.error("Invalid application creating request. Application creating request must have single application "
+ "release. Application name:" + application.getName() + " and type: " + application.getType());
return false;
}
if (iconFile == null) {
log.error("Icon file is not found for the application release. Application name: " +
application.getName() + " and type: " + application.getType());
log.error("Icon file is not found for the application release. Application name: " + application.getName()
+ " and type: " + application.getType());
return false;
}
if (bannerFile == null) {
log.error("Banner file is not found for the application release. Application name: " +
application.getName() + " and application type: " + application.getType());
log.error("Banner file is not found for the application release. Application name: " + application.getName()
+ " and application type: " + application.getType());
return false;
}
if (attachmentList == null || attachmentList.isEmpty()) {
log.error("Screenshots are not found for the application release. Application name: " +
application.getName() + " Application type: " + application.getType());
log.error(
"Screenshots are not found for the application release. Application name: " + application.getName()
+ " Application type: " + application.getType());
return false;
}
if (binaryFile == null && ApplicationType.ENTERPRISE.toString().equals(application.getType())) {
log.error("Binary file is not found for the application release. Application name: "
+ application.getName() + " Application type: " + application.getType());
log.error("Binary file is not found for the application release. Application name: " + application.getName()
+ " Application type: " + application.getType());
return false;
}
return true;
}
private boolean isValidReleaseCreatingRequest(Attachment binaryFile, Attachment iconFile, Attachment bannerFile,
List<Attachment> attachmentList, String appType) {
if (iconFile == null) {
log.error("Icon file is not found with the application release creating request.");
return false;
}
if (bannerFile == null) {
log.error("Banner file is not found with the application release creating request.");
return false;
}
if (attachmentList == null || attachmentList.isEmpty()) {
log.error("Screenshots are not found with the application release creating request.");
return false;
}
if (binaryFile == null && ApplicationType.ENTERPRISE.toString().equals(appType)) {
log.error("Binary file is not found with the application release creating request. Application type: "
+ appType);
return false;
}
return true;

Loading…
Cancel
Save