From 340be6ed658fdfdfa2b95b9afa4ad2d7f131f10c Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Mon, 18 Apr 2022 08:08:02 +0530 Subject: [PATCH] Add new API to fetch UI policy list --- .../service/api/PolicyManagementService.java | 77 +++++++++++++++++++ .../impl/PolicyManagementServiceImpl.java | 31 ++++++++ .../mgt/common/PolicyAdministratorPoint.java | 2 + .../impl/PolicyAdministratorPointImpl.java | 5 ++ .../policy/mgt/core/mgt/PolicyManager.java | 2 + .../mgt/core/mgt/impl/PolicyManagerImpl.java | 29 +++++++ 6 files changed, 146 insertions(+) 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 a84d4e7f038..0aaa57a165c 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 @@ -860,4 +860,81 @@ public interface PolicyManagementService { @QueryParam("limit") int limit ); + + @GET + @Path("/list") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting list of Policies", + responseContainer = "List", + notes = "Retrieve the details of all the policies in WSO2 EMM.", + response = Policy.class, + tags = "Device Policy Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:policies:get-details") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched policies.", + response = Policy.class, + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client already has the latest version " + + "of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = ("Internal Server Error. \n Server error occurred while fetching the policies."), + response = ErrorResponse.class) + }) + Response getPolicyList( + @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, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items", + required = false, + defaultValue = "0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many policy details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") + int limit); } 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 fa93c46c60c..6a2dbe68a3c 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 @@ -498,4 +498,35 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } + @GET + @Path("/list") + @Override + public Response getPolicyList( + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + RequestValidationUtil.validatePaginationParameters(offset, limit); + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + List policies; + List filteredPolicies; + PolicyList targetPolicies = new PolicyList(); + try { + PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); + policies = policyAdministratorPoint.getPolicyList(); + targetPolicies.setCount(policies.size()); + if (offset == 0 && limit == 0) { + targetPolicies.setList(policies); + } else { + filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit); + targetPolicies.setList(filteredPolicies); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving all available policies"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(targetPolicies).build(); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index db5ab41c073..591761db5f1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -163,4 +163,6 @@ public interface PolicyAdministratorPoint { * @throws PolicyManagementException */ List getPolicies(String policyType) throws PolicyManagementException; + + List getPolicyList() throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 1d1fa11c7c9..42e34bf4907 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -332,4 +332,9 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { return policyManager.getPolicies(policyType); } } + + @Override + public List getPolicyList() throws PolicyManagementException { + return policyManager.getPolicyList(); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index a31fe3d50a5..b0b2c241d00 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -89,4 +89,6 @@ public interface PolicyManager { HashMap getAppliedPolicyIdsDeviceIds() throws PolicyManagementException; List getPolicies(String type) throws PolicyManagementException; + + List getPolicyList() throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 0db7f676e51..66a71a84263 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -1502,4 +1502,33 @@ public class PolicyManagerImpl implements PolicyManager { correctiveAction.setAssociatedGeneralPolicyId(null); //avoiding send in payload correctiveAction.setFeatureId(null); //avoiding send in payload } + + @Override + public List getPolicyList() throws PolicyManagementException { + + List policyList; + try { + PolicyManagementDAOFactory.openConnection(); + policyList = policyDAO.getAllPolicies(); + for (Policy policy : policyList) { + policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); + policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); + List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); + if (!deviceGroupWrappers.isEmpty()) { + deviceGroupWrappers = this.getDeviceGroupNames(deviceGroupWrappers); + } + policy.setDeviceGroups(deviceGroupWrappers); + } + Collections.sort(policyList); + } catch (PolicyManagerDAOException e) { + throw new PolicyManagementException("Error occurred while getting all the policies.", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } catch (GroupManagementException e) { + throw new PolicyManagementException("Error occurred while getting device groups.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); + } + return policyList; + } }