From c9f928915efc1244385cf3e2568e801c17538a59 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Apr 2019 02:58:59 +0530 Subject: [PATCH 1/2] Add application artifact dowloading API --- .../mgt/api/services/ArtifactDownloadAPI.java | 4 +- .../impl/ArtifactDownloadAPIImpl.java | 28 ++++++---- .../services/impl/ConfigRetrieveAPIImpl.java | 12 +---- .../mgt/common/services/AppmDataHandler.java | 5 +- .../GenericApplicationReleaseDAOImpl.java | 3 +- .../mgt/core/impl/ApplicationManagerImpl.java | 1 + .../mgt/core/impl/AppmDataHandlerImpl.java | 53 ++++++++++++------- .../mgt/core/util/StorageManagementUtil.java | 5 +- 8 files changed, 63 insertions(+), 48 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/ArtifactDownloadAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java index fc9dece560..df76facbd2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java @@ -52,12 +52,12 @@ import javax.ws.rs.core.Response; ) ) @Path("/artifact") -@Api(value = "ApplicationDTO Management Artifact Downloading Service", description = "This API carries all application management artifact downloading services") +@Api(value = "ApplicationDTO Management Artifact Downloading Service") @Produces(MediaType.APPLICATION_JSON) public interface ArtifactDownloadAPI { @GET - @Path("/download-artifact/{uuid}/{fileName}") + @Path("/{uuid}/{fileName}") @Produces(MediaType.APPLICATION_OCTET_STREAM) @ApiOperation( produces = MediaType.APPLICATION_OCTET_STREAM, 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 2a34e5feac..0929f20403 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 @@ -20,9 +20,10 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI; -import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import javax.ws.rs.GET; @@ -31,6 +32,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.io.InputStream; /** * Implementation of ApplicationDTO Management related APIs. @@ -44,20 +46,26 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { @GET @Override @Produces(MediaType.APPLICATION_OCTET_STREAM) - @Path("/download-artifact/{uuid}/{fileName}") - public Response getArtifact( - @PathParam("uuid") String uuid, + @Path("/{uuid}/{fileName}") + public Response getArtifact(@PathParam("uuid") String uuid, @PathParam("fileName") String fileName) { AppmDataHandler dataHandler = APIUtil.getDataHandler(); try { - UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); - return Response.status(Response.Status.OK).entity(uiConfiguration).build(); - - }catch (ApplicationManagementException e) { - String msg = "Error occurred while getting the application list for publisher "; + InputStream fileInputStream = dataHandler.getArtifactStream(uuid, fileName); + return Response.status(Response.Status.OK).entity(fileInputStream).build(); + } catch (NotFoundException e) { + String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (BadRequestException e) { + String msg = "Invalid data is used with the request to get input stream of the application release. UUID: " + + uuid + " and file name: " + fileName; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while getting the application release artifact file. "; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } - } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java index e32c4b1089..fe7c97ef64 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java @@ -23,7 +23,6 @@ import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; -import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -46,15 +45,8 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { @Path("/ui-config") public Response getUiConfig() { AppmDataHandler dataHandler = APIUtil.getDataHandler(); - try { - UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); - return Response.status(Response.Status.OK).entity(uiConfiguration).build(); - - }catch (ApplicationManagementException e) { - String msg = "Error occurred while getting the application list for publisher "; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); + return Response.status(Response.Status.OK).entity(uiConfiguration).build(); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java index bfab5f78df..508d7afa3b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java @@ -27,9 +27,8 @@ public interface AppmDataHandler { * Get UI configuration which is defined in the app-manager.xml * * @return {@link UIConfiguration} UI configuration - * @throws ApplicationManagementException Exceptions of the ApplicationDTO management. */ - UIConfiguration getUIConfiguration() throws ApplicationManagementException; + UIConfiguration getUIConfiguration(); - InputStream getArtifactStream(String md5sum, String artifactName) throws ApplicationManagementException; + InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index a0a2c0c9c6..d8d473932c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -663,13 +663,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - ApplicationReleaseArtifactPaths applicationReleaseArtifactPaths = null; String releaseHashValue = null; try { conn = this.getDBConnection(); String sql = "SELECT " + "AR.APP_HASH_VALUE AS HASH_VALUE " - + "FROM AP_APP_RELEASE " + + "FROM AP_APP_RELEASE AR " + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;"; stmt = conn.prepareStatement(sql); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 8fdaa315b2..bbd8489d9f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1947,6 +1947,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid(); ApplicationRelease applicationRelease = new ApplicationRelease(); applicationRelease.setDescription(applicationReleaseDTO.getDescription()); + applicationRelease.setUuid(applicationReleaseDTO.getUuid()); applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType()); applicationRelease.setPrice(applicationReleaseDTO.getPrice()); applicationRelease.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 952ae9609f..9a0aee2efe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -17,9 +17,9 @@ package org.wso2.carbon.device.application.mgt.core.impl; -import org.apache.commons.lang.StringUtils; +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.ApplicationReleaseArtifactPaths; 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.ApplicationStorageManager; @@ -29,7 +29,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; -import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import java.io.InputStream; @@ -37,42 +38,54 @@ import java.io.InputStream; public class AppmDataHandlerImpl implements AppmDataHandler { private UIConfiguration uiConfiguration; + private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class); + public AppmDataHandlerImpl(UIConfiguration config) { this.uiConfiguration = config; } @Override - public UIConfiguration getUIConfiguration() throws ApplicationManagementException { + public UIConfiguration getUIConfiguration() { return this.uiConfiguration; } @Override -// throws ApplicationManagementException - public InputStream getArtifactStream(String uuid, String artifactName) { + public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); String artifactPath; - - String appReleaseHashValue = null; + String appReleaseHashValue; try { + ConnectionManagerUtil.openDBConnection(); appReleaseHashValue = applicationReleaseDAO.getReleaseHashValue(uuid, tenantId); + if (appReleaseHashValue == null) { + String msg = "Could't find application release for UUID: " + uuid + ". Hence try with valid UUID."; + log.error(msg); + throw new NotFoundException(msg); + } artifactPath = appReleaseHashValue + Constants.FORWARD_SLASH + artifactName; - return applicationStorageManager.getFileSttream(artifactPath); - - + InputStream inputStream = applicationStorageManager.getFileSttream(artifactPath); + if (inputStream == null) { + String msg = "Couldn't file the file in the file system. File path: " + artifactPath; + log.error(msg); + throw new ApplicationManagementException(msg); + } + return inputStream; } catch (ApplicationManagementDAOException e) { -// todo throw -// throw new ApplicationManagementException(); -// e.printStackTrace(); - }catch (ApplicationStorageManagementException e) { - // todo throw - // throw new ApplicationManagementException(); - // e.printStackTrace(); + String msg = + "Error occurred when retrieving application release hash value for given application release UUID: " + + uuid; + log.error(msg); + throw new ApplicationManagementException(msg); + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred when getting input stream of the " + artifactName + " file."; + log.error(msg); + throw new ApplicationManagementException(msg); + } finally { + ConnectionManagerUtil.closeDBConnection(); } - - return 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/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 06f9da533c..6b08dad7ca 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 @@ -127,8 +127,11 @@ public class StorageManagementUtil { */ public static InputStream getInputStream (String filePath) throws IOException { File sourceFile = new File(filePath); + if (!sourceFile.exists()){ + return null; + } - try (InputStream inputStream = new FileInputStream(filePath)){ + try (InputStream inputStream = new FileInputStream(sourceFile)){ return inputStream; } catch (FileNotFoundException e) { String msg = "Couldn't file the file in file path: " + filePath; From f1672ee961210b4eb3c7c5bf7eab38f349e53e87 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Apr 2019 10:45:16 +0530 Subject: [PATCH 2/2] 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()); + } } }