From f417f7854e3dc375803ea48ff598ad4002a8fd42 Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 5 Oct 2017 13:28:00 +0530 Subject: [PATCH] Adding icon update --- .../api/services/PlatformManagementAPI.java | 53 +++++++++++++++++-- .../impl/PlatformManagementAPIImpl.java | 39 +++++++++----- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java index f60e7199f19..ace466cdd83 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java @@ -212,15 +212,15 @@ public interface PlatformManagementAPI { }) Response addPlatform( @Multipart(value = "Platform", type = "application/json" ) Platform platform, - @Multipart(value = "icon", required = true) Attachment iconFile + @Multipart(value = "icon", required = false) Attachment iconFile ); @PUT @Path("/{identifier}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( - consumes = MediaType.MULTIPART_FORM_DATA, + consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", value = "Update Platform", @@ -246,8 +246,11 @@ public interface PlatformManagementAPI { response = ErrorResponse.class) }) Response updatePlatform( - @Multipart(value = "Platform", type = "application/json" ) Platform platform, - @Multipart(value = "icon", required = false) Attachment iconFile, + @ApiParam( + name = "platform", + value = "The payload of the platform", + required = true) + Platform platform, @ApiParam( name = "identifier", required = true) @@ -372,4 +375,44 @@ public interface PlatformManagementAPI { required = true) @PathParam("name") @Size(min = 3) String name ); + + @POST + @Path("/{identifier}/icon") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @ApiOperation( + consumes = MediaType.MULTIPART_FORM_DATA, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Update Platform icon", + notes = "This will update the platform icon", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated the platform icon"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request parameters passed."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while updating the platform icon.", + response = ErrorResponse.class) + }) + Response updatePlatformIcon( + @ApiParam( + name = "identifier", + required = true) + @PathParam("identifier") + @Size(max = 45) + String identifier, + @Multipart(value = "icon") Attachment iconFile + ); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java index 49448ab824f..a62a52e52ff 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java @@ -184,27 +184,14 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @PUT @Path("/{identifier}") @Override - public Response updatePlatform(@Multipart("platform") Platform platform, @Multipart("icon") Attachment - icon, @PathParam("identifier") @Size(max = 45) String id) { + public Response updatePlatform(Platform platform, @PathParam("identifier") @Size(max = 45) String id) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { APIUtil.getPlatformManager().update(tenantId, id, platform); - if (icon != null) { - InputStream iconFileStream = icon.getDataHandler().getInputStream(); - APIUtil.getPlatformStorageManager().uploadIcon(platform.getIdentifier(), iconFileStream); - } return Response.status(Response.Status.OK).build(); } catch (PlatformManagementException e) { log.error("Error while updating the platform - " + id + " for tenant domain - " + tenantId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); - } catch (IOException e) { - log.error("IO Exception while trying to update the platform icon for the platform : " + platform - .getIdentifier(), e); - return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); - } catch (ResourceManagementException e) { - log.error("Storage Exception while trying to update the platform icon for the platform : " + platform - .getIdentifier(), e); - return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -265,4 +252,28 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @POST + @Path("{identifier}/icon") + @Override + public Response updatePlatformIcon(@PathParam("identifier") String identifier, @Multipart("icon") Attachment + icon) { + try { + if (icon != null) { + InputStream iconFileStream = icon.getDataHandler().getInputStream(); + APIUtil.getPlatformStorageManager().uploadIcon(identifier, iconFileStream); + return Response.status(Response.Status.OK) + .entity("Icon file is successfully updated for the platform :" + identifier).build(); + } else { + return Response.status(Response.Status.BAD_REQUEST).entity("Icon file is not provided to update") + .build(); + } + } catch (ResourceManagementException e) { + log.error("Resource Management exception while trying to update the icon for the platform " + identifier); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + log.error("IO exception while trying to update the icon for the platform " + identifier); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } }