From c1a23bfed8ef92848b32a078a1f6b8217751b8b3 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 6 Oct 2018 10:35:40 +0530 Subject: [PATCH] Add create new app release API --- .../mgt/core/dao/ApplicationReleaseDAO.java | 12 +- .../GenericApplicationReleaseDAOImpl.java | 37 +++ .../mgt/core/impl/ApplicationManagerImpl.java | 30 ++- .../services/ApplicationManagementAPI.java | 97 +++++++- .../impl/ApplicationManagementAPIImpl.java | 227 +++++++++++++----- 5 files changed, 343 insertions(+), 60 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index b7af88ab95e..0ae1d794a13 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -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. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index ee87089652e..f255a7c74ab 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -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()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 83dfc2cf62c..cfe68cb7f75 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -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(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java index d46e8a0b1a7..1ab0ec9187f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java @@ -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, - @PathParam("uuid") String applicationUuid, - LifecycleState state); + 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, + @ApiParam( + name = "action", + value = "Changing lifecycle state", + required = true) + @QueryParam("action") String action + ); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index 27c63268569..9dd52fa389c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -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) { @@ -147,10 +147,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { ApplicationRelease applicationRelease; List attachmentList = new ArrayList<>(); attachmentList.add(screenshot1); - if(screenshot2 != null) { + if (screenshot2 != null) { attachmentList.add(screenshot2); } - if(screenshot3 != null) { + if (screenshot3 != null) { attachmentList.add(screenshot3); } @@ -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 attachments = new ArrayList<>(); + List 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 attachments = new ArrayList<>();; + List attachments = new ArrayList<>(); if (iconFile != null) { iconFileStream = iconFile.getDataHandler().getInputStream(); @@ -245,18 +331,18 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } attachments.add(screenshot1.getDataHandler().getInputStream()); - if(screenshot2 != null) { + if (screenshot2 != null) { attachments.add(screenshot2.getDataHandler().getInputStream()); } - if(screenshot3 != null) { + if (screenshot3 != null) { 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,8 +398,9 @@ 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: " - + applicationUuid); + 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) { log.error("Error occurred while updating the image artifacts of the application with the uuid " @@ -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); @@ -384,9 +469,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } -// applicationRelease = applicationStorageManager -// .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); -// applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease); + // applicationRelease = applicationStorageManager + // .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); + // applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease); return Response.status(Response.Status.OK).entity(applicationRelease).build(); } catch (ApplicationManagementException e) { @@ -402,8 +487,9 @@ 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: " - + applicationUUID); + log.error( + "Error occured while handling the application release updating request. application release UUID: " + + applicationUUID); return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); } } @@ -462,13 +548,12 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { lifecycleState = applicationManager.getLifecycleState(applicationId, applicationUuid); - } catch (NotFoundException e){ + } catch (NotFoundException e) { String msg = "Couldn't found application lifecycle details for appid: " + applicationId + " 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 @@ -499,37 +591,62 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } private boolean isValidAppCreatingRequest(Attachment binaryFile, Attachment iconFile, Attachment bannerFile, - List attachmentList, Application application){ + List 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 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;