From 81aa298740c5c401e524fbfce06ff9887684e337 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 1 Jun 2016 02:18:15 +0530 Subject: [PATCH] Fixed RoleManagementService API issues --- .../device/mgt/jaxrs/beans/PolicyList.java | 55 +++++++++++++++++++ .../device/mgt/jaxrs/beans/RoleList.java | 1 - .../impl/PolicyManagementServiceImpl.java | 13 ++++- .../impl/RoleManagementServiceImpl.java | 18 +++--- 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyList.java new file mode 100644 index 0000000000..21b4f12cbc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyList.java @@ -0,0 +1,55 @@ +/* + * 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.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +@ApiModel(value = "Policy List") +public class PolicyList extends BasePaginatedResult { + + private List policies; + + @ApiModelProperty(value = "List of policies returned") + @JsonProperty("policies") + public List getList() { + return policies; + } + + public void setList(List policies) { + this.policies = policies; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" roles: [").append(policies).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java index 4cb08fa36c..11cfaa3ddc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java @@ -43,7 +43,6 @@ public class RoleList extends BasePaginatedResult { public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{\n"); - sb.append(" count: ").append(getCount()).append(",\n"); sb.append(" next: ").append(getNext()).append(",\n"); sb.append(" previous: ").append(getPrevious()).append(",\n"); 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 f543b7b295..aeacd9046e 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 @@ -27,6 +27,7 @@ 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.PolicyList; 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.FilteringUtil; @@ -120,19 +121,27 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { @QueryParam("limit") int limit) { PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); List policies; + List filteredPolicies; + PolicyList targetPolicies = new PolicyList(); try { PolicyAdministratorPoint policyAdministratorPoint = policyManagementService.getPAP(); policies = policyAdministratorPoint.getPolicies(); if (policies == null || policies.size() == 0) { return Response.status(Response.Status.NOT_FOUND).entity("No policies found.").build(); } + targetPolicies.setCount(policies.size()); + filteredPolicies = FilteringUtil.getFilteredList(policies, offset, limit); + if (filteredPolicies.size() == 0) { + return Response.status(Response.Status.NOT_FOUND).entity("No policies found.").build(); + } + targetPolicies.setList(filteredPolicies); } catch (PolicyManagementException e) { String msg = "ErrorResponse occurred while retrieving all available policies"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity(FilteringUtil.getFilteredList(policies, offset, limit)) - .build(); + + return Response.status(Response.Status.OK).entity(targetPolicies).build(); } @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 58e08c1d8f..46d4888ef2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -58,17 +58,17 @@ public class RoleManagementServiceImpl implements RoleManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { List filteredRoles; - RoleList targetRoles; + RoleList targetRoles = new RoleList(); try { filteredRoles = getRolesFromUserStore(); - if (offset != -1 && limit != -1) { - filteredRoles = FilteringUtil.getFilteredList(filteredRoles, offset, limit); - } - if (filteredRoles == null) { + if (filteredRoles == null || filteredRoles.size() == 0) { return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); } - targetRoles = new RoleList(); targetRoles.setCount(filteredRoles.size()); + filteredRoles = FilteringUtil.getFilteredList(getRolesFromUserStore(), offset, limit); + if (filteredRoles.size() == 0) { + return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); + } targetRoles.setList(filteredRoles); } catch (UserStoreException e) { String msg = "Error occurred while retrieving roles from the underlying user stores"; @@ -219,8 +219,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Override public Response updateRole(@PathParam("roleName") String roleName, RoleWrapper roleWrapper) { if (roleWrapper == null) { - log.error("Request body is incorrect or empty"); - return Response.status(Response.Status.BAD_REQUEST).build(); + return Response.status(Response.Status.BAD_REQUEST).entity("Request body is incorrect or empty").build(); } String newRoleName = roleWrapper.getRoleName(); try { @@ -290,8 +289,7 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Override public Response updateUsersOfRole(@PathParam("roleName") String roleName, List users) { if (users == null || users.size() == 0) { - log.error("No users are found"); - return Response.status(Response.Status.BAD_REQUEST).build(); + return Response.status(Response.Status.BAD_REQUEST).entity("No users are found in the request").build(); } try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();