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 8edc3bc98dd..4693f26435d 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 @@ -189,9 +189,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { Device device; try { RequestValidationUtil.validateDeviceIdentifier(type, id); - DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - device = dms.getDevice(new DeviceIdentifier(id, type)); + if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { + Date sinceDate; + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); + try { + sinceDate = format.parse(ifModifiedSince); + } catch (ParseException e) { + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " + + "string is provided in 'If-Modified-Since' header").build()).build(); + } + device = dms.getDevice(new DeviceIdentifier(id, type), sinceDate); + if (device == null) { + return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " + + "after the timestamp provided in 'If-Modified-Since' header").build(); + } + } else { + device = dms.getDevice(new DeviceIdentifier(id, type)); + } } catch (DeviceManagementException e) { String msg = "Error occurred while fetching the device information."; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index d6653fa892e..27d415f33ca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import java.util.Date; import java.util.HashMap; import java.util.List; @@ -124,6 +125,18 @@ public interface DeviceDAO { */ Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve a device of a given device-identifier and tenant-id. + * + * @param deviceIdentifier device id. + * @param ifModifiedSince last modified time. + * @param tenantId tenant id. + * @return returns the device object. + * @throws DeviceManagementDAOException + */ + Device getDevice(DeviceIdentifier deviceIdentifier, Date ifModifiedSince, int tenantId) throws + DeviceManagementDAOException; + /** * This method is used to retrieve a device of a given device-identifier, enrollment status and tenant-id. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 44062cffc7e..a7af240b3af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -128,7 +128,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - device = DeviceManagementDAOUtil.loadMatchingDevice(rs); + device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while listing devices for type " + @@ -139,6 +139,41 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return device; } + @Override + public Device getDevice(DeviceIdentifier deviceIdentifier, Date since, int tenantId) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + Device device = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " + + "WHERE t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " + + "AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" ; + stmt = conn.prepareStatement(sql); + int paramIdx = 1; + stmt.setString(paramIdx++, deviceIdentifier.getType()); + stmt.setString(paramIdx++, deviceIdentifier.getId()); + stmt.setInt(paramIdx++, tenantId); + stmt.setLong(paramIdx++, since.getTime()); + stmt.setInt(paramIdx, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + device = DeviceManagementDAOUtil.loadMatchingDevice(rs, true); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing device for type " + + "'" + deviceIdentifier.getType() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return device; + } + @Override public Device getDevice(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 39b56701090..cf4531c1721 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -160,14 +160,21 @@ public final class DeviceManagementDAOUtil { //This method will retrieve most appropriate device information when there are multiple device enrollments for //a single device. We'll give the highest priority to active devices. - public static Device loadMatchingDevice(ResultSet rs) throws SQLException { + public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException { Map deviceMap = new HashMap<>(); Device device = loadDevice(rs); + if (deviceInfoIncluded) { + device.setDeviceInfo(loadDeviceInfo(rs)); + } + if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { return device; } while (rs.next()) { device = loadDevice(rs); + if (deviceInfoIncluded) { + device.setDeviceInfo(loadDeviceInfo(rs)); + } if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { return device; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 22b7530cafd..06ae1c875f0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import java.util.Date; import java.util.List; /** @@ -205,6 +206,8 @@ public interface DeviceManagementProviderService { Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException; + Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; List getAvailableDeviceTypes() throws DeviceManagementException; 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 41b4aed9d92..1e308f508c7 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 @@ -807,6 +807,54 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return device; } + @Override + public Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException { + Device device; + try { + DeviceManagementDAOFactory.openConnection(); + device = deviceDAO.getDevice(deviceId, since, this.getTenantId()); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the type '" + deviceId.getType() + "' and id '" + + deviceId.getId() + "'"); + } + return null; + } + DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); + if (device.getDeviceInfo() != null) { + device.getDeviceInfo().setLocation(location); + } + + List applications = applicationDAO.getInstalledApplications(device.getId()); + device.setApplications(applications); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceDetailsMgtDAOException e) { + throw new DeviceManagementException("Error occurred while fetching advanced device information", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + // The changes made here to prevent unit tests getting failed. They failed because when running the unit + // tests there is no osgi services. So getDeviceManager() returns a null. + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + + "Therefore, not attempting method 'getDevice'"); + } + return device; + } + Device pluginSpecificInfo = deviceManager.getDevice(deviceId); + if (pluginSpecificInfo != null) { + device.setFeatures(pluginSpecificInfo.getFeatures()); + device.setProperties(pluginSpecificInfo.getProperties()); + } + return device; + } + @Override public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException { Device device;