Add jax-rs endpoints to modify and remove devices.

Update clientside js to work with secured apis
revert-70aa11f8
charithag 9 years ago
parent 15a6e4b63b
commit f6281c8ef0

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.api;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -30,7 +31,9 @@ import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@ -231,4 +234,52 @@ public class Device {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}
/**
* Update device.
*
* @return update status.
*/
@PUT
@Path("type/{type}/id/{deviceId}")
public Response updateDevice(@PathParam("type") String deviceType, @PathParam("deviceId") String deviceId,
org.wso2.carbon.device.mgt.common.Device updatedDevice) {
try {
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(deviceId);
org.wso2.carbon.device.mgt.common.Device device = deviceManagementService.getDevice(deviceIdentifier);
device.setName(updatedDevice.getName());
device.setDescription(updatedDevice.getDescription());
Boolean response = deviceManagementService.modifyEnrollment(device);
return Response.status(Response.Status.OK).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the list of device types.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
/**
* disenroll device.
*
* @return disenrollment status.
*/
@DELETE
@Path("type/{type}/id/{deviceId}")
public Response disenrollDevice(@PathParam("type") String deviceType, @PathParam("deviceId") String deviceId) {
try {
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(deviceType);
deviceIdentifier.setId(deviceId);
Boolean response = deviceManagementService.disenrollDevice(deviceIdentifier);
return Response.status(Response.Status.OK).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the list of device types.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}

@ -24,7 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.jaxrs.api.common.MDMAPIException;
import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.util.MDMUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
@ -62,7 +62,7 @@ public class Policy {
policy.setPolicyName(policyWrapper.getPolicyName());
policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile()));
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers());
@ -82,7 +82,7 @@ public class Policy {
policy.setPolicyName(policyWrapper.getPolicyName());
policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile()));
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers());
@ -181,7 +181,7 @@ public class Policy {
policy.setId(policyId);
policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile()));
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers());

@ -24,7 +24,7 @@ import org.wso2.carbon.policy.mgt.common.Profile;
import java.util.ArrayList;
import java.util.List;
public class MDMUtil {
public class DeviceMgtUtil {
public static Profile convertProfile(org.wso2.carbon.device.mgt.jaxrs.beans.Profile mdmProfile) {
Profile profile = new Profile();

@ -206,6 +206,20 @@
<method>GET</method>
</Permission>
<Permission>
<name>Modify user device</name>
<path>/device-mgt/user/devices/update</path>
<url>/devices/type/*/id/*</url>
<method>PUT</method>
</Permission>
<Permission>
<name>Remove user device</name>
<path>/device-mgt/user/devices/remove</path>
<url>/devices/type/*/id/*</url>
<method>DELETE</method>
</Permission>
<!--<Permission>-->
<!--<name>Get device</name>-->
<!--<path>/device-mgt/devices/view</path>-->

@ -96,7 +96,7 @@ $(document).ready(function () {
console.log(message);
}, function(message){
console.log(message.content);
});
});
});
});
@ -592,21 +592,19 @@ function attachDeviceEvents() {
$("a.remove-device-link").click(function () {
var deviceId = $(this).data("deviceid");
var deviceType = $(this).data("devicetype");
var removeDeviceAPI = "/devicemgt/api/devices/" + deviceType + "/" + deviceId + "/remove";
var serviceURL = "/devicemgt_admin/devices/type/" + deviceType + "/id/" + deviceId;
$(modalPopupContent).html($('#remove-device-modal-content').html());
showPopup();
$("a#remove-device-yes-link").click(function () {
var postOperationRequest = $.ajax({
url: removeDeviceAPI,
method: "post"
});
postOperationRequest.done(function (data) {
invokerUtil.delete(serviceURL, function (message) {
$(modalPopupContent).html($('#remove-device-200-content').html());
window.location.reload(false);
});
postOperationRequest.fail(function (jqXHR, textStatus) {
setTimeout(function () {
hidePopup();
location.reload(false);
}, 2000);
}, function (message) {
displayDeviceErrors(jqXHR);
});
});
@ -614,7 +612,6 @@ function attachDeviceEvents() {
$("a#remove-device-cancel-link").click(function () {
hidePopup();
});
});
/**
@ -626,7 +623,7 @@ function attachDeviceEvents() {
var deviceId = $(this).data("deviceid");
var deviceType = $(this).data("devicetype");
var deviceName = $(this).data("devicename");
var editDeviceAPI = "/devicemgt/api/devices/" + deviceType + "/" + deviceId + "/update?name=";
var serviceURL = "/devicemgt_admin/devices/type/" + deviceType + "/id/" + deviceId;
$(modalPopupContent).html($('#edit-device-modal-content').html());
$('#edit-device-name').val(deviceName);
@ -634,18 +631,13 @@ function attachDeviceEvents() {
$("a#edit-device-yes-link").click(function () {
var newDeviceName = $('#edit-device-name').val();
var postOperationRequest = $.ajax({
url: editDeviceAPI + newDeviceName,
method: "post"
});
postOperationRequest.done(function (data) {
invokerUtil.put(serviceURL, {"name": newDeviceName}, function (message) {
$(modalPopupContent).html($('#edit-device-200-content').html());
setTimeout(function () {
hidePopup();
location.reload(false);
}, 2000);
});
postOperationRequest.fail(function (jqXHR, textStatus) {
}, function (message) {
displayDeviceErrors(jqXHR);
});
});

Loading…
Cancel
Save