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 5c91b6ab8ff..8a013e13707 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 @@ -58,7 +58,7 @@ import javax.ws.rs.core.Response; public interface ArtifactDownloadAPI { @GET - @Path("/{tenantId}/{uuid}/{folderName}/{fileName}") + @Path("/{tenantId}/{appHashValue}/{folderName}/{fileName}") @Produces(MediaType.APPLICATION_OCTET_STREAM) @ApiOperation( produces = MediaType.APPLICATION_OCTET_STREAM, @@ -88,10 +88,10 @@ public interface ArtifactDownloadAPI { required = true) @PathParam("tenantId") int tenantId, @ApiParam( - name = "uuid", - value = "UUID of the application release.", + name = "hash-value", + value = "Hash value of the application release.", required = true) - @PathParam("uuid") String uuid, + @PathParam("appHashValue") String uuid, @ApiParam( name = "folderName", value = "Name of the folder where the artifact store.", 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 118fbac8ec5..005d00e757a 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 @@ -48,19 +48,20 @@ import javax.ws.rs.core.Response; @Path("/artifact") public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { - private static Log log = LogFactory.getLog(ArtifactDownloadAPIImpl.class); + private static final Log log = LogFactory.getLog(ArtifactDownloadAPIImpl.class); @GET @Override @Produces(MediaType.APPLICATION_OCTET_STREAM) - @Path("/{tenantId}/{uuid}/{folderName}/{fileName}") + @Path("/{tenantId}/{appHashValue}/{folderName}/{fileName}") public Response getArtifact( @PathParam("tenantId") int tenantId, - @PathParam("uuid") String uuid, + @PathParam("appHashValue") String appHashValue, @PathParam("folderName") String folderName, @PathParam("fileName") String fileName) { AppmDataHandler dataHandler = APIUtil.getDataHandler(); - try (InputStream fileInputStream = dataHandler.getArtifactStream(tenantId, uuid, folderName, fileName)) { + try (InputStream fileInputStream = dataHandler + .getArtifactStream(tenantId, appHashValue, folderName, fileName)) { byte[] content = IOUtils.toByteArray(fileInputStream); try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { Response.ResponseBuilder response = Response @@ -75,12 +76,13 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } catch (NotFoundException e) { - String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName; + String msg = "Couldn't find an application release for app hash value: " + appHashValue + + " 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; + + appHashValue + " and file name: " + fileName; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ApplicationManagementException e) { 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 23167b79f38..e789fe54213 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 @@ -28,7 +28,16 @@ public interface AppmDataHandler { Map getLifecycleConfiguration() throws LifecycleManagementException; - InputStream getArtifactStream(int tenantId, String uuid, String folderName, String artifactName) + /** + * Get the Input Stream of the Artifact + * @param tenantId Tenant Id + * @param appHashValue Hash Value of the application + * @param folderName Folder Name + * @param artifactName Artifact Name + * @return {@link InputStream} + * @throws ApplicationManagementException if error occurred while getting Input Stream of the artifact + */ + InputStream getArtifactStream(int tenantId, String appHashValue, String folderName, 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/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 40c3de260c2..bd0f6ad79f1 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,6 +17,7 @@ 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.device.application.mgt.common.config.LifecycleState; @@ -25,15 +26,11 @@ import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorag import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; -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.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; @@ -54,38 +51,58 @@ public class AppmDataHandlerImpl implements AppmDataHandler { return lifecycleStateManager.getLifecycleConfig(); } - @Override public InputStream getArtifactStream(int tenantId, String uuid, String folderName, String artifactName) + @Override + public InputStream getArtifactStream(int tenantId, String appHashValue, String folderName, String artifactName) throws ApplicationManagementException { ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - String appReleaseHashValue; + validateArtifactDownloadRequest(tenantId, appHashValue, folderName, artifactName); 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); - } InputStream inputStream = applicationStorageManager - .getFileStream(appReleaseHashValue, folderName, artifactName, tenantId); + .getFileStream(appHashValue, folderName, artifactName, tenantId); if (inputStream == null) { - String msg = "Couldn't find the file in the file system."; + String msg = "Couldn't find the file in the file system. Tenant Id: " + tenantId + " App Has Value: " + + appHashValue + " Folder Name: " + folderName + " Artifact name: " + artifactName; log.error(msg); - throw new ApplicationManagementException(msg); + throw new NotFoundException(msg); } return inputStream; - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred when retrieving application release hash value for given application release " - + "UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); } catch (ApplicationStorageManagementException e) { String msg = "Error occurred when getting input stream of the " + artifactName + " file."; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * Validate the artifact downloading request + * @param tenantId Tenat Id + * @param appHashValue Application hash value + * @param folderName Folder Name + * @param artifactName Artifact name + * @throws BadRequestException if there is an invalid data to retrieve application artifact. + */ + private void validateArtifactDownloadRequest(int tenantId, String appHashValue, String folderName, + String artifactName) throws BadRequestException { + if (tenantId != -1234 && tenantId <= 0) { + String msg = "Found invalid tenant Id to get application artifact. Tenant Id: " + tenantId; + log.error(msg); + throw new BadRequestException(msg); + } + if (StringUtils.isBlank(appHashValue)) { + String msg = "Found invalid application has value to get application artifact. Application hash value: " + + appHashValue; + log.error(msg); + throw new BadRequestException(msg); + } + if (StringUtils.isBlank(folderName)) { + String msg = "Found invalid folder name to get application artifact. Folder name: " + folderName; + log.error(msg); + throw new BadRequestException(msg); + } + if (StringUtils.isBlank(artifactName)) { + String msg = "Found invalid artifact name to get application artifact. Artifact name: " + artifactName; + log.error(msg); + throw new BadRequestException(msg); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java index a2375f1d553..97e83d3128f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java @@ -379,9 +379,8 @@ public class APIUtil { public static ApplicationRelease releaseDtoToRelease(ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String basePath = - getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid() - + Constants.FORWARD_SLASH; + String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO + .getAppHashValue() + Constants.FORWARD_SLASH; List screenshotPaths = new ArrayList<>(); ApplicationRelease applicationRelease = new ApplicationRelease();