From 5c23d42de163f45386c6fc62a181e29c541acc8b Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 5 Oct 2017 11:33:02 +0530 Subject: [PATCH] Adding icon support for platform --- .../device/application/mgt/api/APIUtil.java | 34 +++- .../api/services/PlatformManagementAPI.java | 30 ++-- .../impl/ApplicationManagementAPIImpl.java | 38 +++- .../impl/PlatformManagementAPIImpl.java | 52 +++++- .../impl/SubscriptionManagementAPIImpl.java | 8 +- .../application/mgt/common/Platform.java | 6 + ...ApplicationStorageManagementException.java | 2 +- .../PlatformStorageManagementException.java | 33 ++++ .../ResourceManagementException.java | 33 ++++ .../services/ApplicationStorageManager.java | 7 +- .../services/PlatformStorageManager.java | 56 ++++++ .../pom.xml | 3 +- .../mgt/core/config/Extension.java | 3 +- .../impl/ApplicationStorageManagerImpl.java | 90 ++-------- .../core/impl/PlatformStorageManagerImpl.java | 166 ++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 21 ++- .../mgt/core/internal/DataHolder.java | 16 +- .../mgt/core/internal/ServiceComponent.java | 15 +- .../core/util/ApplicationManagementUtil.java | 18 +- .../mgt/core/util/StorageManagementUtil.java | 86 +++++++++ .../main/resources/conf/application-mgt.xml | 9 +- 21 files changed, 570 insertions(+), 156 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformStorageManagementException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ResourceManagementException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformStorageManager.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformStorageManagerImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index a9d65a947b..97b9db0be3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -23,12 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; -import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; +import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import javax.ws.rs.core.Response; @@ -47,6 +42,7 @@ public class APIUtil { private static ApplicationReleaseManager applicationReleaseManager; private static ApplicationStorageManager applicationStorageManager; private static SubscriptionManager subscriptionManager; + private static PlatformStorageManager platformStorageManager; public static ApplicationManager getApplicationManager() { if (applicationManager == null) { @@ -147,7 +143,31 @@ public class APIUtil { } return applicationStorageManager; } - public static Response getResponse(ApplicationManagementException ex, Response.Status status) { + + /** + * To get the Platform Storage Manager from the osgi context. + * + * @return PlatformStoreManager instance in the current osgi context. + */ + public static PlatformStorageManager getPlatformStorageManager() { + if (platformStorageManager == null) { + synchronized (APIUtil.class) { + if (platformStorageManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + platformStorageManager = (PlatformStorageManager) ctx + .getOSGiService(PlatformStorageManager.class, null); + if (platformStorageManager == null) { + String msg = "Platform Storage Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return platformStorageManager; + } + + public static Response getResponse(Exception ex, Response.Status status) { return getResponse(ex.getMessage(), status); } 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 8dd3da5e4a..f60e7199f1 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 @@ -28,8 +28,11 @@ 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.Scopes; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.Platform; import javax.validation.constraints.Size; @@ -96,7 +99,6 @@ import javax.ws.rs.core.Response; "such as get all the available platform for a tenant, etc.") @Path("/platforms") @Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public interface PlatformManagementAPI { String SCOPE = "scope"; @@ -181,9 +183,9 @@ public interface PlatformManagementAPI { @POST @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) + @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( - consumes = MediaType.APPLICATION_JSON, + consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, httpMethod = "POST", value = "Add Platform", @@ -209,19 +211,16 @@ public interface PlatformManagementAPI { response = ErrorResponse.class) }) Response addPlatform( - @ApiParam( - name = "platform", - value = "The payload of the platform", - required = true) - Platform platform + @Multipart(value = "Platform", type = "application/json" ) Platform platform, + @Multipart(value = "icon", required = true) Attachment iconFile ); @PUT @Path("/{identifier}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) + @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( - consumes = MediaType.APPLICATION_JSON, + consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", value = "Update Platform", @@ -247,11 +246,8 @@ public interface PlatformManagementAPI { response = ErrorResponse.class) }) Response updatePlatform( - @ApiParam( - name = "platform", - value = "The payload of the platform", - required = true) - Platform platform, + @Multipart(value = "Platform", type = "application/json" ) Platform platform, + @Multipart(value = "icon", required = false) Attachment iconFile, @ApiParam( name = "identifier", required = true) @@ -343,7 +339,7 @@ public interface PlatformManagementAPI { ); @GET - @Path("tags") + @Path("tags/{name}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -374,6 +370,6 @@ public interface PlatformManagementAPI { Response getPlatformTags( @ApiParam(name = "name", value ="The initial part of the name of platform tags that we need to retrieve", required = true) - @QueryParam("name") @Size(min = 3) String name + @PathParam("name") @Size(min = 3) String name ); } 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/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index 03633f65b6..2043014fc0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.ImageArtifact; 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.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; @@ -97,6 +98,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { String msg = "Error occurred while getting the application list"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while getting the image artifacts of the application", e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -132,6 +136,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } catch (ApplicationManagementException e) { log.error("Error occurred while getting application with the uuid " + uuid, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while getting the image artifacts of the application with the uuid " + uuid, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -252,6 +259,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(new ApplicationManagementException( "Exception while trying to read icon, " + "banner files for the application " + applicationUUID, 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); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -259,8 +270,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @PUT @Path("/upload-image-artifacts/{uuid}") public Response updateApplicationArtifacts(@PathParam("uuid") String applicationUUID, - @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, @Multipart - ("screenshot") List attachmentList) { + @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, + @Multipart("screenshot") List attachmentList) { ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { InputStream iconFileStream = null; @@ -282,15 +293,15 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { .uploadImageArtifacts(applicationUUID, iconFileStream, bannerFileStream, attachments); return Response.status(Response.Status.OK) .entity("Successfully updated artifacts for the application " + applicationUUID).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred while updating the artifact for the application " + applicationUUID; - log.error(msg, e); - return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); } catch (IOException e) { log.error("Exception while trying to read icon, banner files for the application " + applicationUUID); return APIUtil.getResponse(new ApplicationManagementException( "Exception while trying to read icon, banner files for the application " + applicationUUID, 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); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -328,6 +339,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { String msg = "Error occurred while deleting the application: " + uuid; log.error(msg, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while deleteing the image artifacts of the application with the uuid " + uuid, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -358,6 +372,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { log.error(errorMessage, e); return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e), Response.Status.INTERNAL_SERVER_ERROR); + } catch (ResourceManagementException e) { + log.error("Error occurred while uploading the releases artifacts of the application with the uuid " + + applicationUUID + " for the release " + applicationRelease.getVersionName(), e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -394,6 +412,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(new ApplicationManagementException( "Error while updating the release artifacts of the application with UUID " + applicationUUID), Response.Status.INTERNAL_SERVER_ERROR); + } catch (ResourceManagementException e) { + log.error("Error occurred while updating the releases artifacts of the application with the uuid " + + applicationUUID + " for the release " + applicationRelease.getVersionName(), e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -470,6 +492,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } catch (ApplicationManagementException e) { log.error("Error while deleting application release with the application UUID " + applicationUUID, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while deleting the releases artifacts of the application with the uuid " + + applicationUUID + " for the release " + version, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } 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 7868733c33..49448ab824 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 @@ -19,13 +19,20 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; 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.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.api.APIUtil; import org.wso2.carbon.device.application.mgt.api.services.PlatformManagementAPI; +import org.wso2.carbon.device.application.mgt.common.ImageArtifact; import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.validation.constraints.Size; @@ -118,6 +125,8 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { if (platform == null) { return Response.status(Response.Status.NOT_FOUND).entity("Platform not found").build(); } + ImageArtifact icon = APIUtil.getPlatformStorageManager().getIcon(id); + return Response.status(Response.Status.OK).entity(platform).build(); } catch (PlatformManagementDAOException e) { log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :" @@ -127,17 +136,26 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :" + tenantId, e); return APIUtil.getResponse(e, Response.Status.NOT_FOUND); + } catch (PlatformStorageManagementException e) { + log.error("Platform Storage Management Exception while trying to update the icon for the platform : " + + id + " for the tenant : " + tenantId, e); + return APIUtil.getResponse(e, Response.Status.NOT_FOUND); } } @POST @Override - public Response addPlatform(Platform platform) { + public Response addPlatform(@Multipart("platform") Platform platform, @Multipart("icon")Attachment icon) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { if (platform != null) { if (platform.validate()) { APIUtil.getPlatformManager().register(tenantId, platform); + + if (icon != null) { + InputStream iconFileStream = icon.getDataHandler().getInputStream(); + APIUtil.getPlatformStorageManager().uploadIcon(platform.getIdentifier(), iconFileStream); + } return Response.status(Response.Status.CREATED).build(); } else { return APIUtil @@ -152,20 +170,41 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { log.error("Platform Management Exception while trying to add the platform with identifier : " + platform .getIdentifier() + " for the tenant : " + tenantId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + log.error("IO Exception while trying to save 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 save platform icon for the platform : " + platform + .getIdentifier(), e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @PUT @Path("/{identifier}") @Override - public Response updatePlatform(Platform platform, @PathParam("identifier") @Size(max = 45) String id) { + public Response updatePlatform(@Multipart("platform") Platform platform, @Multipart("icon") Attachment + icon, @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); } } @@ -175,12 +214,17 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { public Response removePlatform(@PathParam("identifier") @Size(max = 45) String id) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { + APIUtil.getPlatformStorageManager().deleteIcon(id); APIUtil.getPlatformManager().unregister(tenantId, id, false); return Response.status(Response.Status.OK).build(); } catch (PlatformManagementException e) { log.error("Platform Management Exception while trying to un-register the platform with the identifier : " + id + " for the tenant : " + tenantId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (PlatformStorageManagementException e) { + log.error("Platform Storage Management Exception while trying to delete the icon of the platform with " + + "identifier for the tenant :" + tenantId, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -205,9 +249,9 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { } @GET - @Path("tags") + @Path("tags/{name}") @Override - public Response getPlatformTags(@QueryParam("name") String name) { + public Response getPlatformTags(@PathParam("name") String name) { if (name == null || name.isEmpty() || name.length() < 3) { return APIUtil.getResponse("In order to get platform tags, it is required to pass the first 3 " + "characters of the platform tag name", Response.Status.INTERNAL_SERVER_ERROR); 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/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java index 20effa969a..cf0de355b5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java @@ -52,17 +52,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ Object result; SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); try { - String applicationUUID = installationDetails.getApplicationUUID(); + String applicationUUTD = installationDetails.getApplicationUUID(); String versionName = installationDetails.getVersionName(); if (!installationDetails.getDeviceIdentifiers().isEmpty()) { List deviceList = installationDetails.getDeviceIdentifiers(); - result = subscriptionManager.installApplicationForDevices(applicationUUID, versionName, deviceList); + result = subscriptionManager.installApplicationForDevices(applicationUUTD, versionName, deviceList); } else if (!installationDetails.getUserNameList().isEmpty()) { List userList = installationDetails.getUserNameList(); - result = subscriptionManager.installApplicationForUsers(applicationUUID, userList); + result = subscriptionManager.installApplicationForUsers(applicationUUTD, userList); } else if (!installationDetails.getRoleNameList().isEmpty()) { List roleList = installationDetails.getRoleNameList(); - result = subscriptionManager.installApplicationForRoles(applicationUUID, roleList); + result = subscriptionManager.installApplicationForRoles(applicationUUTD, roleList); } else { result = "Missing request data!"; return Response.status(Response.Status.BAD_REQUEST).entity(result).build(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java index 3ac3ef3ead..cf85852df0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java @@ -60,6 +60,8 @@ public class Platform { private boolean defaultTenantMapping; + private ImageArtifact icon; + public Platform(Platform platform) { this.id = platform.getId(); this.name = platform.getName(); @@ -180,6 +182,10 @@ public class Platform { return !(name == null || identifier == null); } + public void setIcon(ImageArtifact icon) { + this.icon = icon; + } + /** * Represents a property of a {@link Platform}. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationStorageManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationStorageManagementException.java index cba6915bcb..0bf95ab7c2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationStorageManagementException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationStorageManagementException.java @@ -22,7 +22,7 @@ package org.wso2.carbon.device.application.mgt.common.exception; /** * Represents the exception thrown during storing and retrieving the artifacts. */ -public class ApplicationStorageManagementException extends ApplicationManagementException { +public class ApplicationStorageManagementException extends ResourceManagementException { public ApplicationStorageManagementException(String message, Throwable ex) { super(message, ex); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformStorageManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformStorageManagementException.java new file mode 100644 index 0000000000..c98ab3ad0e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformStorageManagementException.java @@ -0,0 +1,33 @@ +/* + * 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.common.exception; + +/** + * Represents the exception thrown during storing and retrieving those artifacts. + */ +public class PlatformStorageManagementException extends ResourceManagementException { + public PlatformStorageManagementException(String message, Throwable ex) { + super(message, ex); + } + + public PlatformStorageManagementException(String message) { + super(message); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ResourceManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ResourceManagementException.java new file mode 100644 index 0000000000..11ecb5a2cd --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ResourceManagementException.java @@ -0,0 +1,33 @@ +/* + * 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.common.exception; + +/** + * Represents the exception that will be thrown when there is an issue while managing the resources. + */ +public class ResourceManagementException extends Exception { + ResourceManagementException(String message, Throwable ex) { + super(message, ex); + } + + public ResourceManagementException(String message) { + super(message); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 4f2b675dd6..e1597f8786 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ImageArtifact; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import java.io.InputStream; import java.util.List; @@ -38,7 +39,7 @@ public interface ApplicationStorageManager { * @throws ApplicationStorageManagementException Application Storage Management Exception. */ public void uploadImageArtifacts(String applicationUUID, InputStream iconFile, InputStream bannerFile, - List screenshots) throws ApplicationStorageManagementException; + List screenshots) throws ResourceManagementException; /** * To upload release artifacts for an Application. @@ -47,8 +48,8 @@ public interface ApplicationStorageManager { * @param binaryFile Binary File for the release. * @throws ApplicationStorageManagementException Application Storage Management Exception. */ - public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) throws - ApplicationStorageManagementException; + public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) + throws ResourceManagementException; /** * To get released artifacts for the particular version of the application. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformStorageManager.java new file mode 100644 index 0000000000..5b3f373221 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformStorageManager.java @@ -0,0 +1,56 @@ +/* + * 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.common.services; + +import org.wso2.carbon.device.application.mgt.common.ImageArtifact; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; + +import java.io.InputStream; + +/** + * This class manages all the storage related requirements of Platform. + */ +public interface PlatformStorageManager { + /** + * To upload image artifacts related with an Application. + * + * @param platformIdentifier Identifier of the platform + * @param iconFile Icon File input stream + * @throws PlatformStorageManagementException Platform Storage Management Exception. + */ + public void uploadIcon(String platformIdentifier, InputStream iconFile) throws ResourceManagementException; + + /** + * To get the icon for a particular platform. + * + * @param platformIdentifier Identifier of the platform. + * @return the icon for the given platform. + */ + public ImageArtifact getIcon(String platformIdentifier) throws PlatformStorageManagementException; + + /** + * To delete the icon of a particular platform + * + * @param platformIdentifier Identifier of the platform to which delete icon. + * @throws PlatformStorageManagementException PlatformStorageManagement Exception. + */ + public void deleteIcon(String platformIdentifier) throws PlatformStorageManagementException; +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml index 04b388cedd..b1a17af986 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml @@ -77,7 +77,8 @@ org.wso2.carbon.ndatasource.core, org.wso2.carbon, org.apache.commons.io, - org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}" + org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}", + org.wso2.carbon.base !org.wso2.carbon.device.application.mgt.core.internal.*, diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Extension.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Extension.java index 9ce5c282f7..07e3b09a85 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Extension.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Extension.java @@ -87,7 +87,8 @@ public class Extension { VisibilityTypeManager, SubscriptionManager, VisibilityManager, - ApplicationStorageManager + ApplicationStorageManager, + PlatformStorageManager } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index cbf3e935ce..5ee902e0d6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -19,8 +19,6 @@ package org.wso2.carbon.device.application.mgt.core.impl; -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -30,6 +28,7 @@ import org.wso2.carbon.device.application.mgt.common.ImageArtifact; 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.DBConnectionException; +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.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; @@ -37,18 +36,18 @@ import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManageme import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; +import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; import java.util.Arrays; import java.util.List; +import static org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil.saveFile; + /** * This class contains the default concrete implementation of ApplicationStorage Management. */ @@ -70,7 +69,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void uploadImageArtifacts(String applicationUUID, InputStream iconFileStream, InputStream bannerFileStream, - List screenShotStreams) throws ApplicationStorageManagementException { + List screenShotStreams) throws ResourceManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application application = validateApplication(applicationUUID); String artifactDirectoryPath = storagePath + application.getId(); @@ -78,7 +77,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager log.debug("Artifact Directory Path for saving the artifacts related with application " + applicationUUID + " is " + artifactDirectoryPath); } - createArtifactDirectory(artifactDirectoryPath); + StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); if (iconFileStream != null) { try { saveFile(iconFileStream, artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0]); @@ -155,15 +154,14 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) - throws ApplicationStorageManagementException { + throws ResourceManagementException { Application application = validateApplication(applicationUUID); String artifactDirectoryPath = storagePath + application.getId(); - if (log.isDebugEnabled()) { + if (log.isDebugEnabled()) log.debug("Artifact Directory Path for saving the application release related artifacts related with " + "application " + applicationUUID + " is " + artifactDirectoryPath); - } - createArtifactDirectory(artifactDirectoryPath); + StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); if (binaryFile != null) { try { saveFile(binaryFile, artifactDirectoryPath + File.separator + versionName); @@ -207,7 +205,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager File artifactDirectory = new File(artifactDirectoryPath); if (artifactDirectory.exists()) { - deleteDir(artifactDirectory); + StorageManagementUtil.deleteDir(artifactDirectory); } } @@ -219,14 +217,14 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager File artifact = new File(artifactPath); if (artifact.exists()) { - deleteDir(artifact); + StorageManagementUtil.deleteDir(artifact); } } @Override public void deleteAllApplicationReleaseArtifacts(String applicationUUID) throws ApplicationStorageManagementException { - Application application = validateApplication(applicationUUID); + validateApplication(applicationUUID); try { List applicationReleases = DataHolder.getInstance().getReleaseManager() .getReleases(applicationUUID); @@ -256,12 +254,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager "Image artifact " + name + " does not exist for the " + "application with UUID " + applicationUUID); } else { try { - ImageArtifact imageArtifact = new ImageArtifact(); - imageArtifact.setName(imageFile.getName()); - imageArtifact.setType(Files.probeContentType(imageFile.toPath())); - byte[] imageBytes = IOUtils.toByteArray(new FileInputStream(imageArtifactPath)); - imageArtifact.setEncodedImage(Base64.encodeBase64URLSafeString(imageBytes)); - return imageArtifact; + return StorageManagementUtil.createImageArtifact(imageFile, imageArtifactPath); } catch (FileNotFoundException e) { throw new ApplicationStorageManagementException( "File not found exception while trying to get the image artifact " + name + " for the " @@ -273,61 +266,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } - /** - * To save a file in a given location. - * - * @param inputStream Stream of the file. - * @param path Path the file need to be saved in. - */ - private void saveFile(InputStream inputStream, String path) throws IOException { - OutputStream outStream = null; - try { - byte[] buffer = new byte[inputStream.available()]; - inputStream.read(buffer); - outStream = new FileOutputStream(new File(path)); - outStream.write(buffer); - } finally { - if (inputStream != null) { - inputStream.close(); - } - if (outStream != null) { - outStream.close(); - } - } - } - - /** - * This method is responsible for creating artifact parent directories in the given path. - * - * @param artifactDirectoryPath Path for the artifact directory. - * @throws ApplicationStorageManagementException Application Storage Management Exception. - */ - private void createArtifactDirectory(String artifactDirectoryPath) throws ApplicationStorageManagementException { - File artifactDirectory = new File(artifactDirectoryPath); - - if (!artifactDirectory.exists()) { - if (!artifactDirectory.mkdirs()) { - throw new ApplicationStorageManagementException( - "Cannot create directories in the path to save the application related artifacts"); - } - } - } - - /** - * To delete a directory recursively - * - * @param artifactDirectory Artifact Directory that need to be deleted. - */ - private void deleteDir(File artifactDirectory) { - File[] contents = artifactDirectory.listFiles(); - if (contents != null) { - for (File file : contents) { - deleteDir(file); - } - } - artifactDirectory.delete(); - } - /** * To validate the image artifact names. * @param name Name of the image artifact. @@ -354,7 +292,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager * could not be found. */ private Application validateApplication(String uuid) throws ApplicationStorageManagementException { - Application application = null; + Application application; try { application = DataHolder.getInstance().getApplicationManager().getApplication(uuid); } catch (ApplicationManagementException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformStorageManagerImpl.java new file mode 100644 index 0000000000..d748d79afc --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformStorageManagerImpl.java @@ -0,0 +1,166 @@ +/* + * 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.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.ImageArtifact; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; +import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; +import org.wso2.carbon.device.application.mgt.common.services.PlatformStorageManager; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.util.Constants; +import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; +import org.wso2.carbon.utils.multitenancy.MultitenantUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import static org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil.saveFile; + +/** + * This is the concrete implementation of {@link PlatformStorageManager} + */ +public class PlatformStorageManagerImpl implements PlatformStorageManager { + private static final Log log = LogFactory.getLog(ApplicationStorageManagerImpl.class); + private String storagePath; + + /** + * This creates a new instance of PlatformStorageManager. + * @param storagePath Storage path to store the artifacts related with platform. + */ + public PlatformStorageManagerImpl(String storagePath) { + this.storagePath = storagePath; + } + + @Override + public void uploadIcon(String platformIdentifier, InputStream iconFileStream) throws ResourceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Platform platform = validatePlatform(tenantId, platformIdentifier); + + if (platform.isFileBased()) { + throw new ApplicationStorageManagementException("Icons for the file based platforms need to be added " + + "directly to the deployment location inside icon folder"); + } + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformStorageManagementException("Platform " + platformIdentifier + " is a share platform from " + + "super-tenant. Only the super-tenant users can modify it"); + } + if (log.isDebugEnabled()) { + log.debug("Artifact Directory Path for saving the artifacts related with application " + + platformIdentifier + " is " + storagePath); + } + StorageManagementUtil.createArtifactDirectory(storagePath); + if (iconFileStream != null) { + try { + saveFile(iconFileStream, storagePath + File.separator + platform.getId()); + } catch (IOException e) { + throw new ApplicationStorageManagementException( + "IO Exception while saving the icon file in the server for the platform " + + platformIdentifier, e); + } + } + } + + @Override + public ImageArtifact getIcon(String platformIdentifier) throws PlatformStorageManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Platform platform = validatePlatform(tenantId, platformIdentifier); + String imageArtifactPath = storagePath + platform.getId(); + File imageFile = null; + + if (platform.isFileBased()) { + imageFile = new File(MultitenantUtils.getAxis2RepositoryPath(CarbonContext.getThreadLocalCarbonContext(). + getTenantId()) + Constants.PLATFORMS_DEPLOYMENT_DIR_NAME + File.separator + + Constants.IMAGE_ARTIFACTS[0] + File.separator + platformIdentifier); + } else { + imageFile = new File(imageArtifactPath); + } + + if (!imageFile.exists()) { + return null; + } else { + try { + return StorageManagementUtil.createImageArtifact(imageFile, imageArtifactPath); + } catch (FileNotFoundException e) { + throw new PlatformStorageManagementException( + "File not found exception while trying to get the icon for the " + "platform " + + platformIdentifier, e); + } catch (IOException e) { + throw new PlatformStorageManagementException( + "IO Exception while trying to detect the file type of the platform icon of " + + platformIdentifier, e); + } + } + } + + @Override + public void deleteIcon(String platformIdentifier) throws PlatformStorageManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Platform platform = validatePlatform(tenantId, platformIdentifier); + String imageArtifactPath = storagePath + platform.getId(); + + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformStorageManagementException("Platform " + platformIdentifier + " is a share platform from " + + "super-tenant. Only the super-tenant users can modify it"); + } + if (platform.isFileBased()) { + throw new PlatformStorageManagementException("Platform " + platformIdentifier + " is a file based one. " + + "Please remove the relevant icon file directly from file system."); + } + + File imageFile = new File(imageArtifactPath); + if (imageFile.exists()) { + imageFile.delete(); + } + } + + /** + * To validate the platform, whether the given identifier has a valid platform. + * + * @param tenantId ID of the tenant + * @param identifier Identifier of the platform + * @return Platform related with the particular identifier. + */ + private Platform validatePlatform(int tenantId, String identifier) throws PlatformStorageManagementException { + Platform platform; + try { + PlatformManager platformManager = DataHolder.getInstance().getPlatformManager(); + platform = platformManager.getPlatform(tenantId, identifier); + } catch (PlatformManagementException e) { + throw new PlatformStorageManagementException("Platform Management Exception while getting the platform " + + "related with the identifier " + identifier); + } + + if (platform == null) { + throw new PlatformStorageManagementException("Platform does not exist with the identifier " + identifier); + } + return platform; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 3667e0953d..6fc19346fd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; @@ -40,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * This is the default implementation for the Subscription Manager. @@ -61,9 +63,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { org.wso2.carbon.device.mgt.common.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.device.mgt .common.DeviceIdentifier(device.getId(), device.getType()); try { - DeviceManagementProviderService dmpService = DataHolder.getInstance().getDeviceManagementService(); - if (!dmpService.isEnrolled(deviceIdentifier)) { - log.error("Device with ID: " + device.getId() + " is not enrolled to install the application."); + DeviceManagementDAOFactory.openConnection(); + // todo: replace this with boolean:deviceExsits(deviceId) operation + Map currentDevices = DeviceManagementDAOFactory.getDeviceDAO().getDevice(deviceIdentifier); + DeviceManagementDAOFactory.closeConnection(); + + if (currentDevices.isEmpty()) { + log.error("Device with ID: " + device.getId() + " not found to install the application."); } else { if (log.isDebugEnabled()) { log.debug("Installing application to : " + device.getId()); @@ -90,8 +96,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { // DAOFactory.getSubscriptionDAO().addDeviceApplicationMapping(device.getId(), applicationUUID, false); failedDeviceList.remove(device); } - } catch (DeviceManagementException | OperationManagementException | InvalidDeviceException e) { + } catch (DeviceManagementException | DeviceManagementDAOException | OperationManagementException | InvalidDeviceException | SQLException e) { throw new ApplicationManagementException("Failed to install application " + applicationUUID + " on device " + deviceIdentifier, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } } return failedDeviceList; @@ -102,14 +110,15 @@ public class SubscriptionManagerImpl implements SubscriptionManager { throws ApplicationManagementException { log.info("Install application: " + applicationUUID + " to: " + userList.size() + " users."); List deviceList = new ArrayList<>(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); for (String user : userList) { try { - List devicesOfUser = DataHolder.getInstance().getDeviceManagementService().getDevicesOfUser(user); + List devicesOfUser = DeviceManagementDAOFactory.getDeviceDAO().getDevicesOfUser(user, tenantId); for (Device device : devicesOfUser) { deviceList.add(new DeviceIdentifier(device .getDeviceIdentifier(), device.getType())); } - } catch (DeviceManagementException e) { + } catch (DeviceManagementDAOException e) { log.error("Error when extracting the device list from user[" + user + "].", e); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java index a153b0bc14..31de14afa3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java @@ -18,15 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.internal; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; -import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; -import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; +import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.user.core.service.RealmService; @@ -57,6 +49,8 @@ public class DataHolder { private ApplicationStorageManager applicationStorageManager; + private PlatformStorageManager platformStorageManager; + private static final DataHolder applicationMgtDataHolder = new DataHolder(); private DataHolder() { @@ -154,4 +148,8 @@ public class DataHolder { public ApplicationStorageManager getApplicationStorageManager() { return applicationStorageManager; } + + public void setPlatformStorageManager(PlatformStorageManager platformStorageManager) { + this.platformStorageManager = platformStorageManager; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java index bc0919d8e6..bb12107284 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java @@ -23,15 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; -import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; -import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; +import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; @@ -115,6 +107,11 @@ public class ServiceComponent { DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager); bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null); + PlatformStorageManager platformStorageManager = ApplicationManagementUtil + .getPlatformStorageManagerInstance(); + DataHolder.getInstance().setPlatformStorageManager(platformStorageManager); + bundleContext.registerService(PlatformStorageManager.class.getName(), platformStorageManager, null); + bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(), new PlatformManagementAxis2ConfigurationObserverImpl(), null); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java index 8f8868a000..acc83f34ae 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java @@ -21,15 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; -import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; -import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; -import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; -import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; +import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.config.Extension; @@ -99,6 +91,14 @@ public class ApplicationManagementUtil { return getInstance(extension, ApplicationStorageManager.class); } + public static PlatformStorageManager getPlatformStorageManagerInstance() throws + InvalidConfigurationException { + ConfigurationManager configurationManager = ConfigurationManager.getInstance(); + Extension extension = configurationManager.getExtension(Extension.Name.PlatformStorageManager); + return getInstance(extension, PlatformStorageManager.class); + } + + private static T getInstance(Extension extension, Class cls) throws InvalidConfigurationException { try { Class theClass = Class.forName(extension.getClassName()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java new file mode 100644 index 0000000000..d644fb52cd --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java @@ -0,0 +1,86 @@ +package org.wso2.carbon.device.application.mgt.core.util; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.IOUtils; +import org.wso2.carbon.device.application.mgt.common.ImageArtifact; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; + +import java.io.*; +import java.nio.file.Files; + +public class StorageManagementUtil { + /** + * This method is responsible for creating artifact parent directories in the given path. + * + * @param artifactDirectoryPath Path for the artifact directory. + * @throws ApplicationStorageManagementException Application Storage Management Exception. + */ + public static void createArtifactDirectory(String artifactDirectoryPath) throws ResourceManagementException { + File artifactDirectory = new File(artifactDirectoryPath); + + if (!artifactDirectory.exists()) { + if (!artifactDirectory.mkdirs()) { + throw new ResourceManagementException( + "Cannot create directories in the path to save the application related artifacts"); + } + } + } + + /** + * To delete a directory recursively + * + * @param artifactDirectory Artifact Directory that need to be deleted. + */ + public static void deleteDir(File artifactDirectory) { + File[] contents = artifactDirectory.listFiles(); + if (contents != null) { + for (File file : contents) { + deleteDir(file); + } + } + artifactDirectory.delete(); + } + + + /** + * To save a file in a given location. + * + * @param inputStream Stream of the file. + * @param path Path the file need to be saved in. + */ + public static void saveFile(InputStream inputStream, String path) throws IOException { + OutputStream outStream = null; + try { + byte[] buffer = new byte[inputStream.available()]; + inputStream.read(buffer); + outStream = new FileOutputStream(new File(path)); + outStream.write(buffer); + } finally { + if (inputStream != null) { + inputStream.close(); + } + if (outStream != null) { + outStream.close(); + } + } + } + + /** + * To create {@link ImageArtifact}. + * + * @param imageFile Image File. + * @param imageArtifactPath Path of the image artifact file. + * @return Image Artifact. + * @throws IOException IO Exception. + */ + public static ImageArtifact createImageArtifact(File imageFile, String imageArtifactPath) throws IOException { + ImageArtifact imageArtifact = new ImageArtifact(); + imageArtifact.setName(imageFile.getName()); + imageArtifact.setType(Files.probeContentType(imageFile.toPath())); + byte[] imageBytes = IOUtils.toByteArray(new FileInputStream(imageArtifactPath)); + imageArtifact.setEncodedImage(Base64.encodeBase64URLSafeString(imageBytes)); + return imageArtifact; + } + +} diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index dd88d20bf5..bd74672de3 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -46,9 +46,6 @@ org.wso2.carbon.device.application.mgt.core.impl.VisibilityManagerImpl - - org.wso2.carbon.device.application.mgt.core.impl.VisibilityTypeManagerImpl - org.wso2.carbon.device.application.mgt.core.impl.ApplicationStorageManagerImpl @@ -56,5 +53,11 @@ 6 + + org.wso2.carbon.device.application.mgt.core.impl.PlatformStorageManagerImpl + + repository/resources/platforms + + \ No newline at end of file