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 fc9dece5608..df76facbd24 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 2a34e5feacf..61b9be5f477 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,31 @@ 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); + 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); + 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 e32c4b10891..fe7c97ef640 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 bfab5f78dfd..508d7afa3b9 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 a0a2c0c9c6e..d8d473932cb 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 8fdaa315b23..bbd8489d9f4 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 952ae9609fa..9a0aee2efe3 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 06f9da533cc..7c48a76895f 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,9 +127,11 @@ public class StorageManagementUtil { */ public static InputStream getInputStream (String filePath) throws IOException { File sourceFile = new File(filePath); - - try (InputStream inputStream = new FileInputStream(filePath)){ - return inputStream; + if (!sourceFile.exists()){ + return null; + } + 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 e65dd2bb7e8..b138c7686bf 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()); + } } }