From f6281c8ef083ff3c5f7cb7de5872d3653a762ae3 Mon Sep 17 00:00:00 2001 From: charithag Date: Tue, 26 Apr 2016 20:50:39 +0530 Subject: [PATCH] Add jax-rs endpoints to modify and remove devices. Update clientside js to work with secured apis --- .../carbon/device/mgt/jaxrs/api/Device.java | 53 ++++++++++++++++++- .../carbon/device/mgt/jaxrs/api/Policy.java | 8 +-- .../util/{MDMUtil.java => DeviceMgtUtil.java} | 2 +- .../src/main/webapp/META-INF/permissions.xml | 14 +++++ .../cdmf.page.devices/public/js/listing.js | 30 ++++------- 5 files changed, 82 insertions(+), 25 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/{MDMUtil.java => DeviceMgtUtil.java} (98%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java index 34545599121..f70ab278a5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java @@ -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(); } } -} \ No newline at end of file + + /** + * 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(); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Policy.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Policy.java index bcbe4511094..a0d789b00fc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Policy.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Policy.java @@ -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()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/MDMUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java similarity index 98% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/MDMUtil.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java index 1c9f1881480..c2f5c2b7017 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/MDMUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java @@ -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(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index 35f0f3fdbea..1768fa91608 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -206,6 +206,20 @@ GET + + Modify user device + /device-mgt/user/devices/update + /devices/type/*/id/* + PUT + + + + Remove user device + /device-mgt/user/devices/remove + /devices/type/*/id/* + DELETE + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js index 1bbae6f9eea..8fb5a9e958f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js @@ -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); }); });