From 8557980395efba89bac1bf6dec2dd715ee46b835 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Mon, 27 Apr 2020 23:41:37 +0530 Subject: [PATCH] Improve APPM application retrieving APIs --- .../mgt/core/impl/ApplicationManagerImpl.java | 98 ++++++++++--------- .../ApplicationManagementPublisherAPI.java | 14 ++- ...pplicationManagementPublisherAdminAPI.java | 10 +- ...ApplicationManagementPublisherAPIImpl.java | 6 +- 4 files changed, 68 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/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 be8158585f..0fde607bba 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 @@ -1153,49 +1153,50 @@ public class ApplicationManagerImpl implements ApplicationManager { public Application getApplicationById(int appId, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - boolean isVisibleApp = false; ApplicationDTO applicationDTO = getApplication(appId); - try { ConnectionManagerUtil.openDBConnection(); - List filteredApplicationReleaseDTOs = new ArrayList<>(); - for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { - if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && ( - state == null || applicationReleaseDTO.getCurrentState().equals(state))) { - filteredApplicationReleaseDTOs.add(applicationReleaseDTO); - } + List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(appId, tenantId); + if (!unrestrictedRoles.isEmpty() && !hasUserRole(unrestrictedRoles, userName)) { + String msg = "You are trying to access visibility restricted application and you are not assigned" + + " required roles to view this application,"; + log.error(msg); + throw new ForbiddenException(msg); } - if (state != null && filteredApplicationReleaseDTOs.isEmpty()) { + if (lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { return null; } - applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs); - - List tags = this.applicationDAO.getAppTags(appId, tenantId); - List categories = this.applicationDAO.getAppCategories(appId, tenantId); - applicationDTO.setTags(tags); - if (!categories.isEmpty()){ - applicationDTO.setAppCategories(categories); - } - List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(appId, tenantId); - if (!unrestrictedRoles.isEmpty()) { - if (hasUserRole(unrestrictedRoles, userName)) { - isVisibleApp = true; + List filteredApplicationReleaseDTOs = new ArrayList<>(); + AtomicBoolean isDeletableApp = new AtomicBoolean(true); + AtomicBoolean isHideableApp = new AtomicBoolean(true); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) { + if (isHideableApp.get()) { + isHideableApp.set(false); + } + if (isDeletableApp.get() && !lifecycleStateManager + .isDeletableState(applicationReleaseDTO.getCurrentState())) { + isDeletableApp.set(false); + } + if (state == null || state.equals(applicationReleaseDTO.getCurrentState())) { + filteredApplicationReleaseDTOs.add(applicationReleaseDTO); + } } - } else { - isVisibleApp = true; } - if (!isVisibleApp) { - String msg = "You are trying to access visibility restricted application. You don't have required " - + "roles to view this application,"; - log.error(msg); - throw new ForbiddenException(msg); - } - return APIUtil.appDtoToAppResponse(applicationDTO); + applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs); + applicationDTO.setTags(this.applicationDAO.getAppTags(appId, tenantId)); + applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(appId, tenantId)); + + Application application = APIUtil.appDtoToAppResponse(applicationDTO); + application.setHideableApp(isHideableApp.get()); + application.setDeletableApp(isDeletableApp.get()); + return application; } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the database connection to get application for application ID: " - + appId; + String msg = + "Error occurred while obtaining the database connection to get application for application ID: " + + appId; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (LifecycleManagementException e) { @@ -1219,7 +1220,6 @@ public class ApplicationManagerImpl implements ApplicationManager { public Application getApplicationByUuid(String releaseUuid) throws ApplicationManagementException{ int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - boolean isVisibleApp = false; try { ConnectionManagerUtil.openDBConnection(); @@ -1230,34 +1230,36 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new NotFoundException(msg); } - List tags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId); - List categories = this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); - applicationDTO.setTags(tags); - applicationDTO.setAppCategories(categories); - - List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId); - if (!unrestrictedRoles.isEmpty()) { - if (hasUserRole(unrestrictedRoles, userName)) { - isVisibleApp = true; - } - } else { - isVisibleApp = true; + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + if (lifecycleStateManager.isEndState(applicationReleaseDTO.getCurrentState())) { + return null; } - if (!isVisibleApp) { + List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId); + if (!unrestrictedRoles.isEmpty() && !hasUserRole(unrestrictedRoles, userName)) { String msg = "You are trying to access visibility restricted application. You don't have required " + "roles to view this application,"; log.error(msg); throw new ForbiddenException(msg); } - return APIUtil.appDtoToAppResponse(applicationDTO); + + applicationDTO.setTags(this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId)); + applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId)); + + Application application = APIUtil.appDtoToAppResponse(applicationDTO); + if (lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { + ApplicationDTO entireApplication = applicationDAO.getApplication(applicationDTO.getId(), tenantId); + application.setDeletableApp(isDeletableApp(entireApplication.getApplicationReleaseDTOs())); + } + return application; } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to get application for application " + "release UUID: " + releaseUuid; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (UserStoreException e) { - String msg = "User-store exception occurred while getting application for application release UUID " + releaseUuid; + String msg = "User-store exception occurred while getting application for application release UUID " + + releaseUuid; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index 1d969f0881..00533b2a98 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -165,10 +165,13 @@ public interface ApplicationManagementPublisherAPI { response = ApplicationDTO.class), @ApiResponse( code = 403, - message = "Don't have permission to access the application"), + message = "Forbidden. \n Don't have permission to access the application"), @ApiResponse( code = 404, - message = "Application not found"), + message = "Not Found. \n Application not found"), + @ApiResponse( + code = 409, + message = "Conflict. \n Couldn't find an active application"), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while getting relevant application.", @@ -211,10 +214,13 @@ public interface ApplicationManagementPublisherAPI { response = ApplicationDTO.class), @ApiResponse( code = 403, - message = "Don't have permission to access the application release"), + message = "Forbidden. \n Don't have permission to access the application release"), @ApiResponse( code = 404, - message = "Application release not found"), + message = "Not Found. \n Application release not found"), + @ApiResponse( + code = 409, + message = "Conflict. \n Application release is in the end state of lifecycle flow"), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while getting relevant application release.", 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/admin/ApplicationManagementPublisherAdminAPI.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/admin/ApplicationManagementPublisherAdminAPI.java index 006528d561..9f69bc152f 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/admin/ApplicationManagementPublisherAdminAPI.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/admin/ApplicationManagementPublisherAdminAPI.java @@ -313,8 +313,8 @@ public interface ApplicationManagementPublisherAdminAPI { consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", - value = "Retire the application with the given UUID", - notes = "This will retire the application with the given UUID", + value = "Retire the application with the given app Id", + notes = "This will retire the application with the given app Id", tags = "Application Management", extensions = { @Extension(properties = { @@ -326,7 +326,7 @@ public interface ApplicationManagementPublisherAdminAPI { value = { @ApiResponse( code = 200, - message = "OK. \n Successfully deleted the application identified by UUID.", + message = "OK. \n Successfully deleted the application identified by app Id.", response = List.class), @ApiResponse( code = 500, @@ -341,8 +341,8 @@ public interface ApplicationManagementPublisherAdminAPI { }) Response retireApplication( @ApiParam( - name = "UUID", - value = "Unique identifier of the ApplicationDTO", + name = "appId", + value = "Application Id", required = true) @PathParam("appId") int applicationId ); 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index d9e0a321d1..a7dc95940f 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -116,9 +116,9 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem try { Application application = applicationManager.getApplicationById(appId, state); if (application == null){ - String msg = "Could not found an application release which is in " + state + " state."; + String msg = "Could not found an active application which has Id: " + appId; log.error(msg); - return Response.status(Response.Status.OK).entity(msg).build(); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); } return Response.status(Response.Status.OK).entity(application).build(); } catch (NotFoundException e) { @@ -148,7 +148,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem if (application == null){ String msg = "Application release is in the end state of the application lifecycle flow."; log.error(msg); - return Response.status(Response.Status.OK).entity(msg).build(); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); } return Response.status(Response.Status.OK).entity(application).build(); } catch (NotFoundException e) {