From 6f9e0c30501e4ee20713bfb7638b30b44274e44c Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Wed, 6 May 2020 00:38:45 +0530 Subject: [PATCH] Improve device type agent downloading API --- .../mgt/api/services/ArtifactDownloadAPI.java | 13 ++++---- .../impl/ArtifactDownloadAPIImpl.java | 21 ++++++++----- .../services/ApplicationStorageManager.java | 5 ++- .../mgt/common/services/AppmDataHandler.java | 5 ++- .../impl/ApplicationStorageManagerImpl.java | 31 +++++++------------ .../mgt/core/impl/AppmDataHandlerImpl.java | 27 ++++++++++------ .../application/mgt/core/util/Constants.java | 10 +++++- .../DeviceManagementProviderServiceImpl.java | 13 ++++---- 8 files changed, 69 insertions(+), 56 deletions(-) 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 acae6d1a5b8..5c91b6ab8ff 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 @@ -33,6 +33,7 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -133,7 +134,7 @@ public interface ArtifactDownloadAPI { @PathParam("uuid") String uuid); @GET - @Path("/{deviceType}/agent/{tenantId}") + @Path("/{deviceType}/agent") @Produces(MediaType.APPLICATION_OCTET_STREAM) @ApiOperation( produces = MediaType.APPLICATION_OCTET_STREAM, @@ -156,7 +157,7 @@ public interface ArtifactDownloadAPI { message = "Internal Server Error. \n Error occurred while getting the agent.", response = ErrorResponse.class) }) - Response getAndroidAgent( + Response getDeviceTypeAgent( @ApiParam( name = "deviceType", value = "Device type of the agent.", @@ -164,8 +165,8 @@ public interface ArtifactDownloadAPI { required = true) @PathParam("deviceType") String deviceType, @ApiParam( - name = "tenantId", - value = "Tenant Id of the application artifact belongs.", - required = true) - @PathParam("tenantId") int tenantId); + name = "tenantDomain", + value = "Tenant Domain of the application artifact belongs.", + defaultValue = "carbon.super") + @QueryParam("tenantDomain") String tenantDomain); } 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 d0199b1134a..118fbac8ec5 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 @@ -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.NotFoundException; 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.IOException; import java.io.InputStream; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -114,17 +117,19 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { @GET @Override @Produces(MediaType.APPLICATION_OCTET_STREAM) - @Path("/{deviceType}/agent/{tenantId}") - public Response getAndroidAgent(@PathParam("deviceType") String deviceType, - @PathParam("tenantId") int tenantId) { + @Path("/{deviceType}/agent") + public Response getDeviceTypeAgent(@PathParam("deviceType") String deviceType, + @DefaultValue("carbon.super") + @QueryParam("tenantDomain") String tenantDomain) { AppmDataHandler dataHandler = APIUtil.getDataHandler(); - try (InputStream fileInputStream = dataHandler.getAgentStream(tenantId, deviceType)) { + try (InputStream fileInputStream = dataHandler.getAgentStream(tenantDomain, 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\""); + String fileName = Constants.AGENT_FILE_NAMES.get(deviceType); + response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.header("Content-Length", content.length); return response.build(); } catch (IOException e) { @@ -133,15 +138,15 @@ 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 agent for device type: " + deviceType; + String msg = "Requesting device type agent for unsupported 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"; + String msg = "Couldn't find the device type agent in the system."; 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. "; + String msg = "Error occurred while getting the device type agent. "; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (IOException 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/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 5bfe26e86b1..3d8b0f68215 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -116,10 +116,9 @@ public interface ApplicationStorageManager { * Get the InputStream of the file which is located in filePath * * @param deviceType device type name - * @param tenantId local tenant's id + * @param tenantDomain tenant domain name * @return {@link InputStream} * @throws ApplicationStorageManagementException throws if an error occurs when accessing the file. */ - InputStream getFileStream(String deviceType, int tenantId) - throws ApplicationStorageManagementException, RequestValidatingException; + InputStream getFileStream(String deviceType, String tenantDomain) throws ApplicationStorageManagementException; } 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 baf7fb9a2c5..23167b79f38 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 @@ -34,11 +34,10 @@ public interface AppmDataHandler { /** * Get agent apk * - * @param tenantId local tenant + * @param tenantDomain tenant domain name * @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; + InputStream getAgentStream(String tenantDomain, String deviceType) 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/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 8312ecfe579..fc58f3c9103 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -20,14 +20,12 @@ package org.wso2.carbon.device.application.mgt.core.impl; import com.dd.plist.NSDictionary; import net.dongliu.apk.parser.bean.ApkMeta; import org.apache.commons.io.IOUtils; -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.ApplicationInstaller; 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.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.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.core.exception.ParsingException; @@ -252,24 +250,17 @@ 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); + public InputStream getFileStream(String deviceType, String tenantDomain) throws ApplicationStorageManagementException { + String fileName = Constants.AGENT_FILE_NAMES.get(deviceType); + String filePath = + storagePath + File.separator + "agents" + File.separator + deviceType.toLowerCase() + File.separator + + tenantDomain + 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); } } 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 9752d613c02..46763dae2d7 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; @@ -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.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; import java.io.InputStream; import java.util.Map; @@ -90,26 +93,32 @@ public class AppmDataHandlerImpl implements AppmDataHandler { } @Override - public InputStream getAgentStream(int tenantId, String deviceType) - throws ApplicationManagementException { + public InputStream getAgentStream(String tenantDomain, String deviceType) throws ApplicationManagementException { ApplicationStorageManager applicationStorageManager = APIUtil.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; + DeviceType deviceTypeObj = DataHolder.getInstance().getDeviceManagementService().getDeviceType(deviceType); + if (deviceTypeObj == null) { + String msg = "Couldn't find a registered device type called " + deviceType + " in the system."; log.error(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; } 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; + } catch (DeviceManagementException e) { + String msg = " Error occurred when getting device type details. Device type " + deviceType; log.error(msg, e); - throw new BadRequestException(msg, e); + throw new ApplicationManagementException(msg, e); } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index c43c943d23a..e29959b427b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -21,6 +21,9 @@ package org.wso2.carbon.device.application.mgt.core.util; import org.wso2.carbon.utils.CarbonUtils; import java.io.File; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; /** * Application Management related constants. @@ -61,7 +64,12 @@ public class Constants { public static final String SUBSCRIBED = "SUBSCRIBED"; public static final String UNSUBSCRIBED = "UNSUBSCRIBED"; - public static final String ANDROID_AGENT = "android-agent.apk"; + private static final Map AGENT_DATA = new HashMap<>(); + static { + AGENT_DATA.put("android", "android-agent.apk"); + AGENT_DATA.put("ios", "ios.ipa"); + } + public static final Map AGENT_FILE_NAMES = Collections.unmodifiableMap(AGENT_DATA); /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 26dae9327e6..2b0e42eb19d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -2974,15 +2974,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public DeviceType getDeviceType(String deviceType) throws DeviceManagementException { - if (deviceType != null) { - if (log.isDebugEnabled()) { - log.debug("Get device type '" + deviceType + "'"); - } - } else { - String msg = "Received null deviceType for getDeviceType"; + if (StringUtils.isBlank(deviceType)) { + String msg = "Received either whitespace, empty (\"\") or null value as device type to get device type " + + "details."; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get device type '" + deviceType + "'"); + } + try { DeviceManagementDAOFactory.openConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();