mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Improve device type agent downloading API
This commit is contained in:
parent
dc05306aa9
commit
6f9e0c3050
@ -33,6 +33,7 @@ import javax.ws.rs.GET;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
@ -133,7 +134,7 @@ public interface ArtifactDownloadAPI {
|
|||||||
@PathParam("uuid") String uuid);
|
@PathParam("uuid") String uuid);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{deviceType}/agent/{tenantId}")
|
@Path("/{deviceType}/agent")
|
||||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
@ApiOperation(
|
@ApiOperation(
|
||||||
produces = MediaType.APPLICATION_OCTET_STREAM,
|
produces = MediaType.APPLICATION_OCTET_STREAM,
|
||||||
@ -156,7 +157,7 @@ public interface ArtifactDownloadAPI {
|
|||||||
message = "Internal Server Error. \n Error occurred while getting the agent.",
|
message = "Internal Server Error. \n Error occurred while getting the agent.",
|
||||||
response = ErrorResponse.class)
|
response = ErrorResponse.class)
|
||||||
})
|
})
|
||||||
Response getAndroidAgent(
|
Response getDeviceTypeAgent(
|
||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "deviceType",
|
name = "deviceType",
|
||||||
value = "Device type of the agent.",
|
value = "Device type of the agent.",
|
||||||
@ -164,8 +165,8 @@ public interface ArtifactDownloadAPI {
|
|||||||
required = true)
|
required = true)
|
||||||
@PathParam("deviceType") String deviceType,
|
@PathParam("deviceType") String deviceType,
|
||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "tenantId",
|
name = "tenantDomain",
|
||||||
value = "Tenant Id of the application artifact belongs.",
|
value = "Tenant Domain of the application artifact belongs.",
|
||||||
required = true)
|
defaultValue = "carbon.super")
|
||||||
@PathParam("tenantId") int tenantId);
|
@QueryParam("tenantDomain") String tenantDomain);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,14 +27,17 @@ 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.BadRequestException;
|
||||||
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.util.APIUtil;
|
import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
|
||||||
|
import org.wso2.carbon.device.application.mgt.core.util.Constants;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import javax.ws.rs.DefaultValue;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
@ -114,17 +117,19 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
|
|||||||
@GET
|
@GET
|
||||||
@Override
|
@Override
|
||||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
@Path("/{deviceType}/agent/{tenantId}")
|
@Path("/{deviceType}/agent")
|
||||||
public Response getAndroidAgent(@PathParam("deviceType") String deviceType,
|
public Response getDeviceTypeAgent(@PathParam("deviceType") String deviceType,
|
||||||
@PathParam("tenantId") int tenantId) {
|
@DefaultValue("carbon.super")
|
||||||
|
@QueryParam("tenantDomain") String tenantDomain) {
|
||||||
AppmDataHandler dataHandler = APIUtil.getDataHandler();
|
AppmDataHandler dataHandler = APIUtil.getDataHandler();
|
||||||
try (InputStream fileInputStream = dataHandler.getAgentStream(tenantId, deviceType)) {
|
try (InputStream fileInputStream = dataHandler.getAgentStream(tenantDomain, deviceType)) {
|
||||||
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
|
||||||
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
|
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
|
||||||
response.status(Response.Status.OK);
|
response.status(Response.Status.OK);
|
||||||
response.header("Content-Disposition", "attachment; filename=\"" + deviceType + " agent\"");
|
String fileName = Constants.AGENT_FILE_NAMES.get(deviceType);
|
||||||
|
response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||||
response.header("Content-Length", content.length);
|
response.header("Content-Length", content.length);
|
||||||
return response.build();
|
return response.build();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -133,15 +138,15 @@ 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 agent for device type: " + deviceType;
|
String msg = "Requesting device type agent for unsupported device type " + deviceType;
|
||||||
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 device type received: " + deviceType + ".Valid device type is android";
|
String msg = "Couldn't find the device type agent in the system.";
|
||||||
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) {
|
||||||
String msg = "Error occurred while getting the application release artifact file. ";
|
String msg = "Error occurred while getting the device type agent. ";
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@ -116,10 +116,9 @@ public interface ApplicationStorageManager {
|
|||||||
* Get the InputStream of the file which is located in filePath
|
* Get the InputStream of the file which is located in filePath
|
||||||
*
|
*
|
||||||
* @param deviceType device type name
|
* @param deviceType device type name
|
||||||
* @param tenantId local tenant's id
|
* @param tenantDomain tenant domain name
|
||||||
* @return {@link InputStream}
|
* @return {@link InputStream}
|
||||||
* @throws ApplicationStorageManagementException throws if an error occurs when accessing the file.
|
* @throws ApplicationStorageManagementException throws if an error occurs when accessing the file.
|
||||||
*/
|
*/
|
||||||
InputStream getFileStream(String deviceType, int tenantId)
|
InputStream getFileStream(String deviceType, String tenantDomain) throws ApplicationStorageManagementException;
|
||||||
throws ApplicationStorageManagementException, RequestValidatingException;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,11 +34,10 @@ public interface AppmDataHandler {
|
|||||||
/**
|
/**
|
||||||
* Get agent apk
|
* Get agent apk
|
||||||
*
|
*
|
||||||
* @param tenantId local tenant
|
* @param tenantDomain tenant domain name
|
||||||
* @param deviceType device type name
|
* @param deviceType device type name
|
||||||
* @return {@link InputStream}
|
* @return {@link InputStream}
|
||||||
* @throws ApplicationManagementException throws if an error occurs when accessing the file.
|
* @throws ApplicationManagementException throws if an error occurs when accessing the file.
|
||||||
*/
|
*/
|
||||||
InputStream getAgentStream(int tenantId, String deviceType)
|
InputStream getAgentStream(String tenantDomain, String deviceType) throws ApplicationManagementException;
|
||||||
throws ApplicationManagementException;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,14 +20,12 @@ 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;
|
||||||
@ -252,24 +250,17 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getFileStream(String deviceType, int tenantId)
|
public InputStream getFileStream(String deviceType, String tenantDomain) throws ApplicationStorageManagementException {
|
||||||
throws ApplicationStorageManagementException, RequestValidatingException {
|
String fileName = Constants.AGENT_FILE_NAMES.get(deviceType);
|
||||||
if (StringUtils.isNotBlank(deviceType) && deviceType.equals("android")) {
|
String filePath =
|
||||||
String fileName = Constants.ANDROID_AGENT;
|
storagePath + File.separator + "agents" + File.separator + deviceType.toLowerCase() + File.separator
|
||||||
String filePath =
|
+ tenantDomain + File.separator + fileName;
|
||||||
storagePath + File.separator + "agents" + File.separator + deviceType + File.separator
|
try {
|
||||||
+ tenantId + File.separator + fileName;
|
return StorageManagementUtil.getInputStream(filePath);
|
||||||
try {
|
} catch (IOException e) {
|
||||||
return StorageManagementUtil.getInputStream(filePath);
|
String msg = "Error occured when accessing the file in file path: " + filePath;
|
||||||
} catch (IOException e) {
|
log.error(msg, e);
|
||||||
String msg = "Error occured when accessing the file in file path: " + filePath;
|
throw new ApplicationStorageManagementException(msg, e);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
@ -36,6 +37,8 @@ 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.application.mgt.core.util.ConnectionManagerUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -90,26 +93,32 @@ public class AppmDataHandlerImpl implements AppmDataHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getAgentStream(int tenantId, String deviceType)
|
public InputStream getAgentStream(String tenantDomain, String deviceType) throws ApplicationManagementException {
|
||||||
throws ApplicationManagementException {
|
|
||||||
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
|
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = applicationStorageManager
|
DeviceType deviceTypeObj = DataHolder.getInstance().getDeviceManagementService().getDeviceType(deviceType);
|
||||||
.getFileStream(deviceType, tenantId);
|
if (deviceTypeObj == null) {
|
||||||
if (inputStream == null) {
|
String msg = "Couldn't find a registered device type called " + deviceType + " in the system.";
|
||||||
String msg = "Couldn't find the file in the file system for device type: " + deviceType;
|
|
||||||
log.error(msg);
|
log.error(msg);
|
||||||
throw new NotFoundException(msg);
|
throw new NotFoundException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputStream inputStream = applicationStorageManager.getFileStream(deviceType, tenantDomain);
|
||||||
|
if (inputStream == null) {
|
||||||
|
String msg = "Couldn't find the device type agent in the server. Device type: " + deviceType
|
||||||
|
+ " Tenant Domain: " + tenantDomain;
|
||||||
|
log.error(msg);
|
||||||
|
throw new BadRequestException(msg);
|
||||||
|
}
|
||||||
return inputStream;
|
return inputStream;
|
||||||
} catch (ApplicationStorageManagementException e) {
|
} catch (ApplicationStorageManagementException e) {
|
||||||
String msg = "Error occurred when getting input stream of the " + deviceType + " agent.";
|
String msg = "Error occurred when getting input stream of the " + deviceType + " agent.";
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new ApplicationManagementException(msg, e);
|
throw new ApplicationManagementException(msg, e);
|
||||||
} catch (RequestValidatingException e) {
|
} catch (DeviceManagementException e) {
|
||||||
String msg = "Error invalid request received with device type: " + deviceType;
|
String msg = " Error occurred when getting device type details. Device type " + deviceType;
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new BadRequestException(msg, e);
|
throw new ApplicationManagementException(msg, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,9 @@ package org.wso2.carbon.device.application.mgt.core.util;
|
|||||||
import org.wso2.carbon.utils.CarbonUtils;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application Management related constants.
|
* Application Management related constants.
|
||||||
@ -61,7 +64,12 @@ 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";
|
private static final Map<String, String> AGENT_DATA = new HashMap<>();
|
||||||
|
static {
|
||||||
|
AGENT_DATA.put("android", "android-agent.apk");
|
||||||
|
AGENT_DATA.put("ios", "ios.ipa");
|
||||||
|
}
|
||||||
|
public static final Map<String, String> AGENT_FILE_NAMES = Collections.unmodifiableMap(AGENT_DATA);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -2974,15 +2974,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeviceType getDeviceType(String deviceType) throws DeviceManagementException {
|
public DeviceType getDeviceType(String deviceType) throws DeviceManagementException {
|
||||||
if (deviceType != null) {
|
if (StringUtils.isBlank(deviceType)) {
|
||||||
if (log.isDebugEnabled()) {
|
String msg = "Received either whitespace, empty (\"\") or null value as device type to get device type "
|
||||||
log.debug("Get device type '" + deviceType + "'");
|
+ "details.";
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String msg = "Received null deviceType for getDeviceType";
|
|
||||||
log.error(msg);
|
log.error(msg);
|
||||||
throw new DeviceManagementException(msg);
|
throw new DeviceManagementException(msg);
|
||||||
}
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Get device type '" + deviceType + "'");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DeviceManagementDAOFactory.openConnection();
|
DeviceManagementDAOFactory.openConnection();
|
||||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user