Improve application artifact downloading functionality

revert-70ac1926
tcdlpds@gmail.com 4 years ago
parent 8c50af1631
commit 3bc6ca31b3

@ -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.",

@ -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) {

@ -28,7 +28,16 @@ public interface AppmDataHandler {
Map<String, LifecycleState> 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;
/**

@ -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);
}
}

@ -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<String> screenshotPaths = new ArrayList<>();
ApplicationRelease applicationRelease = new ApplicationRelease();

Loading…
Cancel
Save