Add API to download device-type agent

feature/appm-store/pbac
Yohan Avishke 5 years ago committed by Dharmakeerthi Lasantha
parent cee3ef55d1
commit d07f5b3713

@ -131,4 +131,41 @@ public interface ArtifactDownloadAPI {
value = "UUID of the application release.", value = "UUID of the application release.",
required = true) required = true)
@PathParam("uuid") String uuid); @PathParam("uuid") String uuid);
@GET
@Path("/{deviceType}/agent/{tenantId}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@ApiOperation(
produces = MediaType.APPLICATION_OCTET_STREAM,
httpMethod = "GET",
value = "get the agent of a device type",
notes = "This will download an agent depending on the device type"
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully got the agent.",
response = ApplicationList.class),
@ApiResponse(
code = 404,
message = "Not Found. There doesn't have an defined agent for the resource." +
"query."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while getting the agent.",
response = ErrorResponse.class)
})
Response getAndroidAgent(
@ApiParam(
name = "deviceType",
value = "Device type of the agent.",
example = "android",
required = true)
@PathParam("deviceType") String deviceType,
@ApiParam(
name = "tenantId",
value = "Tenant Id of the application artifact belongs.",
required = true)
@PathParam("tenantId") int tenantId);
} }

@ -110,4 +110,44 @@ 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();
} }
} }
@GET
@Override
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/{deviceType}/agent/{tenantId}")
public Response getAndroidAgent(@PathParam("deviceType") String deviceType,
@PathParam("tenantId") int tenantId) {
AppmDataHandler dataHandler = APIUtil.getDataHandler();
try (InputStream fileInputStream = dataHandler.getAgentStream(tenantId, deviceType)) {
byte[] content = IOUtils.toByteArray(fileInputStream);
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
Response.ResponseBuilder response = Response
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
response.status(Response.Status.OK);
response.header("Content-Disposition", "attachment; filename=\"" + deviceType + " agent\"");
response.header("Content-Length", content.length);
return response.build();
} catch (IOException e) {
String msg = "Error occurred while creating input stream from buffer array. ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (NotFoundException e) {
String msg = "Couldn't find an agent for device type: " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (BadRequestException e){
String msg = "Invalid device type received: " + deviceType + ".Valid device type is android";
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();
} catch (IOException e) {
String msg = "Error occurred while getting the byte array of application release artifact file. ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.common.services;
import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import java.io.InputStream; import java.io.InputStream;
@ -110,4 +111,15 @@ public interface ApplicationStorageManager {
*/ */
InputStream getFileStream(String hashVal, String folderName, String fileName, int tenantId) InputStream getFileStream(String hashVal, String folderName, String fileName, int tenantId)
throws ApplicationStorageManagementException; throws ApplicationStorageManagementException;
/**
* Get the InputStream of the file which is located in filePath
*
* @param deviceType device type name
* @param tenantId local tenant's id
* @return {@link InputStream}
* @throws ApplicationStorageManagementException throws if an error occurs when accessing the file.
*/
InputStream getFileStream(String deviceType, int tenantId)
throws ApplicationStorageManagementException, RequestValidatingException;
} }

@ -37,4 +37,15 @@ public interface AppmDataHandler {
InputStream getArtifactStream(int tenantId, String uuid, String folderName, String artifactName) InputStream getArtifactStream(int tenantId, String uuid, String folderName, String artifactName)
throws ApplicationManagementException; throws ApplicationManagementException;
/**
* Get agent apk
*
* @param tenantId local tenant
* @param deviceType device type name
* @return {@link InputStream}
* @throws ApplicationManagementException throws if an error occurs when accessing the file.
*/
InputStream getAgentStream(int tenantId, String deviceType)
throws ApplicationManagementException;
} }

@ -20,12 +20,14 @@ package org.wso2.carbon.device.application.mgt.core.impl;
import com.dd.plist.NSDictionary; import com.dd.plist.NSDictionary;
import net.dongliu.apk.parser.bean.ApkMeta; import net.dongliu.apk.parser.bean.ApkMeta;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
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.ApplicationInstaller; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO;
import org.wso2.carbon.device.application.mgt.common.DeviceTypes; import org.wso2.carbon.device.application.mgt.common.DeviceTypes;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
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.core.exception.ParsingException; import org.wso2.carbon.device.application.mgt.core.exception.ParsingException;
@ -249,6 +251,28 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
} }
} }
@Override
public InputStream getFileStream(String deviceType, int tenantId)
throws ApplicationStorageManagementException, RequestValidatingException {
if (StringUtils.isNotBlank(deviceType) && deviceType.equals("android")) {
String fileName = Constants.ANDROID_AGENT;
String filePath =
storagePath + File.separator + "agents" + File.separator + deviceType + File.separator
+ tenantId + File.separator + fileName;
try {
return StorageManagementUtil.getInputStream(filePath);
} catch (IOException e) {
String msg = "Error occured when accessing the file in file path: " + filePath;
log.error(msg, e);
throw new ApplicationStorageManagementException(msg, e);
}
} else {
String msg = "Error occurred while accessing the file invalid device type: " + deviceType;
log.error(msg);
throw new RequestValidatingException(msg);
}
}
/*** /***
* This method is responsible to delete artifact file which is located in the artifact path. * This method is responsible to delete artifact file which is located in the artifact path.
* *

@ -24,11 +24,13 @@ import org.wso2.carbon.device.application.mgt.common.config.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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.exception.ApplicationStorageManagementException;
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.exception.RequestValidatingException;
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.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; 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.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.util.DAOUtil; import org.wso2.carbon.device.application.mgt.core.util.DAOUtil;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; 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;
@ -76,7 +78,7 @@ public class AppmDataHandlerImpl implements AppmDataHandler {
InputStream inputStream = applicationStorageManager InputStream inputStream = applicationStorageManager
.getFileStream(appReleaseHashValue, folderName, artifactName, tenantId); .getFileStream(appReleaseHashValue, folderName, artifactName, tenantId);
if (inputStream == null) { if (inputStream == null) {
String msg = "Couldn't file the file in the file system."; String msg = "Couldn't find the file in the file system.";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg); throw new ApplicationManagementException(msg);
} }
@ -94,4 +96,28 @@ public class AppmDataHandlerImpl implements AppmDataHandler {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override
public InputStream getAgentStream(int tenantId, String deviceType)
throws ApplicationManagementException {
ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager();
try {
InputStream inputStream = applicationStorageManager
.getFileStream(deviceType, tenantId);
if (inputStream == null) {
String msg = "Couldn't find the file in the file system for device type: " + deviceType;
log.error(msg);
throw new NotFoundException(msg);
}
return inputStream;
} catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when getting input stream of the " + deviceType + " agent.";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (RequestValidatingException e) {
String msg = "Error invalid request received with device type: " + deviceType;
log.error(msg, e);
throw new BadRequestException(msg, e);
}
}
} }

@ -61,6 +61,8 @@ public class Constants {
public static final String SUBSCRIBED = "SUBSCRIBED"; public static final String SUBSCRIBED = "SUBSCRIBED";
public static final String UNSUBSCRIBED = "UNSUBSCRIBED"; public static final String UNSUBSCRIBED = "UNSUBSCRIBED";
public static final String ANDROID_AGENT = "android-agent.apk";
/** /**
* Database types supported by Application Management. * Database types supported by Application Management.

Loading…
Cancel
Save