From f1672ee961210b4eb3c7c5bf7eab38f349e53e87 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Apr 2019 10:45:16 +0530 Subject: [PATCH] Add artifact download API --- .../mgt/api/services/impl/ArtifactDownloadAPIImpl.java | 7 ++++++- .../application/mgt/core/util/StorageManagementUtil.java | 5 ++--- .../device/application/mgt/handler/util/HandlerUtil.java | 6 +++++- 3 files changed, 13 insertions(+), 5 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/impl/ArtifactDownloadAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java index 0929f20403..61b9be5f47 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java @@ -52,7 +52,12 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { AppmDataHandler dataHandler = APIUtil.getDataHandler(); try { InputStream fileInputStream = dataHandler.getArtifactStream(uuid, fileName); - return Response.status(Response.Status.OK).entity(fileInputStream).build(); + Response.ResponseBuilder response = Response + .ok(fileInputStream, MediaType.APPLICATION_OCTET_STREAM); + response.status(Response.Status.OK); +// response.type("application/html"); + response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); + return response.build(); } catch (NotFoundException e) { String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName; log.error(msg, e); 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 index 6b08dad7ca..7c48a76895 100644 --- 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 @@ -130,9 +130,8 @@ public class StorageManagementUtil { if (!sourceFile.exists()){ return null; } - - try (InputStream inputStream = new FileInputStream(sourceFile)){ - return inputStream; + try { + return new FileInputStream(sourceFile); } catch (FileNotFoundException e) { String msg = "Couldn't file the file in file path: " + filePath; log.error(msg); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java index e65dd2bb7e..b138c7686b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java @@ -207,7 +207,11 @@ public class HandlerUtil { resp.setCharacterEncoding("UTF-8"); proxyResponse.setExecutorResponse(null); try (PrintWriter writer = resp.getWriter()) { - writer.write(gson.toJson(proxyResponse)); + if (proxyResponse.getCode() == HttpStatus.SC_OK){ + writer.write(gson.toJson(proxyResponse.getData())); + } else{ + writer.write(proxyResponse.getData()); + } } }