From 3bce479b4b2efbf9bf04ee60ddc654b96329606b Mon Sep 17 00:00:00 2001 From: charitha Date: Mon, 28 Aug 2017 18:57:37 +0530 Subject: [PATCH] Fix for https://github.com/wso2/product-iots/issues/1305 --- .../impl/DeviceManagementServiceImpl.java | 9 +-- ...ApplicationManagerProviderServiceImpl.java | 66 ++++++++++++------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 5118221ac3..6e49f7bdfb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.Feature; @@ -66,7 +65,6 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import javax.security.auth.login.Configuration; import javax.validation.Valid; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; @@ -92,8 +90,8 @@ import java.util.List; @Consumes(MediaType.APPLICATION_JSON) public class DeviceManagementServiceImpl implements DeviceManagementService { - private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; + private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); @GET @Path("/{type}/{id}/status") @@ -499,15 +497,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("offset") int offset, @QueryParam("limit") int limit) { List applications; - //ApplicationList appList; ApplicationManagementProviderService amc; try { RequestValidationUtil.validateDeviceIdentifier(type, id); amc = DeviceMgtAPIUtils.getAppManagementService(); applications = amc.getApplicationListForDevice(new DeviceIdentifier(id, type)); - - //TODO: return app list + return Response.status(Response.Status.OK).entity(applications).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while fetching the apps of the '" + type + "' device, which carries " + "the id '" + id + "'"; @@ -515,7 +511,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } - return Response.status(Response.Status.OK).entity(applications).build(); } @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 37ee12f054..7c6b026930 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -50,13 +50,12 @@ import java.util.List; */ public class ApplicationManagerProviderServiceImpl implements ApplicationManagementProviderService { + private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; + private static final Log log = LogFactory.getLog(ApplicationManagerProviderServiceImpl.class); private DeviceDAO deviceDAO; private ApplicationDAO applicationDAO; private ApplicationMappingDAO applicationMappingDAO; - private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; - private static final Log log = LogFactory.getLog(ApplicationManagerProviderServiceImpl.class); - public ApplicationManagerProviderServiceImpl(AppManagementConfig appManagementConfig) { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); @@ -198,17 +197,17 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { + if (log.isDebugEnabled()) { + log.debug("Updating application list for device: " + deviceIdentifier.toString()); + } List installedAppList = getApplicationListForDevice(deviceIdentifier); try { Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier, false); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - if (log.isDebugEnabled()) { - log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId()); - } if (log.isDebugEnabled()) { - log.debug("num of apps installed:" + installedAppList.size()); + log.debug("Number of apps installed:" + installedAppList.size()); } List appsToAdd = new ArrayList<>(); List appIdsToRemove = new ArrayList<>(installedAppList.size()); @@ -227,13 +226,14 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem List applicationIds = new ArrayList<>(); for (Application application : applications) { - /* - Truncating the application version if length of the version is greater than maximum allowed length. - */ - if (application.getVersion().length() > - DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH) { + // Adding N/A if application doesn't have a version. Also truncating the application version, + // if length of the version is greater than maximum allowed length. + if (application.getVersion() == null) { + application.setVersion("N/A"); + } else if (application.getVersion().length() > + DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH) { application.setVersion(StringUtils.abbreviate(application.getVersion(), - DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH)); + DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH)); } if (!installedAppList.contains(application)) { installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), @@ -258,24 +258,34 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem if (log.isDebugEnabled()) { log.debug("num of remove app Ids:" + appIdsToRemove.size()); } - DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new ApplicationManagementException("Error occurred saving application list to the device", e); + String msg = "Error occurred saving application list of the device " + deviceIdentifier.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { - throw new ApplicationManagementException("Error occurred while initializing transaction", e); + String msg = "Error occurred while initializing transaction for saving application list to the device " + + deviceIdentifier.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } catch (DeviceManagementException e) { - throw new ApplicationManagementException("Error occurred obtaining the device object.", e); + String msg = "Error occurred obtaining the device object for device " + deviceIdentifier.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (Exception e) { + String msg = "Exception occurred saving application list of the device " + deviceIdentifier.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } } @Override - public List getApplicationListForDevice( - DeviceIdentifier deviceId) throws ApplicationManagementException { - Device device = null; + public List getApplicationListForDevice(DeviceIdentifier deviceId) + throws ApplicationManagementException { + Device device; try { device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false); @@ -288,16 +298,24 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem log.debug("No device is found upon the device identifier '" + deviceId.getId() + "' and type '" + deviceId.getType() + "'. Therefore returning null"); } - return null; + return new ArrayList<>(); } try { DeviceManagementDAOFactory.openConnection(); return applicationDAO.getInstalledApplications(device.getId()); } catch (DeviceManagementDAOException e) { - throw new ApplicationManagementException("Error occurred while fetching the Application List of '" + - deviceId.getType() + "' device carrying the identifier'" + deviceId.getId(), e); + String msg = "Error occurred while fetching the Application List of device " + deviceId.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source to get application " + + "list of the device " + deviceId.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (Exception e) { + String msg = "Exception occurred getting application list of the device " + deviceId.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); }