From 48023c5d8b939dd393b72c47c4129e71291e6f10 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 19 Sep 2017 14:29:04 +0530 Subject: [PATCH] Changing the image return to be a base6 format --- .../services/ApplicationManagementAPI.java | 2 +- .../impl/ApplicationManagementAPIImpl.java | 9 ++- .../application/mgt/common/ImageArtifact.java | 55 +++++++++++++++++++ .../services/ApplicationStorageManager.java | 3 +- .../pom.xml | 4 +- .../impl/ApplicationStorageManagerImpl.java | 16 +++++- .../pom.xml | 2 +- .../publisher/src/{App.scss => App.css} | 0 8 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ImageArtifact.java rename components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/{App.scss => App.css} (100%) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java index 9c524570ba..722c79974a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java @@ -651,7 +651,7 @@ public interface ApplicationManagementAPI { @GET @Path("/image-artifacts/{uuid}") - @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, 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 9cd5d0e956..c73e2a8487 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 @@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; 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.services.ApplicationManager; @@ -452,7 +453,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Override @GET @Path("/image-artifacts/{uuid}") - @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Produces(MediaType.APPLICATION_JSON) public Response getApplicationImageArtifacts(@PathParam("uuid") String applicationUUID, @QueryParam("name") String name, @QueryParam("count") int count) { if (name == null || name.isEmpty()) { @@ -461,10 +462,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - InputStream imageArtifact = applicationStorageManager.getImageArtifact(applicationUUID, name, count); - FileStreamingOutput fileStreamingOutput = new FileStreamingOutput(imageArtifact); - Response.ResponseBuilder response = Response.status(Response.Status.OK).entity(fileStreamingOutput); - response.header("Content-Disposition", "attachment; filename=\"" + name + "\""); + ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(applicationUUID, name, count); + Response.ResponseBuilder response = Response.status(Response.Status.OK).entity(imageArtifact); return response.build(); } catch (ApplicationStorageManagementException e) { log.error("Application Storage Management Exception while getting the image artifact " + name + " of " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ImageArtifact.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ImageArtifact.java new file mode 100644 index 0000000000..85626fab04 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ImageArtifact.java @@ -0,0 +1,55 @@ +/* + * 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; + +/** + * This represents a image artifact of a application. Icon, Screen-shot or Banner. + * It consists of a name, type and base64 encoded string format of the image. + */ +public class ImageArtifact { + private String name; + private String type; + private String encodedImage; + + public String getType() { + return type; + } + + public String getEncodedImage() { + return encodedImage; + } + + public String getName() { + + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void setType(String type) { + this.type = type; + } + + public void setEncodedImage(String encodedImage) { + this.encodedImage = encodedImage; + } +} 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 860812244a..4f2b675dd6 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 @@ -19,6 +19,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 java.io.InputStream; @@ -93,6 +94,6 @@ public interface ApplicationStorageManager { * @return the relevant image artifact. * @throws ApplicationStorageManagementException Application Storage Management Exception. */ - public InputStream getImageArtifact(String applicationUUID, String name, int count) throws + public ImageArtifact getImageArtifact(String applicationUUID, String name, int count) throws ApplicationStorageManagementException; } 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 495bc9c973..9bcc817bef 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 @@ -75,7 +75,9 @@ org.wso2.carbon.user.core.*, org.wso2.carbon.user.api.*, org.wso2.carbon.ndatasource.core, - org.wso2.carbon + org.wso2.carbon, + org.apache.commons.io, + org.apache.commons.codec.binary;version="[1.4.0, 2.0.0)" !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/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 287cd5370b..97d0af2352 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,11 +19,14 @@ 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; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +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; @@ -42,6 +45,7 @@ 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; @@ -207,7 +211,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public InputStream getImageArtifact(String applicationUUID, String name, int count) throws + public ImageArtifact getImageArtifact(String applicationUUID, String name, int count) throws ApplicationStorageManagementException { Application application = validateApplication(applicationUUID); validateImageArtifactNames(name); @@ -222,11 +226,19 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager "Image artifact " + name + " does not exist for the " + "application with UUID " + applicationUUID); } else { try { - return new FileInputStream(imageArtifactPath); + 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; } catch (FileNotFoundException e) { throw new ApplicationStorageManagementException( "File not found exception while trying to get the image artifact " + name + " for the " + "application " + applicationUUID, e); + } catch (IOException e) { + throw new ApplicationStorageManagementException("IO Exception while trying to detect the image " + + "artifact " + name + " for the application " + applicationUUID, e); } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml index 962be6a3df..00548da987 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/pom.xml @@ -28,7 +28,7 @@ 4.0.0 org.wso2.carbon.device.application.mgt.publisher.ui 3.0.46-SNAPSHOT - war + pom WSO2 Carbon - Application Management Publisher UI WSO2 Carbon - Application Management Publisher UI React Application http://wso2.org diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/App.scss b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/App.css similarity index 100% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/App.scss rename to components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.ui/src/main/resources/publisher/src/App.css