From 5b8b15f69b9e8c561a7cfea6e2ecc8b05dba1199 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 31 May 2016 19:12:58 +0530 Subject: [PATCH] fixed api issues in policy --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 63 +++------- .../service/api/PolicyManagementService.java | 1 + .../impl/PolicyManagementServiceImpl.java | 117 ++++++++++++------ .../impl/util/PolicyFilteringUtil.java | 41 ++++++ .../src/main/webapp/META-INF/permissions.xml | 2 +- 5 files changed, 145 insertions(+), 79 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/PolicyFilteringUtil.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java index b8558856fe..31420dae88 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java @@ -21,21 +21,20 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + import java.util.List; @ApiModel(value = "PolicyWrapper", description = "This class carries all information related to Policy " + "Wrappers") public class PolicyWrapper { - @ApiModelProperty(name = "id", value = "The policy ID", required = true) - private int id; - @ApiModelProperty(name = "profile", value = "Contains the details of the profile that is included in the" - + " policy", required = true) - private Profile profile; @ApiModelProperty(name = "policyName", value = "The name of the policy", required = true) private String policyName; + @ApiModelProperty(name = "description", value = "Gives a description on the policy", required = true) private String description; + @ApiModelProperty(name = "compliance", value = "Provides the non-compliance rules. WSO2 EMM provides the" + " following non-compliance rules:\n" + "Enforce - Forcefully enforce the policies on the devices\n" @@ -44,8 +43,7 @@ public class PolicyWrapper { + "violation unknown to the user and the administrator can take the necessary actions with regard" + " to the reported", required = true) private String compliance; - @ApiModelProperty(name = "roles", value = "The roles to whom the policy is applied on", required = true) - private List roles; + @ApiModelProperty(name = "ownershipType", value = "The policy ownership type. It can be any of the " + "following values:\n" + "ANY - The policy will be applied on the BYOD and COPE device types\n" @@ -53,26 +51,21 @@ public class PolicyWrapper { + "COPE (Corporate-Owned, Personally-Enabled) - The policy will only be applied on the COPE " + "device type", required = true) private String ownershipType; - @ApiModelProperty(name = "devices", value = "Lists out the devices the policy is enforced on", + + @ApiModelProperty(name = "profile", value = "Contains the details of the profile that is included in the" + + " policy", required = true) + private Profile profile; + + @ApiModelProperty(name = "roles", value = "The roles to whom the policy is applied on", required = true) + private List roles; + + @ApiModelProperty(name = "deviceIdentifiers", value = "Lists out the devices the policy is enforced on", required = true) - private List devices; + private List deviceIdentifiers; + @ApiModelProperty(name = "users", value = "Lists out the users on whose devices the policy is enforced", required = true) private List users; - @ApiModelProperty(name = "tenantId", value = "The ID of the tenant that created the policy", - required = true) - private int tenantId; - @ApiModelProperty(name = "profileId", value = "The ID of each profile that is in the selected policy", - required = true) - private int profileId; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } public Profile getProfile() { return profile; @@ -122,12 +115,12 @@ public class PolicyWrapper { this.ownershipType = ownershipType; } - public List getDevices() { - return devices; + public List getDeviceIdentifiers() { + return deviceIdentifiers; } - public void setDevices(List devices) { - this.devices = devices; + public void setDeviceIdentifier(List deviceIdentifier) { + this.deviceIdentifiers = deviceIdentifiers; } public List getUsers() { @@ -138,20 +131,4 @@ public class PolicyWrapper { this.users = users; } - public int getTenantId() { - return tenantId; - } - - public void setTenantId(int tenantId) { - this.tenantId = tenantId; - } - - public int getProfileId() { - return profileId; - } - - public void setProfileId(int profileId) { - this.profileId = profileId; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index 53ec37550c..b4daa172bc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -258,6 +258,7 @@ public interface PolicyManagementService { required = true) PolicyWrapper policy); @POST + @Path("/remove-policy") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index faf2b02b1e..32760e1189 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -23,11 +23,13 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.PolicyFilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.policy.mgt.common.Policy; @@ -51,49 +53,62 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @Override public Response addPolicy(PolicyWrapper policyWrapper) { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); - Policy policy = this.getPolicyFromWrapper(policyWrapper); + try { + Policy policy = this.getPolicyFromWrapper(policyWrapper); - List devices = policy.getDevices(); - if (devices != null && devices.size() == 1) { - DeviceAccessAuthorizationService deviceAccessAuthorizationService = - DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(devices.get(0).getDeviceIdentifier(), - devices.get(0).getType()); - PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - String username = threadLocalCarbonContext.getUsername(); - try { - if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { - return Response.status(Response.Status.UNAUTHORIZED).entity("Current logged in user is " + - "not authorized to add policies").build(); + List devices = policy.getDevices(); + if (devices != null && devices.size() == 1) { + DeviceAccessAuthorizationService deviceAccessAuthorizationService = + DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(devices.get(0).getDeviceIdentifier(), + devices.get(0).getType()); + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + String username = threadLocalCarbonContext.getUsername(); + try { + if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { + return Response.status(Response.Status.UNAUTHORIZED).entity("Current logged in user is " + + "not authorized to add policies").build(); + } + } catch (DeviceAccessAuthorizationException e) { + String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy"; + log.error(msg, e); + return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - } catch (DeviceAccessAuthorizationException e) { - String msg = "ErrorResponse occurred while checking if the current user is authorized to add a policy"; - log.error(msg, e); - return javax.ws.rs.core.Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - } - try { + PolicyAdministratorPoint pap = policyManagementService.getPAP(); pap.addPolicy(policy); - return Response.status(Response.Status.OK).entity("Policy has been added successfully").build(); + return Response.status(Response.Status.CREATED).entity("Policy has been added successfully").build(); } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while adding policy"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceManagementException e) { + String msg = "ErrorResponse occurred while retrieving device list."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } - private Policy getPolicyFromWrapper(PolicyWrapper policyWrapper) { - Policy policy = new org.wso2.carbon.policy.mgt.common.Policy(); + private Policy getPolicyFromWrapper(PolicyWrapper policyWrapper) throws DeviceManagementException { + Policy policy = new Policy(); policy.setPolicyName(policyWrapper.getPolicyName()); - policy.setProfileId(policyWrapper.getProfileId()); policy.setDescription(policyWrapper.getDescription()); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile())); policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setRoles(policyWrapper.getRoles()); policy.setUsers(policyWrapper.getUsers()); - policy.setTenantId(policyWrapper.getTenantId()); policy.setCompliance(policyWrapper.getCompliance()); + //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. + List devices = null; + List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); + if (deviceIdentifiers != null) { + for (DeviceIdentifier id : deviceIdentifiers) { + devices.add(DeviceMgtAPIUtils.getDeviceManagementService().getDevice(id)); + } + } + policy.setDevices(devices); + policy.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); return policy; } @@ -116,7 +131,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity(policies).build(); + return Response.status(Response.Status.OK).entity(PolicyFilteringUtil.getPolicies(policies, offset, limit)) + .build(); } @GET @@ -144,19 +160,29 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @Override public Response updatePolicy(@PathParam("id") int id, PolicyWrapper policyWrapper) { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); - Policy policy = this.getPolicyFromWrapper(policyWrapper); try { + Policy policy = this.getPolicyFromWrapper(policyWrapper); + policy.setId(id); PolicyAdministratorPoint pap = policyManagementService.getPAP(); + Policy exisitingPolicy = pap.getPolicy(id); + if (exisitingPolicy == null) { + return Response.status(Response.Status.NOT_FOUND).entity("Policy not found.").build(); + } pap.updatePolicy(policy); return Response.status(Response.Status.OK).entity("Policy has successfully been updated").build(); } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while updating the policy"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceManagementException e) { + String msg = "ErrorResponse occurred while retrieving the device list."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } @POST + @Path("/remove-policy") @Override public Response removePolicies(List policyIds) { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); @@ -164,8 +190,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { try { PolicyAdministratorPoint pap = policyManagementService.getPAP(); for (int i : policyIds) { - org.wso2.carbon.policy.mgt.common.Policy policy = pap.getPolicy(i); - if (!pap.deletePolicy(policy)) { + Policy policy = pap.getPolicy(i); + if (policy == null || !pap.deletePolicy(policy)) { policyDeleted = false; } } @@ -182,41 +208,62 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } - @POST + @PUT @Path("/activate-policy") @Override public Response activatePolicies(List policyIds) { + boolean isPolicyActivated = false; try { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyAdministratorPoint pap = policyManagementService.getPAP(); for (int i : policyIds) { - pap.activatePolicy(i); + Policy policy = pap.getPolicy(i); + if (policy != null) { + pap.activatePolicy(i); + isPolicyActivated = true; + } } } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while activating policies"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated").build(); + if (isPolicyActivated) { + return Response.status(Response.Status.OK).entity("Selected policies have been successfully activated") + .build(); + } else { + return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been activated") + .build(); + } } - @POST + @PUT @Path("/deactivate-policy") @Override public Response deactivatePolicies(List policyIds) { + boolean isPolicyDeActivated = false; try { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyAdministratorPoint pap = policyManagementService.getPAP(); for (int i : policyIds) { - pap.inactivatePolicy(i); + Policy policy = pap.getPolicy(i); + if (policy != null) { + pap.inactivatePolicy(i); + isPolicyDeActivated = true; + } } } catch (PolicyManagementException e) { String msg = "Exception in inactivating policies."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + - "deactivated").build(); + if (isPolicyDeActivated) { + return Response.status(Response.Status.OK).entity("Selected policies have been successfully " + + "deactivated").build(); + } else { + return Response.status(Response.Status.NOT_FOUND).entity("Selected policies have not been deactivated") + .build(); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/PolicyFilteringUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/PolicyFilteringUtil.java new file mode 100644 index 0000000000..d57266145b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/PolicyFilteringUtil.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.service.impl.util; + +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.Collections; +import java.util.List; + +/** + * This is used instead of filtering from cache. + * Todo : need to implement proper pagination support on retrieving policies. + */ +public class PolicyFilteringUtil { + + /** + * This is used to filter from the cached policies. + */ + public static List getPolicies(List sourceList, int offset, int limit) { + if(sourceList == null || sourceList.size() < offset){ + return Collections.emptyList(); + } + return sourceList.subList(offset, Math.min(offset + limit, sourceList.size())); + } +} 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 a886c71925..0c9d193aef 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 @@ -736,7 +736,7 @@ Remove policy /device-mgt/admin/policies/remove - /policies/bulk-remove + /policies/remove-policy POST