From ce557266ecca90ed3b27e9e6b4ae5f6d092f1db8 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 1 Jun 2016 00:10:30 +0530 Subject: [PATCH 01/10] Fixed issues in RoleManagementService --- .../service/impl/RoleManagementServiceImpl.java | 14 +++++++++++++- .../src/main/webapp/META-INF/permissions.xml | 16 ++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) 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 2007821939..94df0875c9 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 @@ -170,6 +170,10 @@ public class RoleManagementServiceImpl implements RoleManagementService { @POST @Override public Response addRole(RoleWrapper roleWrapper) { + if (roleWrapper == null) { + log.error("Request body is incorrect or empty"); + return Response.status(Response.Status.BAD_REQUEST).build(); + } try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (log.isDebugEnabled()) { @@ -197,6 +201,10 @@ public class RoleManagementServiceImpl implements RoleManagementService { @Path("/{roleName}") @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(); + } String newRoleName = roleWrapper.getRoleName(); try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); @@ -256,10 +264,14 @@ public class RoleManagementServiceImpl implements RoleManagementService { return Response.status(Response.Status.OK).build(); } - @POST + @PUT @Path("/{roleName}/users") @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(); + } try { final UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (log.isDebugEnabled()) { 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..dbf4de5cc4 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 @@ -460,7 +460,7 @@ List roles /device-mgt/admin/roles/list - /roles/permissions + /roles/*/permissions GET @@ -481,22 +481,14 @@ Update role /device-mgt/admin/roles/update - /roles + /roles/* PUT - - - - - - - - Update role /device-mgt/admin/roles/update - /roles/users + /roles/*/users PUT @@ -510,7 +502,7 @@ Remove role /device-mgt/admin/roles/remove - /roles + /roles/* DELETE From 7aa37ff4d5a118e3f9ade737405002828b0697ad Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 1 Jun 2016 00:31:48 +0530 Subject: [PATCH 02/10] Fixed issues in RoleManagementService --- .../jaxrs/service/impl/PolicyManagementServiceImpl.java | 4 ++-- .../mgt/jaxrs/service/impl/RoleManagementServiceImpl.java | 4 ++++ .../util/{PolicyFilteringUtil.java => FilteringUtil.java} | 7 +++---- 3 files changed, 9 insertions(+), 6 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/{PolicyFilteringUtil.java => FilteringUtil.java} (81%) 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 32760e1189..f543b7b295 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 @@ -29,7 +29,7 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization 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.service.impl.util.FilteringUtil; 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; @@ -131,7 +131,7 @@ 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(PolicyFilteringUtil.getPolicies(policies, offset, limit)) + return Response.status(Response.Status.OK).entity(FilteringUtil.getFilteredList(policies, offset, limit)) .build(); } 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 c0c90e38b5..58e08c1d8f 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 @@ -25,6 +25,7 @@ import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.service.api.RoleManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; @@ -60,6 +61,9 @@ public class RoleManagementServiceImpl implements RoleManagementService { RoleList targetRoles; try { filteredRoles = getRolesFromUserStore(); + if (offset != -1 && limit != -1) { + filteredRoles = FilteringUtil.getFilteredList(filteredRoles, offset, limit); + } if (filteredRoles == null) { return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").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/FilteringUtil.java similarity index 81% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/PolicyFilteringUtil.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/FilteringUtil.java index d57266145b..3cc650029c 100644 --- 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/FilteringUtil.java @@ -18,21 +18,20 @@ */ 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 { +public class FilteringUtil { /** * This is used to filter from the cached policies. */ - public static List getPolicies(List sourceList, int offset, int limit) { + public static List getFilteredList(List sourceList, int offset, int limit) { if(sourceList == null || sourceList.size() < offset){ return Collections.emptyList(); } From 81aa298740c5c401e524fbfce06ff9887684e337 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 1 Jun 2016 02:18:15 +0530 Subject: [PATCH 03/10] 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(); From 25e45dc9f1053c9d0ab2f350898ee33dc1200f58 Mon Sep 17 00:00:00 2001 From: dilanua Date: Wed, 1 Jun 2016 10:44:08 +0530 Subject: [PATCH 04/10] Fixing issues in GET /operations and /admin/devices APIs --- .../service/impl/DeviceManagementServiceImpl.java | 4 ++-- .../admin/DeviceManagementAdminServiceImpl.java | 14 +++++++++++--- .../src/main/webapp/META-INF/permissions.xml | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 75e886cb92..b9e181745a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -326,8 +326,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { dms = DeviceMgtAPIUtils.getDeviceManagementService(); operations = dms.getOperations(new DeviceIdentifier(id, type)); if (operations == null) { - Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + - "the provided type and id"); + return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + + "the provided type and id").build(); } } catch (OperationManagementException e) { String msg = "Error occurred while fetching the operations for the '" + type + "' device, which " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 54140ccada..6400e59821 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -25,9 +25,11 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnauthorizedAccessException; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.*; @@ -67,11 +69,17 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe return Response.status(Response.Status.NOT_FOUND).entity("No device, which carries the name '" + name + "', is currently enrolled in the system").build(); } - return Response.status(Response.Status.OK).entity(devices).build(); + + DeviceList deviceList = new DeviceList(); + deviceList.setCount(devices.size()); + deviceList.setList(devices); + + return Response.status(Response.Status.OK).entity(deviceList).build(); } catch (DeviceManagementException e) { - String msg = "Error occurred while fetching the devices that carry the name '" + name + "'"; + String msg = "Error occurred at server side while fetching device list."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } finally { PrivilegedCarbonContext.endTenantFlow(); } 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 45293bc4a1..5a9c3f5347 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 @@ -394,6 +394,20 @@ GET + + View device + /device-mgt/admin/devices/view + /devices/*/*/operations + GET + + + + View device + /device-mgt/user/devices/view + /devices/*/*/operations + GET + + From 07a4a65c7c7df7d9999f0b2eadc03080a8631fe3 Mon Sep 17 00:00:00 2001 From: dilanua Date: Wed, 1 Jun 2016 11:14:17 +0530 Subject: [PATCH 05/10] Fixing permission issues with GET /devices/*/*/features API --- .../src/main/webapp/META-INF/permissions.xml | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) 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 5a9c3f5347..8797ebc6cb 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 @@ -418,34 +418,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + List policies /device-mgt/admin/policies/list - /features/* + /devices/*/*/features GET View device /device-mgt/admin/devices/view - /features/* + /devices/*/*/features GET View device /device-mgt/user/devices/view - /features/* + /devices/*/*/features GET View device /device-mgt/user/devices/view - /features + /devices/*/*/features GET - + From d926bdaa3464ab45f999f7ac84ac4e8985c637ce Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 1 Jun 2016 12:20:18 +0530 Subject: [PATCH 06/10] removed unused permission from permission.xml --- .../src/main/webapp/META-INF/permissions.xml | 193 ++++++------------ 1 file changed, 67 insertions(+), 126 deletions(-) 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 8797ebc6cb..5034e47d1d 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 @@ -30,104 +30,7 @@ - - - Fetch Activity related details - /device-mgt/admin/activities/view - /activities/* - GET - - - Fetch Activity related details - /device-mgt/admin/activities/view - /activities - GET - - - - - List devices - /device-mgt/admin/devices/list - /devices - GET - - - - List device types - /device-mgt/admin/devices/list - /devices/types - GET - - - - List device types - /device-mgt/user/devices/list - /devices/types - GET - - - - Add policy - /device-mgt/admin/policies/add - /devices/types - GET - - - - Add User policy - /device-mgt/user/policies/add - /devices/types - GET - - - - Edit policy - /device-mgt/admin/policies/update - /devices/types - GET - - - - Edit User policy - /device-mgt/user/policies/update - /devices/types - GET - - - - View device - /device-mgt/admin/devices/view - /devices/view - GET - - - - View user device - /device-mgt/user/devices/view - /devices/view - 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 - - - - - - - - + Device Management /device-mgt @@ -239,51 +142,90 @@ / GET + - - - - - - - - - - + - View device - /device-mgt/admin/devices/view - /devices/user/*/* + Fetch Activity related details + /device-mgt/admin/activities/view + /activities/* GET - - Devices Count All + Fetch Activity related details + /device-mgt/admin/activities/view + /activities + GET + + + + + List devices /device-mgt/admin/devices/list - /devices/count + /devices GET - - Device Count - /device-mgt/user/devices/list - /devices/user/*/count + Retrieve device information + /device-mgt/admin/devices/list + /devices/*/*/info GET - - List devices + Get device /device-mgt/admin/devices/list - /devices/name/*/* + /devices/*/* GET - - List All Own Devices - /device-mgt/user/devices/list - /devices/user/* + Get device location + /device-mgt/admin/devices/list + /devices/*/*/location + GET + + + devices location + /device-mgt/admin/devices/list + /devices/locations + POST + + + Get devices feature + /device-mgt/admin/devices/list + /devices/*/*/features + GET + + + Search devices + /device-mgt/admin/devices/list + /devices/search-devices + POST + + + list device application + /device-mgt/admin/devices/list + /devices/*/*/applications GET + + list device operation + /device-mgt/admin/devices/list + /devices/*/*/operations + GET + + + list device effective-policy + /device-mgt/admin/devices/list + /devices/*/*/effective-policy + GET + + + list devices + /device-mgt/admin/devices/list + /devices/admin/devices + GET + + @@ -1033,5 +975,4 @@ GET - From aa6298b5e77d75cbcbbd850a4522d160163b9245 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 1 Jun 2016 12:28:10 +0530 Subject: [PATCH 07/10] updated device management admin api permission --- .../src/main/webapp/META-INF/permissions.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5034e47d1d..4328808d68 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 @@ -222,7 +222,7 @@ list devices /device-mgt/admin/devices/list - /devices/admin/devices + /admin/devices GET From 52c59c8e53dcc0022369fbe48c0a2945aeb6d25a Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 1 Jun 2016 12:39:38 +0530 Subject: [PATCH 08/10] added permission updates on certificates --- .../src/main/webapp/META-INF/permissions.xml | 2 +- .../src/main/webapp/META-INF/permissions.xml | 16 ++++------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/src/main/webapp/META-INF/permissions.xml index 8718c827e0..b38133cc1f 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -33,7 +33,7 @@ get certificate in the database /device-mgt/emm-admin/certificate/GetSignCSR - /certificates/signcsr + /certificates/scep/signcsr POST emm_admin diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/webapp/META-INF/permissions.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/webapp/META-INF/permissions.xml index 480483adb4..dcd6b92155 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/webapp/META-INF/permissions.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/webapp/META-INF/permissions.xml @@ -29,38 +29,30 @@ --> - - - get certificate in the database - /device-mgt/emm-admin/certificate/GetSignCSR - /certificates/sign-csr - POST - emm_admin - Save certificate /device-mgt/admin/certificate/Save - /certificates + /admin/certificates POST Get certificate /device-mgt/admin/certificate/Get - /certificates/* + /admin/certificates/* GET Get all certificates /device-mgt/admin/certificate/GetAll - /certificates + /admin/certificates GET Remove certificate /device-mgt/admin/certificate/Remove - /certificates/* + /admin/certificates/* DELETE From 0bd2a46690d6beb4968e9ba6b04bbc3f4286289e Mon Sep 17 00:00:00 2001 From: dilanua Date: Wed, 1 Jun 2016 15:58:45 +0530 Subject: [PATCH 09/10] Adding H2/MSSQL/ORACLE/POSTGRESQL support for GET /admin/devices API --- .../impl/DeviceManagementServiceImpl.java | 5 +- .../DeviceManagementAdminServiceImpl.java | 15 +++-- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 5 +- .../carbon/device/mgt/core/dao/DeviceDAO.java | 3 +- .../core/dao/impl/AbstractDeviceDAOImpl.java | 65 ------------------- .../dao/impl/device/GenericDeviceDAOImpl.java | 65 +++++++++++++++++++ .../dao/impl/device/OracleDeviceDAOImpl.java | 65 +++++++++++++++++++ .../impl/device/PostgreSQLDeviceDAOImpl.java | 65 +++++++++++++++++++ .../impl/device/SQLServerDeviceDAOImpl.java | 65 +++++++++++++++++++ 9 files changed, 276 insertions(+), 77 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b9e181745a..d7065e0878 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -272,10 +272,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { throw new UnexpectedServerErrorException( new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - if (devices == null) { + if (devices == null || devices.size() == 0) { return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " + - "the provided type and id").build(); + "the provided search filters").build(); } + return Response.status(Response.Status.OK).entity(devices).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 6400e59821..46f80d18bf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -56,30 +56,31 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe int currentTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if (MultitenantConstants.SUPER_TENANT_ID != currentTenantId) { throw new UnauthorizedAccessException( - new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage( - "Current logged in user is not authorized to perform this operation").build()); + new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage( + "Current logged in user is not authorized to perform this operation").build()); } PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain)); List devices = DeviceMgtAPIUtils.getDeviceManagementService(). - getDevicesByNameAndType(name, type, offset, limit); - if (devices == null) { + getDevicesByNameAndType(name, type, offset, limit); + if (devices == null || devices.size() == 0) { return Response.status(Response.Status.NOT_FOUND).entity("No device, which carries the name '" + - name + "', is currently enrolled in the system").build(); + name + "', is currently enrolled in the system").build(); } + // setting up paginated result DeviceList deviceList = new DeviceList(); - deviceList.setCount(devices.size()); deviceList.setList(devices); + deviceList.setCount(devices.size()); return Response.status(Response.Status.OK).entity(deviceList).build(); } catch (DeviceManagementException e) { String msg = "Error occurred at server side while fetching device list."; log.error(msg, e); throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 9f273b63b1..d9c998c622 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -252,12 +252,13 @@ public class DeviceMgtAPIUtils { RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); if (realmService == null) { - throw new IllegalStateException(""); + throw new IllegalStateException("Realm service has not been initialized."); } try { return realmService.getTenantManager().getTenantId(tenantDomain); } catch (UserStoreException e) { - throw new DeviceManagementException(""); + throw new DeviceManagementException("Error occured while trying to " + + "obtain tenant id of currently logged in user"); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index a65f40db65..ae7fd00de2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -259,7 +259,8 @@ public interface DeviceDAO { * @return returns list of devices. * @throws DeviceManagementDAOException */ - List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) throws DeviceManagementDAOException; + List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException; /** * This method is used to retrieve devices of a given device name as a paginated result. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 0bc4d83f64..96dce585c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -619,71 +619,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return deviceCount; } - /** - * Get the list of devices that matches with the given device name. - * - * @param deviceName Name of the device. - * @param tenantId Id of the current tenant - * @return device list - * @throws DeviceManagementDAOException - */ - @Override - public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) - throws DeviceManagementDAOException { - - String filteringString = ""; - if (deviceName != null && !deviceName.isEmpty()) { - filteringString = filteringString + " AND d.NAME LIKE ?"; - } - - if (type != null && !type.isEmpty()) { - filteringString = filteringString + " AND t.NAME = ?"; - } - - Connection conn; - PreparedStatement stmt = null; - List devices = new ArrayList<>(); - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + - "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + - ") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - - int i = 1; - - if (deviceName != null && !deviceName.isEmpty()) { - stmt.setString(++i, deviceName + "%"); - } - - if (type != null && !type.isEmpty()) { - stmt.setString(++i, type); - } - - stmt.setInt(++i, offset); - stmt.setInt(++i, limit); - - rs = stmt.executeQuery(); - - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " + - "'" + deviceName + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return devices; - } - @Override public int addEnrollment(Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index e009fd6343..d10b7e9d99 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -303,6 +303,71 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { return devices; } + /** + * Get the list of devices that matches with the given device name and (or) device type. + * + * @param deviceName Name of the device. + * @param tenantId Id of the current tenant + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException { + + String filteringString = ""; + if (deviceName != null && !deviceName.isEmpty()) { + filteringString = filteringString + " AND d.NAME LIKE ?"; + } + + if (type != null && !type.isEmpty()) { + filteringString = filteringString + " AND t.NAME = ?"; + } + + Connection conn; + PreparedStatement stmt = null; + List devices = new ArrayList<>(); + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + + int i = 1; + + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(++i, deviceName + "%"); + } + + if (type != null && !type.isEmpty()) { + stmt.setString(++i, type); + } + + stmt.setInt(++i, offset); + stmt.setInt(++i, limit); + + rs = stmt.executeQuery(); + + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index bc8756710c..7d2f874dd5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -310,6 +310,71 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { return devices; } + /** + * Get the list of devices that matches with the given device name and (or) device type. + * + * @param deviceName Name of the device. + * @param tenantId Id of the current tenant + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException { + + String filteringString = ""; + if (deviceName != null && !deviceName.isEmpty()) { + filteringString = filteringString + " AND d.NAME LIKE ?"; + } + + if (type != null && !type.isEmpty()) { + filteringString = filteringString + " AND t.NAME = ?"; + } + + Connection conn; + PreparedStatement stmt = null; + List devices = new ArrayList<>(); + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, " + + "d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT d.ID, d.NAME, d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " + + "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID) rs) WHERE offset >= ? AND ROWNUM <= ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + + int i = 1; + + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(++i, deviceName + "%"); + } + + if (type != null && !type.isEmpty()) { + stmt.setString(++i, type); + } + + stmt.setInt(++i, offset); + stmt.setInt(++i, limit); + + rs = stmt.executeQuery(); + + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index 00ed10661b..878e310742 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -303,6 +303,71 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { return devices; } + /** + * Get the list of devices that matches with the given device name and (or) device type. + * + * @param deviceName Name of the device. + * @param tenantId Id of the current tenant + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException { + + String filteringString = ""; + if (deviceName != null && !deviceName.isEmpty()) { + filteringString = filteringString + " AND d.NAME LIKE ?"; + } + + if (type != null && !type.isEmpty()) { + filteringString = filteringString + " AND t.NAME = ?"; + } + + Connection conn; + PreparedStatement stmt = null; + List devices = new ArrayList<>(); + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID OFFSET ? LIMIT ?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + + int i = 1; + + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(++i, deviceName + "%"); + } + + if (type != null && !type.isEmpty()) { + stmt.setString(++i, type); + } + + stmt.setInt(++i, offset); + stmt.setInt(++i, limit); + + rs = stmt.executeQuery(); + + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index d39bc7f52d..476b703ad8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -305,6 +305,71 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { return devices; } + /** + * Get the list of devices that matches with the given device name and (or) device type. + * + * @param deviceName Name of the device. + * @param tenantId Id of the current tenant + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException { + + String filteringString = ""; + if (deviceName != null && !deviceName.isEmpty()) { + filteringString = filteringString + " AND d.NAME LIKE ?"; + } + + if (type != null && !type.isEmpty()) { + filteringString = filteringString + " AND t.NAME = ?"; + } + + Connection conn; + PreparedStatement stmt = null; + List devices = new ArrayList<>(); + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + + int i = 1; + + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(++i, deviceName + "%"); + } + + if (type != null && !type.isEmpty()) { + stmt.setString(++i, type); + } + + stmt.setInt(++i, offset); + stmt.setInt(++i, limit); + + rs = stmt.executeQuery(); + + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } From e002ffb54ab08e6c4b0ff9081e5288c41ffa6783 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 2 Jun 2016 00:20:14 +0530 Subject: [PATCH 10/10] Re-enabling retrieval of device info --- .../service/api/DeviceManagementService.java | 125 +++++++++--------- .../impl/DeviceManagementServiceImpl.java | 45 ++++--- 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 2fad649ab4..9c88e11cd8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -201,69 +201,68 @@ public interface DeviceManagementService { required = false) @HeaderParam("If-Modified-Since") String ifModifiedSince); -// @POST -// @ApiOperation( -// consumes = MediaType.APPLICATION_JSON, -// produces = MediaType.APPLICATION_JSON, -// httpMethod = "POST", -// value = "Retrieve devices information from the supplied device identifies.", -// notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + -// "identifiers.", -// response = DeviceInfo.class, -// responseContainer = "List", -// tags = "Device Management") -// @ApiResponses( -// value = { -// @ApiResponse( -// code = 200, -// message = "OK. \n Information of the submitted list of devices is returned", -// response = DeviceInfo.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 has been modified the last time.\n" + -// "Used by caches, or in conditional requests.")}), -// @ApiResponse( -// code = 303, -// message = "See Other. \n Source can be retrieved from the URL specified at the Location header.", -// responseHeaders = { -// @ResponseHeader( -// name = "Content-Location", -// description = "The Source URL of the document.")}), -// @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."), -// @ApiResponse( -// code = 406, -// message = "Not Acceptable. \n The requested media type is not supported."), -// @ApiResponse( -// code = 500, -// message = "Internal Server ErrorResponse. \n " + -// "Server error occurred while retrieving information of the list of the devices submitted.") -// }) -// @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) -// Response getDevicesInfo( -// @ApiParam( -// name = "deviceIds", -// value = "List of device identifiers", -// required = true) List deviceIds, -// @ApiParam( -// name = "If-Modified-Since", -// value = "Timestamp of the last modified date", -// required = false) -// @HeaderParam("If-Modified-Since") String timestamp); + @POST + @Path("/get-info") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Retrieve devices information from the supplied device identifies.", + notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + + "identifiers.", + tags = "Device Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Information of the submitted list of devices is returned", + response = DeviceInfo.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 has been modified the last time.\n" + + "Used by caches, or in conditional requests.")}), + @ApiResponse( + code = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @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."), + @ApiResponse( + code = 406, + message = "Not Acceptable. \n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server ErrorResponse. \n " + + "Server error occurred while retrieving information of the list of the devices submitted.") + }) + @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) + Response getDevicesInfo( + @ApiParam( + name = "If-Modified-Since", + value = "Timestamp of the last modified date", + required = false) + @HeaderParam("If-Modified-Since") String timestamp, + @ApiParam( + name = "deviceIds", + value = "List of device identifiers", + required = true) List deviceIds); @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index d7065e0878..04eb20326c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -131,28 +131,29 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } return Response.status(Response.Status.OK).entity(deviceInfo).build(); } -// -// @POST -// @Override -// public Response getDevicesInfo( -// List deviceIds, -// @HeaderParam("If-Modified-Since") String timestamp) { -// DeviceInformationManager informationManager; -// List deviceInfo; -// try { -// informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); -// deviceInfo = informationManager.getDevicesInfo(deviceIds); -// if (deviceInfo == null) { -// return Response.status(Response.Status.NOT_FOUND).entity("No device information is available for the " + -// "device list submitted").build(); -// } -// } catch (DeviceDetailsMgtException e) { -// String msg = "Error occurred while getting the device information."; -// log.error(msg, e); -// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); -// } -// return Response.status(Response.Status.OK).entity(deviceInfo).build(); -// } + + @POST + @Path("/get-info") + @Override + public Response getDevicesInfo( + @HeaderParam("If-Modified-Since") String timestamp, + List deviceIds) { + DeviceInformationManager informationManager; + List deviceInfo; + try { + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceInfo = informationManager.getDevicesInfo(deviceIds); + if (deviceInfo == null) { + return Response.status(Response.Status.NOT_FOUND).entity("No device information is available for the " + + "device list submitted").build(); + } + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device information."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceInfo).build(); + } @GET @Path("/{type}/{id}")