Harshan Liyanage 7 years ago
parent 6b4d11c1e4
commit 46761ed76d

@ -32,7 +32,6 @@ import io.swagger.annotations.Tag;
import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
@ -43,7 +42,6 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData;
import org.wso2.carbon.device.mgt.common.search.SearchContext;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationRequest;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
@ -61,7 +59,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* Device related REST-API. This can be used to manipulated device related details.
@ -380,7 +377,8 @@ public interface DeviceManagementService {
produces = MediaType.APPLICATION_JSON,
httpMethod = "GET",
value = "Getting Details of a Device",
notes = "Get the details of a device by specifying the device type and device identifier.",
notes = "Get the details of a device by specifying the device type and device identifier and optionally " +
"the owner.",
tags = "Device Management",
extensions = {
@Extension(properties = {
@ -432,22 +430,29 @@ public interface DeviceManagementService {
required = true)
@PathParam("type")
@Size(max = 45)
String type,
String type,
@ApiParam(
name = "id",
value = "The device identifier of the device you want ot get details.",
required = true)
@PathParam("id")
@Size(max = 45)
String id,
String id,
@ApiParam(
name = "If-Modified-Since",
value = "Checks if the requested variant was modified, since the specified date-time. \n" +
"Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z. \n" +
"Example: Mon, 05 Jan 2014 15:10:00 +0200",
name = "owner",
value = "The owner of the device you want ot get details.",
required = false)
@QueryParam("owner")
@Size(max = 100)
String owner,
@ApiParam(
name = "If-Modified-Since",
value = "Checks if the requested variant was modified, since the specified date-time. \n" +
"Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z. \n" +
"Example: Mon, 05 Jan 2014 15:10:00 +0200",
required = false)
@HeaderParam("If-Modified-Since")
String ifModifiedSince);
String ifModifiedSince);
@PUT
@Path("/{type}/{id}")

@ -354,6 +354,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
public Response getDevice(
@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id,
@QueryParam("owner") @Size(max = 100) String owner,
@HeaderParam("If-Modified-Since") String ifModifiedSince) {
Device device = null;
try {
@ -367,14 +368,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type);
// check whether the user is authorized
if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, authorizedUser)) {
String msg = "User '" + authorizedUser + "' is not authorized to retrieve the given device id '" + id;
String msg = "User '" + authorizedUser + "' is not authorized to retrieve the given device id '" + id + "'";
log.error(msg);
return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(msg).build()).build();
}
Date sinceDate = null;
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
Date sinceDate;
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try {
sinceDate = format.parse(ifModifiedSince);
@ -383,13 +384,47 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
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();
}
if (!StringUtils.isEmpty(owner)) {
if (authorizedUser.equalsIgnoreCase(owner) || deviceAccessAuthorizationService.isDeviceAdminUser()) {
if (sinceDate != null) {
device = dms.getDevice(new DeviceIdentifier(id, type), owner, sinceDate, true);
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), owner, true);
}
} else {
String msg = "User '" + authorizedUser + "' is not authorized to retrieve the given device id '" + id +
"' which belongs to user '" + owner + "'";
log.error(msg);
return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(msg).build()).build();
}
} else if (deviceAccessAuthorizationService.isDeviceAdminUser()) {
if (sinceDate != null) {
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));
}
} else {
device = dms.getDevice(new DeviceIdentifier(id, type));
owner = authorizedUser;
if (sinceDate != null) {
device = dms.getDevice(new DeviceIdentifier(id, type), owner, sinceDate, true);
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), owner, true);
}
}
} catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the device information.";

@ -126,7 +126,19 @@ 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.
* This method is used to retrieve a device of a given device-identifier and owner and tenant-id.
*
* @param deviceIdentifier device id.
* @param owner username of the owner.
* @param tenantId tenant id.
* @return returns the device object.
* @throws DeviceManagementDAOException
*/
Device getDevice(DeviceIdentifier deviceIdentifier, String owner, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given device-identifier and tenant-id which modified
* later than the ifModifiedSince param.
*
* @param deviceIdentifier device id.
* @param ifModifiedSince last modified time.
@ -137,6 +149,20 @@ public interface DeviceDAO {
Device getDevice(DeviceIdentifier deviceIdentifier, Date ifModifiedSince, int tenantId) throws
DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given device-identifier and owner and tenant-id which modified
* later than the ifModifiedSince param.
*
* @param deviceIdentifier device id.
* @param owner username of the owner.
* @param ifModifiedSince last modified time.
* @param tenantId tenant id.
* @return returns the device object.
* @throws DeviceManagementDAOException
*/
Device getDevice(DeviceIdentifier deviceIdentifier, String owner, Date ifModifiedSince, int tenantId) throws
DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given device-identifier, enrollment status and tenant-id.
*

@ -141,6 +141,40 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceIdentifier, String owner, 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 WHERE " +
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
"AND TENANT_ID = ? AND e.OWNER = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceIdentifier.getType());
stmt.setString(2, deviceIdentifier.getId());
stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
stmt.setString(5, owner);
rs = stmt.executeQuery();
if (rs.next()) {
device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
"'" + deviceIdentifier.getType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceIdentifier, Date since, int tenantId)
throws DeviceManagementDAOException {
@ -176,6 +210,41 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceIdentifier, String owner, 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 t.ID = d.DEVICE_TYPE_ID 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 = ? AND e.OWNER = ?" ;
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceIdentifier.getType());
stmt.setString(2, deviceIdentifier.getId());
stmt.setInt(3, tenantId);
stmt.setLong(4, since.getTime());
stmt.setInt(5, tenantId);
stmt.setString(6, owner);
rs = stmt.executeQuery();
if (rs.next()) {
device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false);
}
} 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 {

@ -184,6 +184,19 @@ public interface DeviceManagementProviderService {
*/
Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException;
/**
* Returns the device of specified id owned by user with given username.
*
* @param deviceId - Device Id
* @param owner - Username of the owner
* @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required
* along with the device data.
* @return Device returns null when device is not available.
* @throws DeviceManagementException
*/
Device getDevice(DeviceIdentifier deviceId, String owner, boolean requireDeviceInfo) throws DeviceManagementException;
/**
* Returns the device of specified id.
*
@ -206,6 +219,20 @@ public interface DeviceManagementProviderService {
*/
Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException;
/**
* Returns the device of specified id and owned by user with given username.
*
* @param deviceId - Device Id
* @param owner - Username of the owner
* @param since - Date value where the resource was last modified
* @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required
* along with the device data.
* @return Device returns null when device is not available.
* @throws DeviceManagementException
*/
Device getDevice(DeviceIdentifier deviceId, String owner, Date since, boolean requireDeviceInfo)
throws DeviceManagementException;
/**
* Returns the device of specified id with the given status.
*

@ -771,6 +771,53 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceId, String owner, boolean requireDeviceInfo)
throws DeviceManagementException {
if (deviceId == null) {
String msg = "Received null device identifier for method getDevice";
log.error(msg);
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get device by device id :" + deviceId.getId() + " of type '" + deviceId.getType() +
" and owner '" + owner + "' and requiredDeviceInfo: " + requireDeviceInfo);
}
int tenantId = this.getTenantId();
Device device = null;
try {
DeviceManagementDAOFactory.openConnection();
device = deviceDAO.getDevice(deviceId, owner, tenantId);
if (device == null) {
String msg = "No device is found upon the type '" + deviceId.getType() + "' and id '" +
deviceId.getId() + "' and owner '" + owner + "'";
if (log.isDebugEnabled()) {
log.debug(msg);
}
return null;
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while obtaining the device for '" + deviceId.getId() + "' and owner '"
+ owner + "'";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDevice: " + deviceId.getId() + " with owner: " + owner;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
if (requireDeviceInfo) {
device = this.getAllDeviceInfo(device);
}
return device;
}
@Override
public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException,
ConfigurationManagementException {
@ -983,6 +1030,51 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceId, String owner, Date since, boolean requireDeviceInfo)
throws DeviceManagementException {
if (deviceId == null || since == null) {
String msg = "Received incomplete data for getDevice";
log.error(msg);
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get device since '" + since.toString() + "' with identifier: " + deviceId.getId()
+ " and type '" + deviceId.getType() + "' and owner '" + owner + "'");
}
Device device;
try {
DeviceManagementDAOFactory.openConnection();
device = deviceDAO.getDevice(deviceId, owner, since, this.getTenantId());
if (device == null) {
if (log.isDebugEnabled()) {
log.debug("No device is found upon the type '" + deviceId.getType() + "' and id '" +
deviceId.getId() + "' and owner name '" + owner + "'");
}
return null;
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while obtaining the device for id '" + deviceId.getId() + "' and owner '" +
owner + "'";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDevice for device: " + deviceId.getId() + " and owner: " + owner;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
if (requireDeviceInfo) {
device = this.getAllDeviceInfo(device);
}
return device;
}
@Override
public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException {
return this.getDevice(deviceId, status, true);

@ -63,7 +63,7 @@ deviceModule = function () {
/*
@Updated
*/
publicMethods.viewDevice = function (deviceType, deviceId) {
publicMethods.viewDevice = function (deviceType, deviceId, owner) {
var carbonUser = session.get(constants["USER_SESSION_KEY"]);
if (!carbonUser) {
log.error("User object was not found in the session");
@ -113,6 +113,9 @@ deviceModule = function () {
try {
utility.startTenantFlow(carbonUser);
var url = devicemgtProps["httpsURL"] + "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId;
if (owner) {
url = url + "?owner=" + owner;
}
return serviceInvokers.XMLHttp.get(
url,
function (backendResponse) {

@ -413,7 +413,8 @@ function loadDevices(searchType, searchParam) {
$(row).attr('data-deviceid', htmlspecialchars(data.deviceIdentifier));
$(row).attr('data-devicetype', htmlspecialchars(data.deviceType));
$(row).attr('data-url', context + '/device/' + htmlspecialchars(data.deviceType) + '?id=' + htmlspecialchars(data.deviceIdentifier));
$(row).attr('data-url', context + '/device/' + htmlspecialchars(data.deviceType) + '?id='
+ htmlspecialchars(data.deviceIdentifier) + '&owner=' + htmlspecialchars(data.userPattern)) ;
var model = htmlspecialchars(getPropertyValue(data.properties, 'DEVICE_MODEL'));
var vendor = htmlspecialchars(getPropertyValue(data.properties, 'VENDOR'));
var owner = htmlspecialchars(data.userPattern);

@ -23,6 +23,7 @@ function onRequest(context) {
var log = new Log("device-view.js");
var deviceType = context.uriParams.deviceType;
var deviceId = request.getParameter("id");
var owner = request.getParameter("owner");
var attributes = [];
var featureList = [];
var user = userModule.getCarbonUser();
@ -131,7 +132,7 @@ function onRequest(context) {
if (deviceType != null && deviceType != undefined && deviceId != null && deviceId != undefined) {
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
var device = deviceModule.viewDevice(deviceType, deviceId);
var device = deviceModule.viewDevice(deviceType, deviceId, owner);
if (device && device.status != "error") {
displayData.device = device.content;
displayData.autoCompleteParams = autoCompleteParams;

@ -20,10 +20,11 @@ function onRequest(context) {
var log = new Log("cdmf.unit.device.view/view.js");
var deviceType = context["uriParams"]["deviceType"];
var deviceId = request.getParameter("id");
var owner = request.getParameter("owner");
var deviceViewData = {};
if (deviceType && deviceId) {
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
var response = deviceModule.viewDevice(deviceType, deviceId);
var response = deviceModule.viewDevice(deviceType, deviceId, owner);
if (response["status"] == "success") {
deviceViewData["deviceFound"] = true;
deviceViewData["isAuthorized"] = true;

Loading…
Cancel
Save