mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Improve application artifact downloading functionality
This commit is contained in:
parent
8c50af1631
commit
3bc6ca31b3
@ -58,7 +58,7 @@ import javax.ws.rs.core.Response;
|
|||||||
public interface ArtifactDownloadAPI {
|
public interface ArtifactDownloadAPI {
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{tenantId}/{uuid}/{folderName}/{fileName}")
|
@Path("/{tenantId}/{appHashValue}/{folderName}/{fileName}")
|
||||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
@ApiOperation(
|
@ApiOperation(
|
||||||
produces = MediaType.APPLICATION_OCTET_STREAM,
|
produces = MediaType.APPLICATION_OCTET_STREAM,
|
||||||
@ -88,10 +88,10 @@ public interface ArtifactDownloadAPI {
|
|||||||
required = true)
|
required = true)
|
||||||
@PathParam("tenantId") int tenantId,
|
@PathParam("tenantId") int tenantId,
|
||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "uuid",
|
name = "hash-value",
|
||||||
value = "UUID of the application release.",
|
value = "Hash value of the application release.",
|
||||||
required = true)
|
required = true)
|
||||||
@PathParam("uuid") String uuid,
|
@PathParam("appHashValue") String uuid,
|
||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "folderName",
|
name = "folderName",
|
||||||
value = "Name of the folder where the artifact store.",
|
value = "Name of the folder where the artifact store.",
|
||||||
|
|||||||
@ -48,19 +48,20 @@ import javax.ws.rs.core.Response;
|
|||||||
@Path("/artifact")
|
@Path("/artifact")
|
||||||
public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
|
public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
|
||||||
|
|
||||||
private static Log log = LogFactory.getLog(ArtifactDownloadAPIImpl.class);
|
private static final Log log = LogFactory.getLog(ArtifactDownloadAPIImpl.class);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Override
|
@Override
|
||||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
@Path("/{tenantId}/{uuid}/{folderName}/{fileName}")
|
@Path("/{tenantId}/{appHashValue}/{folderName}/{fileName}")
|
||||||
public Response getArtifact(
|
public Response getArtifact(
|
||||||
@PathParam("tenantId") int tenantId,
|
@PathParam("tenantId") int tenantId,
|
||||||
@PathParam("uuid") String uuid,
|
@PathParam("appHashValue") String appHashValue,
|
||||||
@PathParam("folderName") String folderName,
|
@PathParam("folderName") String folderName,
|
||||||
@PathParam("fileName") String fileName) {
|
@PathParam("fileName") String fileName) {
|
||||||
AppmDataHandler dataHandler = APIUtil.getDataHandler();
|
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);
|
byte[] content = IOUtils.toByteArray(fileInputStream);
|
||||||
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
|
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
|
||||||
Response.ResponseBuilder response = Response
|
Response.ResponseBuilder response = Response
|
||||||
@ -75,12 +76,13 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
|
|||||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
}
|
}
|
||||||
} catch (NotFoundException e) {
|
} 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);
|
log.error(msg, e);
|
||||||
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
||||||
} catch (BadRequestException e) {
|
} catch (BadRequestException e) {
|
||||||
String msg = "Invalid data is used with the request to get input stream of the application release. UUID: "
|
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);
|
log.error(msg, e);
|
||||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||||
} catch (ApplicationManagementException e) {
|
} catch (ApplicationManagementException e) {
|
||||||
|
|||||||
@ -28,7 +28,16 @@ public interface AppmDataHandler {
|
|||||||
|
|
||||||
Map<String, LifecycleState> getLifecycleConfiguration() throws LifecycleManagementException;
|
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;
|
throws ApplicationManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package org.wso2.carbon.device.application.mgt.core.impl;
|
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.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.device.application.mgt.common.config.LifecycleState;
|
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.exception.LifecycleManagementException;
|
||||||
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
|
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.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.exception.BadRequestException;
|
||||||
import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
|
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.exception.NotFoundException;
|
||||||
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
|
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.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.common.exceptions.DeviceManagementException;
|
||||||
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||||
|
|
||||||
@ -54,38 +51,58 @@ public class AppmDataHandlerImpl implements AppmDataHandler {
|
|||||||
return lifecycleStateManager.getLifecycleConfig();
|
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 {
|
throws ApplicationManagementException {
|
||||||
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
|
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
|
||||||
ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
|
validateArtifactDownloadRequest(tenantId, appHashValue, folderName, artifactName);
|
||||||
String appReleaseHashValue;
|
|
||||||
try {
|
try {
|
||||||
ConnectionManagerUtil.openDBConnection();
|
InputStream inputStream = applicationStorageManager
|
||||||
appReleaseHashValue = applicationReleaseDAO.getReleaseHashValue(uuid, tenantId);
|
.getFileStream(appHashValue, folderName, artifactName, tenantId);
|
||||||
if (appReleaseHashValue == null) {
|
if (inputStream == null) {
|
||||||
String msg = "Could't find application release for UUID: " + uuid + ". Hence try with valid UUID.";
|
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);
|
log.error(msg);
|
||||||
throw new NotFoundException(msg);
|
throw new NotFoundException(msg);
|
||||||
}
|
}
|
||||||
InputStream inputStream = applicationStorageManager
|
|
||||||
.getFileStream(appReleaseHashValue, folderName, artifactName, tenantId);
|
|
||||||
if (inputStream == null) {
|
|
||||||
String msg = "Couldn't find the file in the file system.";
|
|
||||||
log.error(msg);
|
|
||||||
throw new ApplicationManagementException(msg);
|
|
||||||
}
|
|
||||||
return inputStream;
|
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) {
|
} catch (ApplicationStorageManagementException e) {
|
||||||
String msg = "Error occurred when getting input stream of the " + artifactName + " file.";
|
String msg = "Error occurred when getting input stream of the " + artifactName + " file.";
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new ApplicationManagementException(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)
|
public static ApplicationRelease releaseDtoToRelease(ApplicationReleaseDTO applicationReleaseDTO)
|
||||||
throws ApplicationManagementException {
|
throws ApplicationManagementException {
|
||||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||||
String basePath =
|
String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO
|
||||||
getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid()
|
.getAppHashValue() + Constants.FORWARD_SLASH;
|
||||||
+ Constants.FORWARD_SLASH;
|
|
||||||
|
|
||||||
List<String> screenshotPaths = new ArrayList<>();
|
List<String> screenshotPaths = new ArrayList<>();
|
||||||
ApplicationRelease applicationRelease = new ApplicationRelease();
|
ApplicationRelease applicationRelease = new ApplicationRelease();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user