From 3813856be8f01b2c2871828b8e946702245ccda0 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jun 2016 14:52:19 +0530 Subject: [PATCH 1/8] Removing DeviceWrapper and all its associated usage, as it appeared to be a redundant element in the precense of 'Device' --- .../common/device/details/DeviceWrapper.java | 86 ------------------- 1 file changed, 86 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceWrapper.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceWrapper.java deleted file mode 100644 index 0869b0866f..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceWrapper.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.common.device.details; - -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 org.wso2.carbon.device.mgt.common.app.mgt.Application; - -import java.util.List; - -@ApiModel(value = "DeviceWrapper", description = "This contains device details including, " + - "location and device meta information.") -public class DeviceWrapper { - - @ApiModelProperty(name = "device", value = "Device's basic information", required = true) - private Device device; - @ApiModelProperty(name = "deviceIdentifier", value = "Device identifier used to identify a device.", - required = true) - private DeviceIdentifier deviceIdentifier; - @ApiModelProperty(name = "deviceInfo", value = "Device's runtime information", required = true) - private DeviceInfo deviceInfo; - @ApiModelProperty(name = "deviceLocation", value = "Device's current location", required = true) - private DeviceLocation deviceLocation; - @ApiModelProperty(name = "applications", value = "Application list of devices", required = true) - private List applications; - - public Device getDevice() { - return device; - } - - public void setDevice(Device device) { - this.device = device; - } - - public DeviceIdentifier getDeviceIdentifier() { - return deviceIdentifier; - } - - public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) { - this.deviceIdentifier = deviceIdentifier; - } - - public DeviceInfo getDeviceInfo() { - return deviceInfo; - } - - public void setDeviceInfo(DeviceInfo deviceInfo) { - this.deviceInfo = deviceInfo; - } - - public DeviceLocation getDeviceLocation() { - return deviceLocation; - } - - public void setDeviceLocation(DeviceLocation deviceLocation) { - this.deviceLocation = deviceLocation; - } - - public List getApplications() { - return applications; - } - - public void setApplications(List applications) { - this.applications = applications; - } -} - From 9a7286a84bbb4e9425cea4e204a0e48a11b404d2 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jun 2016 14:52:59 +0530 Subject: [PATCH 2/8] Removing DeviceWrapper and all its associated usage, as it appeared to be a redundant element in the precense of 'Device' --- .../service/api/DeviceManagementService.java | 4 +- .../impl/DeviceManagementServiceImpl.java | 18 ++- .../device/mgt/core/search/mgt/Processor.java | 8 +- .../core/search/mgt/ResultSetAggregator.java | 4 +- .../core/search/mgt/SearchManagerService.java | 6 +- .../mgt/core/search/mgt/dao/SearchDAO.java | 7 +- .../search/mgt/dao/impl/SearchDAOImpl.java | 49 +++----- .../core/search/mgt/impl/ProcessorImpl.java | 107 ++++++++---------- .../mgt/impl/ResultSetAggregatorImpl.java | 38 ++++--- .../mgt/impl/SearchManagerServiceImpl.java | 8 +- .../mgt/core/search/mgt/impl/Utils.java | 39 +++---- .../device/mgt/core/Search/SearchDevice.java | 48 ++++---- 12 files changed, 157 insertions(+), 179 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 bd0933816a..7b5b61e2da 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 @@ -24,7 +24,6 @@ import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.app.mgt.Application; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; @@ -298,8 +297,7 @@ public interface DeviceManagementService { code = 200, message = "OK. \n Device list searched for has successfully been retrieved. Location header " + "contains URL of newly enrolled device", - response = DeviceWrapper.class, - responseContainer = "List", + response = DeviceList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", 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 9fcb5824a3..00b128e4b3 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 @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.search.SearchContext; @@ -77,6 +76,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); PaginationRequest request = new PaginationRequest(offset, limit); PaginationResult result; + DeviceList devices = new DeviceList(); if (type != null) { request.setDeviceType(type); @@ -129,13 +129,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { result = dms.getAllDevices(request); int resultCount = result.getRecordsTotal(); if (resultCount == 0) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" + - " enrolled with the server").build()); + Response.status(Response.Status.OK).entity(devices).build(); } } - DeviceList devices = new DeviceList(); devices.setList((List) result.getData()); devices.setCount(result.getRecordsTotal()); return Response.status(Response.Status.OK).entity(devices).build(); @@ -210,7 +207,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { public Response searchDevices(@QueryParam("offset") int offset, @QueryParam("limit") int limit, SearchContext searchContext) { SearchManagerService searchManagerService; - List devices; + List devices; + DeviceList deviceList = new DeviceList(); try { searchManagerService = DeviceMgtAPIUtils.getSearchManagerService(); devices = searchManagerService.search(searchContext); @@ -221,11 +219,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } if (devices == null || devices.size() == 0) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that no device is found upon " + - "the provided search filters").build()); + Response.status(Response.Status.OK).entity(deviceList); } - return Response.status(Response.Status.OK).entity(devices).build(); + + deviceList.setList(devices); + return Response.status(Response.Status.OK).entity(deviceList).build(); } @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java index 4c7058be02..30644c27b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java @@ -19,15 +19,15 @@ package org.wso2.carbon.device.mgt.core.search.mgt; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; -import org.wso2.carbon.device.mgt.common.search.Condition; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.search.SearchContext; import java.util.List; public interface Processor { - List execute(SearchContext searchContext) throws SearchMgtException; + List execute(SearchContext searchContext) throws SearchMgtException; + + List getUpdatedDevices(long epochTime) throws SearchMgtException; - List getUpdatedDevices(long epochTime) throws SearchMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ResultSetAggregator.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ResultSetAggregator.java index 9a5b9ab3f7..3741b46679 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ResultSetAggregator.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ResultSetAggregator.java @@ -19,13 +19,13 @@ package org.wso2.carbon.device.mgt.core.search.mgt; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import java.util.List; import java.util.Map; public interface ResultSetAggregator { - List aggregate(Map> deviceWrappers); + List aggregate(Map> devices); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java index 554ecfc0f9..ff86e47545 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java @@ -19,15 +19,15 @@ package org.wso2.carbon.device.mgt.core.search.mgt; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.search.SearchContext; import java.util.List; public interface SearchManagerService { - List search(SearchContext searchContext) throws SearchMgtException; + List search(SearchContext searchContext) throws SearchMgtException; - List getUpdated(long epochTime) throws SearchMgtException; + List getUpdated(long epochTime) throws SearchMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/SearchDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/SearchDAO.java index 04379dae6d..bd17d7c26e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/SearchDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/SearchDAO.java @@ -19,13 +19,14 @@ package org.wso2.carbon.device.mgt.core.search.mgt.dao; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import java.util.List; public interface SearchDAO { - List searchDeviceDetailsTable(String query) throws SearchDAOException; + List searchDeviceDetailsTable(String query) throws SearchDAOException; + + List searchDevicePropertyTable(String query) throws SearchDAOException; - List searchDevicePropertyTable(String query) throws SearchDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java index 89725a135d..fc162c59b9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java @@ -25,7 +25,6 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.search.mgt.dao.SearchDAO; @@ -38,22 +37,19 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - public class SearchDAOImpl implements SearchDAO { private static final Log log = LogFactory.getLog(SearchDAOImpl.class); @Override - public List searchDeviceDetailsTable(String query) throws SearchDAOException { - + public List searchDeviceDetailsTable(String query) throws SearchDAOException { if (log.isDebugEnabled()) { log.debug("Query : " + query); } - Connection conn; PreparedStatement stmt = null; ResultSet rs; - List devices = new ArrayList<>(); + List devices = new ArrayList<>(); Map devs = new HashMap<>(); try { conn = this.getConnection(); @@ -101,15 +97,11 @@ public class SearchDAOImpl implements SearchDAO { deviceLocation.setDeviceId(rs.getInt("ID")); deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); - DeviceWrapper wrapper = new DeviceWrapper(); - wrapper.setDevice(device); - wrapper.setDeviceInfo(deviceInfo); - wrapper.setDeviceLocation(deviceLocation); - wrapper.setDeviceIdentifier(identifier); - devices.add(wrapper); + deviceInfo.setLocation(deviceLocation); + device.setDeviceInfo(deviceInfo); + devices.add(device); devs.put(device.getId(), device.getId()); } - } } catch (SQLException e) { throw new SearchDAOException("Error occurred while acquiring the device details.", e); @@ -126,8 +118,7 @@ public class SearchDAOImpl implements SearchDAO { } @Override - public List searchDevicePropertyTable(String query) throws SearchDAOException { - + public List searchDevicePropertyTable(String query) throws SearchDAOException { if (log.isDebugEnabled()) { log.debug("Query : " + query); } @@ -135,7 +126,7 @@ public class SearchDAOImpl implements SearchDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs; - List devices = new ArrayList<>(); + List devices = new ArrayList<>(); Map devs = new HashMap<>(); try { conn = this.getConnection(); @@ -183,13 +174,9 @@ public class SearchDAOImpl implements SearchDAO { deviceLocation.setDeviceId(rs.getInt("ID")); deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); - DeviceWrapper wrapper = new DeviceWrapper(); - wrapper.setDevice(device); - wrapper.setDeviceInfo(deviceInfo); - wrapper.setDeviceLocation(deviceLocation); - wrapper.setDeviceIdentifier(identifier); - - devices.add(wrapper); + deviceInfo.setLocation(deviceLocation); + device.setDeviceInfo(deviceInfo); + devices.add(device); devs.put(device.getId(), device.getId()); } @@ -214,8 +201,7 @@ public class SearchDAOImpl implements SearchDAO { return DeviceManagementDAOFactory.getConnection(); } - private List fillPropertiesOfDevices(List devices) throws SearchDAOException { - + private List fillPropertiesOfDevices(List devices) throws SearchDAOException { if (devices.isEmpty()) { return null; } @@ -249,14 +235,13 @@ public class SearchDAOImpl implements SearchDAO { return devices; } - private DeviceInfo getDeviceInfo(List devices, int deviceId) { - - for (DeviceWrapper dw : devices) { - if (dw.getDevice().getId() == deviceId) { - if (dw.getDeviceInfo() == null) { - dw.setDeviceInfo(new DeviceInfo()); + private DeviceInfo getDeviceInfo(List devices, int deviceId) { + for (Device device : devices) { + if (device.getId() == deviceId) { + if (device.getDeviceInfo() == null) { + device.setDeviceInfo(new DeviceInfo()); } - return dw.getDeviceInfo(); + return device.getDeviceInfo(); } } return null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index 70f5b87feb..2673c3f263 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -19,10 +19,7 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -48,13 +45,13 @@ public class ProcessorImpl implements Processor { } @Override - public List execute(SearchContext searchContext) throws SearchMgtException { + public List execute(SearchContext searchContext) throws SearchMgtException { QueryBuilder queryBuilder = new QueryBuilderImpl(); - List generalDevices = new ArrayList<>(); - List> allANDDevices = new ArrayList<>(); - List> allORDevices = new ArrayList<>(); - List locationDevices = new ArrayList<>(); + List generalDevices = new ArrayList<>(); + List> allANDDevices = new ArrayList<>(); + List> allORDevices = new ArrayList<>(); + List locationDevices = new ArrayList<>(); try { Map> queries = queryBuilder.buildQueries(searchContext.getConditions()); DeviceManagementDAOFactory.openConnection(); @@ -64,13 +61,13 @@ public class ProcessorImpl implements Processor { } if (queries.containsKey(Constants.PROP_AND)) { for (String query : queries.get(Constants.PROP_AND)) { - List andDevices = searchDAO.searchDevicePropertyTable(query); + List andDevices = searchDAO.searchDevicePropertyTable(query); allANDDevices.add(andDevices); } } if (queries.containsKey(Constants.PROP_OR)) { for (String query : queries.get(Constants.PROP_OR)) { - List orDevices = searchDAO.searchDevicePropertyTable(query); + List orDevices = searchDAO.searchDevicePropertyTable(query); allORDevices.add(orDevices); } } @@ -88,23 +85,22 @@ public class ProcessorImpl implements Processor { DeviceManagementDAOFactory.closeConnection(); } - ResultSetAggregator aggregator = new ResultSetAggregatorImpl(); - Map> deviceWrappers = new HashMap<>(); + Map> devices = new HashMap<>(); - deviceWrappers.put(Constants.GENERAL, generalDevices); - deviceWrappers.put(Constants.PROP_AND, this.processANDSearch(allANDDevices)); - deviceWrappers.put(Constants.PROP_OR, this.processORSearch(allORDevices)); - deviceWrappers.put(Constants.LOCATION, locationDevices); + devices.put(Constants.GENERAL, generalDevices); + devices.put(Constants.PROP_AND, this.processANDSearch(allANDDevices)); + devices.put(Constants.PROP_OR, this.processORSearch(allORDevices)); + devices.put(Constants.LOCATION, locationDevices); - List finalDeviceWrappers = aggregator.aggregate(deviceWrappers); - this.setApplicationListOfDevices(finalDeviceWrappers); - return finalDeviceWrappers; + List finalDevices = aggregator.aggregate(devices); + this.setApplicationListOfDevices(finalDevices); + return finalDevices; } @Override - public List getUpdatedDevices(long epochTime) throws SearchMgtException { + public List getUpdatedDevices(long epochTime) throws SearchMgtException { if((1 + (int)Math.floor(Math.log10(epochTime))) <=10 ) { epochTime = epochTime * 1000; @@ -126,15 +122,14 @@ public class ProcessorImpl implements Processor { } - private List processANDSearch(List> deLists) { - - List devices = new ArrayList<>(); - List smallestDeviceList = this.findListWithLowestItems(deLists); - List> maps = this.convertDeviceListToMap(deLists); + private List processANDSearch(List> deLists) { + List deviceList = new ArrayList<>(); + List smallestDeviceList = this.findListWithLowestItems(deLists); + List> maps = this.convertDeviceListToMap(deLists); boolean valueExist = false; - for (DeviceWrapper dw : smallestDeviceList) { - for (Map deviceWrapperMap : maps) { - if (deviceWrapperMap.containsKey(dw.getDevice().getId())) { + for (Device device : smallestDeviceList) { + for (Map devices : maps) { + if (devices.containsKey(device.getId())) { valueExist = true; } else { valueExist = false; @@ -142,63 +137,61 @@ public class ProcessorImpl implements Processor { } } if (valueExist) { - devices.add(dw); + deviceList.add(device); } } - return devices; + return deviceList; } - private List processORSearch(List> deLists) { - List devices = new ArrayList<>(); - Map map = new HashMap<>(); + private List processORSearch(List> deLists) { + List devices = new ArrayList<>(); + Map map = new HashMap<>(); - for (List list : deLists) { - for (DeviceWrapper dw : list) { - if (!map.containsKey(dw.getDevice().getId())) { - map.put(dw.getDevice().getId(), dw); - devices.add(dw); + for (List list : deLists) { + for (Device device : list) { + if (!map.containsKey(device.getId())) { + map.put(device.getId(), device); + devices.add(device); } } } return devices; } - private List findListWithLowestItems(List> deLists) { - + private List findListWithLowestItems(List> deLists) { int size = 0; - List deviceWrappers = new ArrayList<>(); - for (List list : deLists) { + List devices = new ArrayList<>(); + for (List list : deLists) { if (size == 0) { size = list.size(); - deviceWrappers = list; - continue; + devices = list; } else { if (list.size() < size) { - deviceWrappers = list; + devices = list; } } } - return deviceWrappers; + return devices; } - private List> convertDeviceListToMap(List> deLists) { - - List> maps = new ArrayList<>(); - for (List deviceWrapperList : deLists) { - Map deviceWrapperMap = new HashMap<>(); + private List> convertDeviceListToMap(List> deLists) { + List> maps = new ArrayList<>(); + for (List devices : deLists) { + Map deviceMap = new HashMap<>(); - for (DeviceWrapper dw : deviceWrapperList) { - deviceWrapperMap.put(dw.getDevice().getId(), dw); + for (Device device: devices) { + deviceMap.put(device.getId(), device); } - maps.add(deviceWrapperMap); + maps.add(deviceMap); } return maps; } - private void setApplicationListOfDevices(List deviceWrappers) throws SearchMgtException { + + private void setApplicationListOfDevices(List devices) throws SearchMgtException { try { DeviceManagementDAOFactory.openConnection(); - for (DeviceWrapper wrapper : deviceWrappers) { - wrapper.setApplications(applicationDAO.getInstalledApplications(wrapper.getDevice().getId())); + for (Device device : devices) { + device.setApplications(applicationDAO.getInstalledApplications(device.getId())); } } catch (DeviceManagementDAOException e) { throw new SearchMgtException("Error occurred while fetching the Application List of devices ", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ResultSetAggregatorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ResultSetAggregatorImpl.java index dc33400b6e..acc565a9af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ResultSetAggregatorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ResultSetAggregatorImpl.java @@ -19,23 +19,26 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.core.search.mgt.Constants; import org.wso2.carbon.device.mgt.core.search.mgt.ResultSetAggregator; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ResultSetAggregatorImpl implements ResultSetAggregator { @Override - public List aggregate(Map> deviceWrappers) { + public List aggregate(Map> devices) { - Map generalQueryMap = this.convertToMap(deviceWrappers.get(Constants.GENERAL)); - Map andMap = this.convertToMap(deviceWrappers.get(Constants.PROP_AND)); - Map orMap = this.convertToMap(deviceWrappers.get(Constants.PROP_OR)); - Map locationMap = this.convertToMap(deviceWrappers.get(Constants.LOCATION)); - Map finalMap = new HashMap<>(); - List finalResult = new ArrayList<>(); + Map generalQueryMap = this.convertToMap(devices.get(Constants.GENERAL)); + Map andMap = this.convertToMap(devices.get(Constants.PROP_AND)); + Map orMap = this.convertToMap(devices.get(Constants.PROP_OR)); + Map locationMap = this.convertToMap(devices.get(Constants.LOCATION)); + Map finalMap = new HashMap<>(); + List finalResult = new ArrayList<>(); if (andMap.isEmpty()) { finalMap = generalQueryMap; @@ -70,24 +73,23 @@ public class ResultSetAggregatorImpl implements ResultSetAggregator { return finalResult; } - private Map convertToMap(List deviceWrappers) { - - if (deviceWrappers == null) { + private Map convertToMap(List devices) { + if (devices == null) { return null; } - Map deviceWrapperMap = new HashMap<>(); - for (DeviceWrapper dw : deviceWrappers) { - deviceWrapperMap.put(dw.getDevice().getId(), dw); + Map deviceWrapperMap = new HashMap<>(); + for (Device device : devices) { + deviceWrapperMap.put(device.getId(), device); } return deviceWrapperMap; } - private List convertDeviceMapToList(Map map) { - List list = new ArrayList<>(); - + private List convertDeviceMapToList(Map map) { + List list = new ArrayList<>(); for (Integer a : map.keySet()) { list.add(map.get(a)); } return list; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java index cc9a51d919..faecb93e9c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java @@ -19,7 +19,7 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.search.mgt.Processor; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; @@ -29,7 +29,6 @@ import java.util.List; public class SearchManagerServiceImpl implements SearchManagerService { - private Processor processor; public SearchManagerServiceImpl() { @@ -37,13 +36,14 @@ public class SearchManagerServiceImpl implements SearchManagerService { } @Override - public List search(SearchContext searchContext) throws SearchMgtException { + public List search(SearchContext searchContext) throws SearchMgtException { return processor.execute(searchContext); } @Override - public List getUpdated(long epochTime) throws SearchMgtException { + public List getUpdated(long epochTime) throws SearchMgtException { return processor.getUpdatedDevices(epochTime); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java index 3522228511..17990ff6fb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java @@ -19,7 +19,7 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import java.util.ArrayList; import java.util.HashMap; @@ -65,22 +65,23 @@ public class Utils { boolean bool = false; switch (column) { - case "deviceModel": + case "deviceModel": bool = true; break; - case "vendor": + case "vendor": bool = true; break; - case "osVersion": + case "osVersion": bool = true; break; - case "connectionType": + case "connectionType": bool = true; break; - case "ssid": + case "ssid": bool = true; break; - default: bool =false; + default: + bool = false; break; } @@ -89,9 +90,9 @@ public class Utils { public static String getConvertedValue(String column, String value) { - if(checkColumnType(column)){ - return "\'" + value + "\'"; - } else return value; + if (checkColumnType(column)) { + return "\'" + value + "\'"; + } else return value; } @@ -130,28 +131,28 @@ public class Utils { return stList; } - public static Integer[] getArrayOfDeviceIds(List deviceWrappers) { - - Integer[] arr = new Integer[deviceWrappers.size()]; + public static Integer[] getArrayOfDeviceIds(List devices) { + Integer[] arr = new Integer[devices.size()]; int x = 0; - for (DeviceWrapper dw : deviceWrappers) { - arr[x] = dw.getDevice().getId(); + for (Device device : devices) { + arr[x] = device.getId(); x++; } return arr; } - public static String getDeviceIdsAsString(List deviceWrappers) { + public static String getDeviceIdsAsString(List devices) { String str = ""; - for (DeviceWrapper dw : deviceWrappers) { - str += dw.getDevice().getId() + ","; + for (Device device : devices) { + str += device.getId() + ","; } - if (deviceWrappers.isEmpty()) { + if (devices.isEmpty()) { return null; } return str.substring(0, str.length() - 1); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java index 93c15914ad..8d46eb2d58 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java @@ -24,7 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.common.device.details.DeviceWrapper; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.search.Condition; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -79,22 +79,22 @@ public class SearchDevice extends BaseDeviceManagementTest { context.setConditions(conditions); SearchManagerService service = new SearchManagerServiceImpl(); - List deviceWrappers = service.search(context); + List devices = service.search(context); Gson gson = new Gson(); - String bbbb = gson.toJson(deviceWrappers); + String bbbb = gson.toJson(devices); log.info(bbbb); - for (DeviceWrapper dw : deviceWrappers) { - log.debug(dw.getDevice().getDescription()); - log.debug(dw.getDevice().getDeviceIdentifier()); + for (Device device : devices) { + log.debug(device.getDescription()); + log.debug(device.getDeviceIdentifier()); } } @Test - public void doValidLocationSearch() throws Exception{ + public void doValidLocationSearch() throws Exception { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -109,21 +109,21 @@ public class SearchDevice extends BaseDeviceManagementTest { context.setConditions(conditions); SearchManagerService service = new SearchManagerServiceImpl(); - List deviceWrappers = service.search(context); + List devices = service.search(context); Gson gson = new Gson(); - String bbbb = gson.toJson(deviceWrappers); + String bbbb = gson.toJson(devices); log.info("Valid Search " + bbbb); - for (DeviceWrapper dw : deviceWrappers) { - log.debug(dw.getDevice().getDescription()); - log.debug(dw.getDevice().getDeviceIdentifier()); + for (Device device : devices) { + log.debug(device.getDescription()); + log.debug(device.getDeviceIdentifier()); } } @Test - public void doInvalidLocationSearch() throws Exception{ + public void doInvalidLocationSearch() throws Exception { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -138,21 +138,21 @@ public class SearchDevice extends BaseDeviceManagementTest { context.setConditions(conditions); SearchManagerService service = new SearchManagerServiceImpl(); - List deviceWrappers = service.search(context); + List devices = service.search(context); Gson gson = new Gson(); - String bbbb = gson.toJson(deviceWrappers); + String bbbb = gson.toJson(devices); log.info("Invalid Search " + bbbb); - for (DeviceWrapper dw : deviceWrappers) { - log.debug(dw.getDevice().getDescription()); - log.debug(dw.getDevice().getDeviceIdentifier()); + for (Device device : devices) { + log.debug(device.getDescription()); + log.debug(device.getDeviceIdentifier()); } } @Test - public void doStringSearch() throws Exception{ + public void doStringSearch() throws Exception { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); @@ -167,16 +167,16 @@ public class SearchDevice extends BaseDeviceManagementTest { context.setConditions(conditions); SearchManagerService service = new SearchManagerServiceImpl(); - List deviceWrappers = service.search(context); + List devices = service.search(context); Gson gson = new Gson(); - String bbbb = gson.toJson(deviceWrappers); + String bbbb = gson.toJson(devices); log.info("Invalid Search " + bbbb); - for (DeviceWrapper dw : deviceWrappers) { - log.debug(dw.getDevice().getDescription()); - log.debug(dw.getDevice().getDeviceIdentifier()); + for (Device device : devices) { + log.debug(device.getDescription()); + log.debug(device.getDeviceIdentifier()); } } } From f1eca17991f400b2f97954c5a75a96e4ff9ebfa3 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jun 2016 14:57:12 +0530 Subject: [PATCH 3/8] Code cleanup --- .../device/mgt/core/search/mgt/impl/Utils.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java index 17990ff6fb..4c73b33cf7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java @@ -32,7 +32,6 @@ public class Utils { private static Map locationColumnsMap = new HashMap<>(); static { - genericColumnsMap.put("deviceModel", "DEVICE_MODEL"); genericColumnsMap.put("vendor", "VENDOR"); genericColumnsMap.put("osVersion", "OS_VERSION"); @@ -105,23 +104,11 @@ public class Utils { } public static boolean checkDeviceDetailsColumns(String str) { - if (genericColumnsMap.containsKey(str)) { - return true; - } - if (genericColumnsMap.containsValue(str)) { - return true; - } - return false; + return genericColumnsMap.containsKey(str) || genericColumnsMap.containsValue(str); } public static boolean checkDeviceLocationColumns(String str) { - if (locationColumnsMap.containsKey(str)) { - return true; - } - if (locationColumnsMap.containsValue(str)) { - return true; - } - return false; + return locationColumnsMap.containsKey(str) || locationColumnsMap.containsValue(str); } public static List convertStringToList(String str) { From 0c54753b3447a4392d558140bb8362e67984956d Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 22 Jun 2016 15:31:55 +0530 Subject: [PATCH 4/8] Cleaning up UserManagementService API implementation --- .../device/mgt/jaxrs/beans/BasicUserInfo.java | 68 +++++++++++++++++++ .../mgt/jaxrs/beans/BasicUserInfoList.java | 57 ++++++++++++++++ .../beans/{UserWrapper.java => UserInfo.java} | 58 ++-------------- .../{UserList.java => UserInfoList.java} | 9 ++- .../service/api/UserManagementService.java | 25 ++----- .../impl/UserManagementServiceImpl.java | 36 ++++------ 6 files changed, 157 insertions(+), 96 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/{UserWrapper.java => UserInfo.java} (52%) rename components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/{UserList.java => UserInfoList.java} (89%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java new file mode 100644 index 0000000000..467fd8afb5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfo.java @@ -0,0 +1,68 @@ +/* + * 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 io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(value = "BasicUserInfo", description = "Basic user information and the roles of the user.") +public class BasicUserInfo { + + private String username; + + @ApiModelProperty(name = "firstname", value = "The first name of the user.", required = true ) + private String firstname; + @ApiModelProperty(name = "lastname", value = "The last name of the user.", required = true ) + private String lastname; + @ApiModelProperty(name = "emailAddress", value = "The email address of the user.", required = true ) + private String emailAddress; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java new file mode 100644 index 0000000000..c8e58383c0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasicUserInfoList.java @@ -0,0 +1,57 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +@ApiModel(value = "BasicUserInfoList", description = "This contains basic details of a set of users that matches " + + "a given criteria as a collection") +public class BasicUserInfoList extends BasePaginatedResult { + + private List users = new ArrayList<>(); + + @ApiModelProperty(value = "List of devices returned") + @JsonProperty("users") + public List getList() { + return users; + } + + public void setList(List users) { + this.users = users; + } + + @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(" users: [").append(users).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/UserWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java similarity index 52% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserWrapper.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java index 8009ae233e..4513c6f647 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfo.java @@ -21,60 +21,23 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -@ApiModel(value = "UserWrapper", description = "User details and the roles of the user.") -public class UserWrapper { - - private String username; - /* - Base64 encoded password - */ +@ApiModel(value = "UserInfo", description = "User details and the roles of the user.") +public class UserInfo extends BasicUserInfo { @ApiModelProperty(name = "password", value = "Base64 encoded password.", required = true ) private String password; - @ApiModelProperty(name = "firstname", value = "The first name of the user.", required = true ) - private String firstname; - @ApiModelProperty(name = "lastname", value = "The last name of the user.", required = true ) - private String lastname; - @ApiModelProperty(name = "emailAddress", value = "The email address of the user.", required = true ) - private String emailAddress; + @ApiModelProperty(name = "roles", value = "List of roles.", required = true ) private String[] roles; - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - - public String getEmailAddress() { - return emailAddress; + public String getPassword() { + return password; } - public void setEmailAddress(String emailAddress) { - this.emailAddress = emailAddress; + public void setPassword(String password) { + this.password = password; } - /* - Giving a clone of the array since arrays are mutable - */ public String[] getRoles() { String[] copiedRoles = roles; if (roles != null){ @@ -87,11 +50,4 @@ public class UserWrapper { this.roles = roles; } - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java similarity index 89% rename from components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java rename to components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java index a205f0a3ac..bc98be244e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserInfoList.java @@ -27,17 +27,17 @@ import java.util.List; @ApiModel(value = "List of users", description = "This contains a set of users that matches a given " + "criteria as a collection") -public class UserList extends BasePaginatedResult { +public class UserInfoList extends BasePaginatedResult { - private List users = new ArrayList<>(); + private List users = new ArrayList<>(); @ApiModelProperty(value = "List of devices returned") @JsonProperty("users") - public List getList() { + public List getList() { return users; } - public void setList(List users) { + public void setList(List users) { this.users = users; } @@ -54,5 +54,4 @@ public class UserList extends BasePaginatedResult { return sb.toString(); } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java index 89ac39b3eb..fbbeb51736 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java @@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.api; import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserList; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.*; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -95,7 +92,7 @@ public interface UserManagementService { @ApiParam( name = "user", value = "User related details.", - required = true) UserWrapper user); + required = true) UserInfo user); @GET @Path("/{username}") @@ -105,13 +102,13 @@ public interface UserManagementService { value = "Getting details of a user.", notes = "If you wish to get the details of a specific user that is registered with EMM," + " you can do so using the REST API.", - response = UserWrapper.class, + response = BasicUserInfo.class, tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the requested role.", - response = UserWrapper.class, + response = BasicUserInfo.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -209,7 +206,7 @@ public interface UserManagementService { @ApiParam( name = "userData", value = "User related details.", - required = true) UserWrapper userData); + required = true) UserInfo userData); @DELETE @Path("/{username}") @@ -247,15 +244,12 @@ public interface UserManagementService { value = "Get the role list of a user.", notes = "A user can be assigned to one or more role in EMM. Using this REST API you are " + "able to get the role/roles a user is assigned to.", - response = String.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the role list assigned to the user.", - response = String.class, - responseContainer = "List", + response = RoleList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -297,15 +291,12 @@ public interface UserManagementService { value = "Get user list", notes = "If you wish to get the details of all the users registered with EMM, you can do so " + "using the REST API", - response = UserList.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the requested role.", - response = UserList.class, - responseContainer = "List", + response = UserInfoList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -364,8 +355,6 @@ public interface UserManagementService { + "search for that user by giving a character or a few characters in the username. " + "You will be given a list of users having the user name with the exact order of the " + "characters you provided.", - response = String.class, - responseContainer = "List", tags = "User Management") @ApiResponses(value = { @ApiResponse( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index ee67791fdd..177290e9f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -18,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -26,12 +25,9 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.*; import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.*; -import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper; -import org.wso2.carbon.device.mgt.jaxrs.beans.UserList; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; @@ -55,7 +51,7 @@ public class UserManagementServiceImpl implements UserManagementService { @POST @Override - public Response addUser(UserWrapper userWrapper) { + public Response addUser(UserInfo userWrapper) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(userWrapper.getUsername())) { @@ -172,7 +168,7 @@ public class UserManagementServiceImpl implements UserManagementService { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(username)) { - UserWrapper user = new UserWrapper(); + BasicUserInfo user = new BasicUserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); @@ -203,7 +199,7 @@ public class UserManagementServiceImpl implements UserManagementService { @PUT @Path("/{username}") @Override - public Response updateUser(@PathParam("username") String username, UserWrapper userWrapper) { + public Response updateUser(@PathParam("username") String username, UserInfo userWrapper) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(userWrapper.getUsername())) { @@ -316,8 +312,9 @@ public class UserManagementServiceImpl implements UserManagementService { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (userStoreManager.isExistingUser(username)) { - return Response.status(Response.Status.OK).entity(Collections.singletonList( - getFilteredRoles(userStoreManager, username))).build(); + RoleList result = new RoleList(); + result.setList(getFilteredRoles(userStoreManager, username)); + return Response.status(Response.Status.OK).entity(result).build(); } else { // Outputting debug message upon trying to remove non-existing user if (log.isDebugEnabled()) { @@ -343,7 +340,7 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("Getting the list of users with all user-related information"); } - List userList, offsetList; + List userList, offsetList; String appliedFilter = ((filter == null) || filter.isEmpty() ? "*" : filter); int appliedLimit = (limit <= 0) ? -1 : (limit + offset); @@ -353,9 +350,9 @@ public class UserManagementServiceImpl implements UserManagementService { //As the listUsers function accepts limit only to accommodate offset we are passing offset + limit String[] users = userStoreManager.listUsers(appliedFilter, appliedLimit); userList = new ArrayList<>(users.length); - UserWrapper user; + BasicUserInfo user; for (String username : users) { - user = new UserWrapper(); + user = new BasicUserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); @@ -368,12 +365,7 @@ public class UserManagementServiceImpl implements UserManagementService { } else { offsetList = new ArrayList<>(); } - -// if (offsetList.size() <= 0) { -// return Response.status(Response.Status.NOT_FOUND).entity("No users available for retrieval").build(); -// } - - UserList result = new UserList(); + BasicUserInfoList result = new BasicUserInfoList(); result.setList(offsetList); result.setCount(offsetList.size()); @@ -394,14 +386,14 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("Getting the list of users with all user-related information using the filter : " + filter); } - List userList; + List userList; try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] users = userStoreManager.listUsers(filter + "*", -1); userList = new ArrayList<>(users.length); - UserWrapper user; + UserInfo user; for (String username : users) { - user = new UserWrapper(); + user = new UserInfo(); user.setUsername(username); user.setEmailAddress(getClaimValue(username, Constants.USER_CLAIM_EMAIL_ADDRESS)); user.setFirstname(getClaimValue(username, Constants.USER_CLAIM_FIRST_NAME)); From 235d690f1ea9a0199dadf95f5c92ba5be6e0bc0e Mon Sep 17 00:00:00 2001 From: madhawap Date: Fri, 24 Jun 2016 12:19:32 +0530 Subject: [PATCH 5/8] Implemented 'offset' and 'limit' parameters to the 'operation' api --- .../device/mgt/jaxrs/beans/OperationList.java | 52 +++++++++++++++++++ .../device/mgt/jaxrs/beans/PolicyList.java | 2 +- .../device/mgt/jaxrs/beans/UserList.java | 1 - .../impl/DeviceManagementServiceImpl.java | 15 ++++-- .../mgt/dao/impl/GenericOperationDAOImpl.java | 8 ++- 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/OperationList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/OperationList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/OperationList.java new file mode 100644 index 0000000000..0080c8db8c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/OperationList.java @@ -0,0 +1,52 @@ +/* + * 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.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; + +import java.util.List; + +public class OperationList extends BasePaginatedResult { + private List operations; + + @ApiModelProperty(value = "List of operations returned") + @JsonProperty("operations") + public List getList() { + return operations; + } + + public void setList(List operations) { + this.operations = operations; + } + + @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(" operations: [").append(operations).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/PolicyList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyList.java index 21b4f12cbc..b7f4436283 100644 --- 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 @@ -47,7 +47,7 @@ public class PolicyList extends BasePaginatedResult { 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(" policies: [").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/UserList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java index a205f0a3ac..ee06cde126 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/UserList.java @@ -45,7 +45,6 @@ public class UserList 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/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 00b128e4b3..31db04ba28 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 @@ -33,6 +33,7 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.beans.OperationList; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.NotFoundException; @@ -266,14 +267,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { - List operations; + OperationList operationsList = new OperationList(); + PaginationRequest request = new PaginationRequest(offset, limit); + PaginationResult result; DeviceManagementProviderService dms; try { RequestValidationUtil.validateDeviceIdentifier(type, id); dms = DeviceMgtAPIUtils.getDeviceManagementService(); - operations = dms.getOperations(new DeviceIdentifier(id, type)); - if (operations == null) { + result = dms.getOperations(new DeviceIdentifier(id, type),request); + int resultCount = result.getRecordsTotal(); + + if (resultCount == 0) { throw new NotFoundException( new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("It is likely that" + " no operation is found upon the provided type and id").build()); @@ -285,7 +290,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { throw new UnexpectedServerErrorException( new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).entity(operations).build(); + operationsList.setList((List) result.getData()); + operationsList.setCount(result.getRecordsTotal()); + return Response.status(Response.Status.OK).entity(operationsList).build(); } @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 6e06aad36c..4c60e4a6b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -834,7 +834,8 @@ public class GenericOperationDAOImpl implements OperationDAO { } @Override - public List getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException { + public List getOperationsForDevice(int enrolmentId) + throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation; @@ -844,9 +845,12 @@ public class GenericOperationDAOImpl implements OperationDAO { String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + "OPERATION_CODE, om.STATUS, om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + - "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC"; + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC" + + "LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); + /*stmt.setInt(2, request.getStartIndex()); + stmt.setInt(3, request.getRowCount());*/ rs = stmt.executeQuery(); while (rs.next()) { From 16edca2aeadf741b377d119f938ca9388ef98957 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 24 Jun 2016 13:57:48 +0530 Subject: [PATCH 6/8] fixing ios enrollment issue by fetching the identity certificate from the header. --- .../framework/authenticator/CertificateAuthenticator.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java index 30313d2499..0b53c87306 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java @@ -29,7 +29,7 @@ public class CertificateAuthenticator implements WebappAuthenticator { private static final String CERTIFICATE_AUTHENTICATOR = "CertificateAuth"; private static final String MUTUAL_AUTH_HEADER = "mutual-auth-header"; private static final String PROXY_MUTUAL_AUTH_HEADER = "proxy-mutual-auth-header"; - private static final String CERTIFICATE_VERIFICATION_HEADER = "certificate-verification-header"; + private static final String CERTIFICATE_VERIFICATION_HEADER = "Mdm-Signature"; private static final String CLIENT_CERTIFICATE_ATTRIBUTE = "javax.servlet.request.X509Certificate"; @Override @@ -55,7 +55,6 @@ public class CertificateAuthenticator implements WebappAuthenticator { authenticationInfo.setStatus(Status.CONTINUE); } - String certVerificationHeader = request.getContext().findParameter(CERTIFICATE_VERIFICATION_HEADER); try { // When there is a load balancer terminating mutual SSL, it should pass this header along and // as the value of this header, the client certificate subject dn should be passed. @@ -78,7 +77,7 @@ public class CertificateAuthenticator implements WebappAuthenticator { } } else if (request.getHeader(CERTIFICATE_VERIFICATION_HEADER) != null) { - String certHeader = request.getHeader(certVerificationHeader); + String certHeader = request.getHeader(CERTIFICATE_VERIFICATION_HEADER); if (certHeader != null && AuthenticatorFrameworkDataHolder.getInstance().getCertificateManagementService(). verifySignature(certHeader)) { From 50009c7ac580a43a5fb45647b009b79e3d972e01 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 24 Jun 2016 16:13:50 +0530 Subject: [PATCH 7/8] adding apply policy end point --- .../service/api/PolicyManagementService.java | 25 +++++++++++++++++++ .../impl/PolicyManagementServiceImpl.java | 17 +++++++++++++ .../src/main/webapp/META-INF/permissions.xml | 6 +++++ 3 files changed, 48 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 c1e268eb7f..84a4179a70 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 @@ -386,5 +386,30 @@ public interface PolicyManagementService { @ApiParam(name = "policyIds", value = "Policy ID list to be deactivated.", required = true) List policyIds); + @PUT + @Produces("application/json") + @Path("apply-changes") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Applying Changes on Policies.", + notes = "Policies in the active state will be applied to new device that register with WSO2 EMM based on" + + " the policy enforcement criteria . In a situation where you need to make changes to existing" + + " policies (removing, activating, deactivating and updating) or add new policies, the existing" + + " devices will not receive these changes immediately. Once all the required changes are made" + + " you need to apply the changes to push the policy changes to the existing devices.") + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "Changes have been successfully updated."), + @ApiResponse( + code = 500, + message = "ErrorResponse in deactivating policies.", + response = ErrorResponse.class) + }) + @Permission(scope = "policy-modify", permissions = {"/permission/admin/device-mgt/admin/policies/update"}) + Response applyChanges(); + } 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 e22b3996d1..2cc81ad7b8 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 @@ -301,4 +301,21 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + @Override + @PUT + @Produces("application/json") + @Path("apply-changes") + public Response applyChanges() { + try { + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + PolicyAdministratorPoint pap = policyManagementService.getPAP(); + pap.publishChanges(); + } catch (PolicyManagementException e) { + String msg = "Exception in applying changes."; + log.error(msg, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + } + return Response.status(Response.Status.OK).entity("Changes have been successfully updated.").build(); + } } 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 1e254bbce1..d7249c6fde 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 @@ -611,6 +611,12 @@ /policies/* Put + + Edit policy + /device-mgt/admin/policies/update + /policies/apply-changes + PUT + From 9c215c4ac9f4a6b73267c17912799cf0c3adbd99 Mon Sep 17 00:00:00 2001 From: madhawap Date: Mon, 27 Jun 2016 10:51:09 +0530 Subject: [PATCH 8/8] added /priorities end-point to PolicyManagement --- .../service/api/PolicyManagementService.java | 29 +++++++++++++++ .../impl/PolicyManagementServiceImpl.java | 35 +++++++++++++++++++ 2 files changed, 64 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 c1e268eb7f..922315c8a6 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 @@ -23,6 +23,7 @@ import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -387,4 +388,32 @@ public interface PolicyManagementService { required = true) List policyIds); + @PUT + @Path("/priorities") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Prioritizing policies.", + notes = "", + tags = "Device Policy Management" + ) + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "Policy Priorities successfully updated."), + @ApiResponse( + code = 400, + message = "Policy priorities did not update. Bad Request.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Exception in updating policy priorities.", + response = ErrorResponse.class) + }) + @Permission(scope = "", permissions = {}) + Response updatePolicyPriorities(@ApiParam(name = "priorityUpdatedPolicies", value = "", + required = true) List priorityUpdatedPolicies); + + } 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 e22b3996d1..9063b9231e 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 @@ -40,10 +40,12 @@ import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.ArrayList; import java.util.List; @Path("/policies") @@ -301,4 +303,37 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + @PUT + @Path("/priorities") + public Response updatePolicyPriorities(List priorityUpdatedPolicies) { + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + List policiesToUpdate = new ArrayList<>(priorityUpdatedPolicies.size()); + int i; + for (i = 0; i < priorityUpdatedPolicies.size(); i++) { + Policy policyObj = new Policy(); + policyObj.setId(priorityUpdatedPolicies.get(i).getId()); + policyObj.setPriorityId(priorityUpdatedPolicies.get(i).getPriority()); + policiesToUpdate.add(policyObj); + } + boolean policiesUpdated; + try { + PolicyAdministratorPoint pap = policyManagementService.getPAP(); + policiesUpdated = pap.updatePolicyPriorities(policiesToUpdate); + } catch (PolicyManagementException e) { + String error = "Exception in updating policy priorities."; + log.error(error, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(error).build()); + } + if (policiesUpdated) { + return Response.status(Response.Status.OK).entity("Policy Priorities successfully " + + "updated.").build(); + + } else { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did " + + "not update. Bad Request.").build()); + } + } + }