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.httpclient.HttpStatus;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.DeviceMgtAPIUtils;
import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload; import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
@ -231,4 +234,52 @@ public class Device {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); 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.common.MDMAPIException;
import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; 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.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.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload; import org.wso2.carbon.device.mgt.jaxrs.api.util.ResponsePayload;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
@ -62,7 +62,7 @@ public class Policy {
policy.setPolicyName(policyWrapper.getPolicyName()); policy.setPolicyName(policyWrapper.getPolicyName());
policy.setProfileId(policyWrapper.getProfileId()); policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription()); policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile())); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles()); policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers()); policy.setUsers(policyWrapper.getUsers());
@ -82,7 +82,7 @@ public class Policy {
policy.setPolicyName(policyWrapper.getPolicyName()); policy.setPolicyName(policyWrapper.getPolicyName());
policy.setProfileId(policyWrapper.getProfileId()); policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription()); policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile())); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles()); policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers()); policy.setUsers(policyWrapper.getUsers());
@ -181,7 +181,7 @@ public class Policy {
policy.setId(policyId); policy.setId(policyId);
policy.setProfileId(policyWrapper.getProfileId()); policy.setProfileId(policyWrapper.getProfileId());
policy.setDescription(policyWrapper.getDescription()); policy.setDescription(policyWrapper.getDescription());
policy.setProfile(MDMUtil.convertProfile(policyWrapper.getProfile())); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setRoles(policyWrapper.getRoles()); policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers()); policy.setUsers(policyWrapper.getUsers());

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

@ -206,6 +206,20 @@
<method>GET</method> <method>GET</method>
</Permission> </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>--> <!--<Permission>-->
<!--<name>Get device</name>--> <!--<name>Get device</name>-->
<!--<path>/device-mgt/devices/view</path>--> <!--<path>/device-mgt/devices/view</path>-->

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

Loading…
Cancel
Save