From d79c11c7627164678f4513f030196a2917431644 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 1 May 2019 19:48:27 +0530 Subject: [PATCH] Add admin API interface for app publisher --- .../common/services/ApplicationManager.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 90 ++++-------- .../ApplicationManagementPublisherAPI.java | 17 ++- ...pplicationManagementPublisherAdminAPI.java | 136 ++++++++++++++++++ ...ApplicationManagementPublisherAPIImpl.java | 53 +------ ...cationManagementPublisherAdminAPIImpl.java | 110 ++++++++++++++ 6 files changed, 293 insertions(+), 115 deletions(-) create mode 100644 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 create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 1a9cc58b6c3..12f64c65866 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -68,7 +68,7 @@ public interface ApplicationManager { * @param applicationId ID for tha application * @throws ApplicationManagementException ApplicationDTO Management Exception */ - List deleteApplication(int applicationId) throws ApplicationManagementException; + void deleteApplication(int applicationId) throws ApplicationManagementException; /** * Delete an application identified by the unique ID. 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 4a161c6a4e6..68f64b4a96e 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 @@ -476,8 +476,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - - } catch (IOException e) { String msg = "Error occurred when getting byte array of binary file. Installer name: " + applicationArtifact @@ -1045,80 +1043,52 @@ public class ApplicationManagerImpl implements ApplicationManager { // return applicationReleases; // } - @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + @Override + public void deleteApplication(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); List storedLocations = new ArrayList<>(); - ApplicationDTO application; - + ApplicationDTO applicationDTO; + if (log.isDebugEnabled()) { + log.debug("Request is received to delete applications which are related with the application id " + + applicationId); + } try { ConnectionManagerUtil.beginDBTransaction(); - application = this.applicationDAO.getApplicationById(applicationId, tenantId); - - if (application == null) { - throw new NotFoundException("Couldn't found an application for ApplicationDTO ID: " + applicationId); - } + applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application - .getUnrestrictedRoles().isEmpty() && hasUserRole(application.getUnrestrictedRoles(), userName)) { - throw new ForbiddenException( - "You don't have permission to delete this application. In order to delete an application you " - + "need to have required permission. ApplicationDTO ID: " + applicationId); - } - List applicationReleases = getReleases(application, null); - if (log.isDebugEnabled()) { - log.debug("Request is received to delete applications which are related with the application id " - + applicationId); + if (applicationDTO == null) { + throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId); } - for (ApplicationReleaseDTO applicationRelease : applicationReleases) { - LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO - .getLatestLifeCycleState(applicationId, applicationRelease.getUuid()); - LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), - appLifecycleState.getCurrentState()); - if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState(), userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - String currentState = appLifecycleState.getCurrentState(); - List lifecycleFlow = searchLifecycleStateFlow(currentState, - AppLifecycleState.REMOVED.toString()); - for (String nextState : lifecycleFlow) { - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); - if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Can't delete application release which has the UUID:" + applicationRelease - .getUuid() - + " and its belongs to the application which has application ID:" - + applicationId + " You have to move the lifecycle state from " - + currentState + " to acceptable state"); - } - currentState = nextState; - } + List applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); + List activeApplicationReleaseDTOs = new ArrayList<>(); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())){ + activeApplicationReleaseDTOs.add(applicationReleaseDTO); } - storedLocations.add(applicationRelease.getAppHashValue()); + storedLocations.add(applicationReleaseDTO.getAppHashValue()); + } + if (!activeApplicationReleaseDTOs.isEmpty()) { + String msg = "There are application releases which are not in the state " + lifecycleStateManager + .getEndState() + ". Hence you are not allowed to delete the application"; + log.error(msg); + throw new ForbiddenException(msg); } this.applicationDAO.deleteApplication(applicationId); ConnectionManagerUtil.commitDBTransaction(); - } catch (UserStoreException e) { - String msg = "Error occured while check whether current user has the permission to delete an application"; + applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting application data for application id: " + applicationId; log.error(msg); throw new ApplicationManagementException(msg, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occured while changing the application lifecycle state into REMOVED state."; + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred when deleting application artifacts in the file system. Application id: " + + applicationId; log.error(msg); - throw new ApplicationManagementException(msg, e); + throw new ApplicationManagementException(msg); } finally { ConnectionManagerUtil.closeDBConnection(); } - return storedLocations; } private List searchLifecycleStateFlow(String start, String finish) throws ApplicationManagementException { 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 30dbfb2fb40..8b7b1093cba 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 @@ -80,7 +80,7 @@ import javax.ws.rs.core.Response; name = "Get ApplicationDTO Details", description = "Get application details", key = "perm:app:publisher:view", - permissions = {"/app-mgt/publisher/application/update"} + permissions = {"/app-mgt/publisher/application/view"} ), @Scope( name = "Update an ApplicationDTO", @@ -91,8 +91,7 @@ import javax.ws.rs.core.Response; } ) @Path("/applications") -@Api(value = "ApplicationDTO Management", description = "This API carries all application management related operations " + - "such as get all the applications, add application, etc.") +@Api(value = "ApplicationDTO Management") @Produces(MediaType.APPLICATION_JSON) public interface ApplicationManagementPublisherAPI { @@ -377,7 +376,7 @@ public interface ApplicationManagementPublisherAPI { @DELETE @Consumes("application/json") - @Path("/{appid}") + @Path("/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -400,14 +399,20 @@ public interface ApplicationManagementPublisherAPI { @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while deleting the application.", - response = ErrorResponse.class) + response = ErrorResponse.class), + @ApiResponse( + code = 403, + message = "Don't have permission to delete the application"), + @ApiResponse( + code = 404, + message = "Application not found"), }) Response deleteApplication( @ApiParam( name = "UUID", value = "Unique identifier of the ApplicationDTO", required = true) - @PathParam("appid") int applicationId + @PathParam("appId") int applicationId ); @PUT 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 new file mode 100644 index 00000000000..b8b973d5820 --- /dev/null +++ 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 @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.publisher.api.services.admin; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; +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.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; + +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * APIs to handle application management related tasks. + */ +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "ApplicationDTO Management Publisher Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "ApplicationManagementPublisherAdminService"), + @ExtensionProperty(name = "context", value = "/api/application-mgt-publisher/v1.0/admin/applications"), + }) + } + ), + tags = { + @Tag(name = "application_management, device_management", description = "App publisher related Admin APIs") + } +) +@Scopes( + scopes = { + @Scope( + name = "Delete Application Release", + description = "Delete Application Release", + key = "perm:admin:app:publisher:update", + permissions = {"/app-mgt/publisher/admin/application/update"} + ) + } +) +@Path("/admin/applications") +@Api(value = "ApplicationDTO Management") +@Produces(MediaType.APPLICATION_JSON) +public interface ApplicationManagementPublisherAdminAPI { + + String SCOPE = "scope"; + + @DELETE + @Path("/{appid}/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get all applications", + notes = "This will get all applications", + tags = "ApplicationDTO Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully delete application releaset.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an application release with UUID" + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting application release.", + response = ErrorResponse.class) + }) Response deleteApplicationRelease( + @ApiParam( + name = "appId", + value = "application Id", + required = true) + @PathParam("appid") int applicationId, + @ApiParam( + name = "uuid", + value = "application release UUID", + required = true) + @PathParam("uuid") String releaseUuid); +} 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 a72767a6ed6..4b5ab8369ba 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 @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.common.*; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; @@ -376,7 +375,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationReleaseWrapper).build(); } - return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); } catch (BadRequestException e) { String msg = @@ -408,16 +406,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem //todo ---------------------- */ @DELETE - @Path("/{appid}") - public Response deleteApplication( - @PathParam("appid") int applicationId) { + @Path("/{appId}") + public Response deleteApplication(@PathParam("appId") int applicationId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - List storedLocations = applicationManager.deleteApplication(applicationId); - applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); - String responseMsg = "Successfully deleted the application and application releases: " + applicationId; - return Response.status(Response.Status.OK).entity(responseMsg).build(); + applicationManager.deleteApplication(applicationId); + return Response.status(Response.Status.OK) + .entity("Successfully deleted the application for application ID: " + applicationId).build(); } catch (NotFoundException e) { String msg = "Couldn't found application for application id: " + applicationId + " to delete the application"; @@ -431,44 +426,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem String msg = "Error occurred while deleting the application: " + applicationId; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @DELETE - @Path("/{appid}/{uuid}") - public Response deleteApplicationRelease( - @PathParam("appid") int applicationId, - @PathParam("uuid") String releaseUuid) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - try { - String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); - applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); - String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; - return Response.status(Response.Status.OK).entity(responseMsg).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found application release which is having application id: " + applicationId - + " and application release UUID:" + releaseUuid; - log.error(msg, e); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (ForbiddenException e) { - String msg = - "You don't have require permission to delete the application release which has UUID " + releaseUuid - + " and application ID " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - }catch (ApplicationManagementException e) { - String msg = "Error occurred while deleting the application: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } 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/admin/ApplicationManagementPublisherAdminAPIImpl.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/admin/ApplicationManagementPublisherAdminAPIImpl.java new file mode 100644 index 00000000000..f360f9b01b4 --- /dev/null +++ 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/admin/ApplicationManagementPublisherAdminAPIImpl.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.publisher.api.services.impl.admin; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.jaxrs.ext.multipart.Attachment; +import org.apache.cxf.jaxrs.ext.multipart.Multipart; +import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ApplicationType; +import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +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.RequestValidatingException; +import org.wso2.carbon.device.application.mgt.common.response.Application; +import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.util.APIUtil; +import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementPublisherAPI; +import org.wso2.carbon.device.application.mgt.publisher.api.services.admin.ApplicationManagementPublisherAdminAPI; + +import javax.activation.DataHandler; +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +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.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Implementation of Application Management related APIs. + */ +@Produces({"application/json"}) +@Path("/applications") +public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationManagementPublisherAdminAPI { + + private static Log log = LogFactory.getLog(ApplicationManagementPublisherAdminAPIImpl.class); + + @DELETE + @Path("/{appid}/{uuid}") + public Response deleteApplicationRelease( + @PathParam("appid") int applicationId, + @PathParam("uuid") String releaseUuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); + applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); + String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; + return Response.status(Response.Status.OK).entity(responseMsg).build(); + } catch (NotFoundException e) { + String msg = "Couldn't found application release which is having application id: " + applicationId + + " and application release UUID:" + releaseUuid; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ForbiddenException e) { + String msg = + "You don't have require permission to delete the application release which has UUID " + releaseUuid + + " and application ID " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + }catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting the application: " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred while deleting the application storage: " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + +}