From 47e6f6d29277aba546fddc0c5c90b6a885fa5a1d Mon Sep 17 00:00:00 2001 From: GPrathap Date: Wed, 25 Jan 2017 08:30:45 +0530 Subject: [PATCH] adding username as parameter --- .../dashboard/GadgetDataService.java | 28 ++++----- .../dao/AbstractGadgetDataServiceDAO.java | 43 +++++++------- .../dashboard/dao/GadgetDataServiceDAO.java | 28 ++++----- .../dashboard/impl/GadgetDataServiceImpl.java | 57 ++++++++++--------- .../mgt/analytics/dashboard/util/APIUtil.java | 50 ++++++++++++++++ .../mgt/jaxrs/service/impl/DashboardImpl.java | 50 ++++++++++------ 6 files changed, 162 insertions(+), 94 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/util/APIUtil.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java index c2b1b55a757..5a251fc9c29 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/GadgetDataService.java @@ -47,7 +47,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet) + DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException; /** @@ -64,7 +64,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet) + DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException; /** @@ -74,7 +74,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - DeviceCountByGroup getTotalDeviceCount() throws DataAccessLayerException; + DeviceCountByGroup getTotalDeviceCount(String userName) throws DataAccessLayerException; /** * This method is used to get device counts classified by connectivity statuses. @@ -83,7 +83,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException; + List getDeviceCountsByConnectivityStatuses(String userName) throws DataAccessLayerException; /** * This method is used to get device counts classified by potential vulnerabilities. @@ -92,7 +92,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException; + List getDeviceCountsByPotentialVulnerabilities(String userName) throws DataAccessLayerException; /** * This method is used to get non-compliant device counts classified by individual features. @@ -105,7 +105,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) throws + PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount, String userName) throws InvalidStartIndexValueException, InvalidResultCountValueException, DataAccessLayerException; /** @@ -122,7 +122,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet) + List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException; /** @@ -139,7 +139,7 @@ public interface GadgetDataService { */ @SuppressWarnings("unused") List getFeatureNonCompliantDeviceCountsByPlatforms(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException; /** @@ -156,7 +156,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet) + List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException; /** @@ -174,7 +174,7 @@ public interface GadgetDataService { */ @SuppressWarnings("unused") List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException; /** @@ -196,7 +196,7 @@ public interface GadgetDataService { * @throws InvalidResultCountValueException This can occur if resultCount value is lesser than its minimum (5). */ @SuppressWarnings("unused") - PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount) + PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException, InvalidStartIndexValueException, InvalidResultCountValueException; @@ -220,7 +220,7 @@ public interface GadgetDataService { */ @SuppressWarnings("unused") PaginationResult getFeatureNonCompliantDevicesWithDetails(String featureCode, BasicFilterSet basicFilterSet, - int startIndex, int resultCount) throws InvalidFeatureCodeValueException, + int startIndex, int resultCount, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException, InvalidStartIndexValueException, InvalidResultCountValueException; @@ -238,7 +238,7 @@ public interface GadgetDataService { * executing SQL query and retrieving data. */ @SuppressWarnings("unused") - List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet) + List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException; /** @@ -255,7 +255,7 @@ public interface GadgetDataService { */ @SuppressWarnings("unused") List getFeatureNonCompliantDevicesWithDetails(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java index 97815120645..80d0fbac51e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/AbstractGadgetDataServiceDAO.java @@ -36,17 +36,18 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import static org.wso2.carbon.device.mgt.analytics.dashboard.util.APIUtil.getAuthenticatedUser; + public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceDAO { @Override - public DeviceCountByGroup getTotalDeviceCount() throws SQLException { + public DeviceCountByGroup getTotalDeviceCount(String userName) throws SQLException { int totalDeviceCount; try { - totalDeviceCount = this.getFilteredDeviceCount(null); + totalDeviceCount = this.getFilteredDeviceCount(null, userName); } catch (InvalidPotentialVulnerabilityValueException e) { throw new AssertionError(e); } - DeviceCountByGroup deviceCountByGroup = new DeviceCountByGroup(); deviceCountByGroup.setGroup("total"); deviceCountByGroup.setDisplayNameForGroup("Total"); @@ -56,20 +57,17 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet) + public DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException { - - int filteredDeviceCount = this.getFilteredDeviceCount(extendedFilterSet); - + int filteredDeviceCount = this.getFilteredDeviceCount(extendedFilterSet, userName); DeviceCountByGroup deviceCountByGroup = new DeviceCountByGroup(); deviceCountByGroup.setGroup("filtered"); deviceCountByGroup.setDisplayNameForGroup("Filtered"); deviceCountByGroup.setDeviceCount(filteredDeviceCount); - return deviceCountByGroup; } - private int getFilteredDeviceCount(ExtendedFilterSet extendedFilterSet) + private int getFilteredDeviceCount(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException { Map filters = this.extractDatabaseFiltersFromBean(extendedFilterSet); @@ -118,7 +116,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException { + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException { if (featureCode == null || featureCode.isEmpty()) { throw new InvalidFeatureCodeValueException("Feature code should not be either null or empty."); @@ -176,7 +174,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDeviceCountsByConnectivityStatuses() throws SQLException { + public List getDeviceCountsByConnectivityStatuses(String userName) throws SQLException { Connection con; PreparedStatement stmt = null; ResultSet rs = null; @@ -208,7 +206,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDeviceCountsByPotentialVulnerabilities() throws SQLException { + public List getDeviceCountsByPotentialVulnerabilities(String userName) throws SQLException { // getting non-compliant device count DeviceCountByGroup nonCompliantDeviceCount = new DeviceCountByGroup(); nonCompliantDeviceCount.setGroup(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT); @@ -230,10 +228,10 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD private int getNonCompliantDeviceCount() throws SQLException { ExtendedFilterSet extendedFilterSet = new ExtendedFilterSet(); - extendedFilterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants. - PotentialVulnerability.NON_COMPLIANT); + extendedFilterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants.PotentialVulnerability.NON_COMPLIANT); try { - return this.getFilteredDeviceCount(extendedFilterSet); + String userName = getAuthenticatedUser(); + return this.getFilteredDeviceCount(extendedFilterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { throw new AssertionError(e); } @@ -244,14 +242,15 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD extendedFilterSet.setPotentialVulnerability(GadgetDataServiceDAOConstants. PotentialVulnerability.UNMONITORED); try { - return this.getFilteredDeviceCount(extendedFilterSet); + String userName = getAuthenticatedUser(); + return this.getFilteredDeviceCount(extendedFilterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { throw new AssertionError(e); } } @Override - public List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet) + public List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException { Map filters = this.extractDatabaseFiltersFromBean(extendedFilterSet); @@ -307,7 +306,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getFeatureNonCompliantDeviceCountsByPlatforms(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException { + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException { if (featureCode == null || featureCode.isEmpty()) { throw new InvalidFeatureCodeValueException("Feature code should not be either null or empty."); @@ -366,7 +365,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet) + public List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException { Map filters = this.extractDatabaseFiltersFromBean(extendedFilterSet); @@ -423,7 +422,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException { + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException { if (featureCode == null || featureCode.isEmpty()) { throw new InvalidFeatureCodeValueException("Feature code should not be either null or empty."); @@ -482,7 +481,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD } @Override - public List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet) + public List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException { Map filters = this.extractDatabaseFiltersFromBean(extendedFilterSet); @@ -539,7 +538,7 @@ public abstract class AbstractGadgetDataServiceDAO implements GadgetDataServiceD @Override public List getFeatureNonCompliantDevicesWithDetails(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException { + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException { if (featureCode == null || featureCode.isEmpty()) { throw new InvalidFeatureCodeValueException("Feature code should not be either null or empty."); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java index c5c1638fbab..0033f193273 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/GadgetDataServiceDAO.java @@ -30,45 +30,45 @@ import java.util.List; public interface GadgetDataServiceDAO { - DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet) + DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException; - DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet) + DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException; - DeviceCountByGroup getTotalDeviceCount() throws SQLException; + DeviceCountByGroup getTotalDeviceCount(String userName) throws SQLException; - List getDeviceCountsByConnectivityStatuses() throws SQLException; + List getDeviceCountsByConnectivityStatuses(String userName) throws SQLException; - List getDeviceCountsByPotentialVulnerabilities() throws SQLException; + List getDeviceCountsByPotentialVulnerabilities(String userName) throws SQLException; - PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) throws + PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount, String userName) throws InvalidStartIndexValueException, InvalidResultCountValueException, SQLException; - List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet) + List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException; List getFeatureNonCompliantDeviceCountsByPlatforms(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException; + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException; - List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet) + List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException; List getFeatureNonCompliantDeviceCountsByOwnershipTypes(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException; + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException; - PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount) + PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount, String userName) throws InvalidPotentialVulnerabilityValueException, InvalidStartIndexValueException, InvalidResultCountValueException, SQLException; PaginationResult getFeatureNonCompliantDevicesWithDetails(String featureCode, BasicFilterSet basicFilterSet, - int startIndex, int resultCount) throws InvalidFeatureCodeValueException, + int startIndex, int resultCount, String userName) throws InvalidFeatureCodeValueException, InvalidStartIndexValueException, InvalidResultCountValueException, SQLException; - List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet) + List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, SQLException; List getFeatureNonCompliantDevicesWithDetails(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, SQLException; + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, SQLException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java index 495172e3e02..9a392ab8df8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/impl/GadgetDataServiceImpl.java @@ -36,13 +36,14 @@ import java.util.List; public class GadgetDataServiceImpl implements GadgetDataService { @Override - public DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet) + public DeviceCountByGroup getDeviceCount(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException { DeviceCountByGroup filteredDeviceCount; try { + GadgetDataServiceDAOFactory.openConnection(); filteredDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getDeviceCount(extendedFilterSet); + getDeviceCount(extendedFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -53,13 +54,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet) + public DeviceCountByGroup getFeatureNonCompliantDeviceCount(String featureCode, BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException { DeviceCountByGroup featureNonCompliantDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCount = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(featureCode, basicFilterSet); + getGadgetDataServiceDAO().getFeatureNonCompliantDeviceCount(featureCode, basicFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -70,11 +71,11 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public DeviceCountByGroup getTotalDeviceCount() throws DataAccessLayerException { + public DeviceCountByGroup getTotalDeviceCount(String userName) throws DataAccessLayerException { DeviceCountByGroup totalDeviceCount; try { GadgetDataServiceDAOFactory.openConnection(); - totalDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getTotalDeviceCount(); + totalDeviceCount = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO().getTotalDeviceCount(userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -85,12 +86,12 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByConnectivityStatuses() throws DataAccessLayerException { + public List getDeviceCountsByConnectivityStatuses(String userName) throws DataAccessLayerException { List deviceCountsByConnectivityStatuses; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByConnectivityStatuses = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses(); + getGadgetDataServiceDAO().getDeviceCountsByConnectivityStatuses(userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -101,12 +102,12 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByPotentialVulnerabilities() throws DataAccessLayerException { + public List getDeviceCountsByPotentialVulnerabilities(String userName) throws DataAccessLayerException { List deviceCountsByPotentialVulnerabilities; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByPotentialVulnerabilities = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getDeviceCountsByPotentialVulnerabilities(); + getDeviceCountsByPotentialVulnerabilities(userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -117,14 +118,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount) + public PaginationResult getNonCompliantDeviceCountsByFeatures(int startIndex, int resultCount, String userName) throws InvalidStartIndexValueException, InvalidResultCountValueException, DataAccessLayerException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getNonCompliantDeviceCountsByFeatures(startIndex, resultCount); + getNonCompliantDeviceCountsByFeatures(startIndex, resultCount, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -135,13 +136,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet) + public List getDeviceCountsByPlatforms(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException { List deviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getDeviceCountsByPlatforms(extendedFilterSet); + getDeviceCountsByPlatforms(extendedFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -153,13 +154,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getFeatureNonCompliantDeviceCountsByPlatforms(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException { List featureNonCompliantDeviceCountsByPlatforms; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByPlatforms = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getFeatureNonCompliantDeviceCountsByPlatforms(featureCode, basicFilterSet); + getFeatureNonCompliantDeviceCountsByPlatforms(featureCode, basicFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -170,14 +171,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet) + public List getDeviceCountsByOwnershipTypes(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException { List deviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); deviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getDeviceCountsByOwnershipTypes(extendedFilterSet); + getDeviceCountsByOwnershipTypes(extendedFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -189,13 +190,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List - getFeatureNonCompliantDeviceCountsByOwnershipTypes(String featureCode, BasicFilterSet basicFilterSet) + getFeatureNonCompliantDeviceCountsByOwnershipTypes(String featureCode, BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException { List featureNonCompliantDeviceCountsByOwnershipTypes; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDeviceCountsByOwnershipTypes = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getFeatureNonCompliantDeviceCountsByOwnershipTypes(featureCode, basicFilterSet); + getFeatureNonCompliantDeviceCountsByOwnershipTypes(featureCode, basicFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -206,14 +207,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount) + public PaginationResult getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, int startIndex, int resultCount, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException, InvalidStartIndexValueException, InvalidResultCountValueException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getDevicesWithDetails(extendedFilterSet, startIndex, resultCount); + getDevicesWithDetails(extendedFilterSet, startIndex, resultCount, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -225,14 +226,14 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public PaginationResult getFeatureNonCompliantDevicesWithDetails(String featureCode, BasicFilterSet basicFilterSet, - int startIndex, int resultCount) throws InvalidFeatureCodeValueException, + int startIndex, int resultCount, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException, InvalidStartIndexValueException, InvalidResultCountValueException { PaginationResult paginationResult; try { GadgetDataServiceDAOFactory.openConnection(); paginationResult = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getFeatureNonCompliantDevicesWithDetails(featureCode, basicFilterSet, startIndex, resultCount); + getFeatureNonCompliantDevicesWithDetails(featureCode, basicFilterSet, startIndex, resultCount, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -243,13 +244,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { } @Override - public List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet) + public List getDevicesWithDetails(ExtendedFilterSet extendedFilterSet, String userName) throws InvalidPotentialVulnerabilityValueException, DataAccessLayerException { List devicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); devicesWithDetails = GadgetDataServiceDAOFactory. - getGadgetDataServiceDAO().getDevicesWithDetails(extendedFilterSet); + getGadgetDataServiceDAO().getDevicesWithDetails(extendedFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); @@ -261,13 +262,13 @@ public class GadgetDataServiceImpl implements GadgetDataService { @Override public List getFeatureNonCompliantDevicesWithDetails(String featureCode, - BasicFilterSet basicFilterSet) throws InvalidFeatureCodeValueException, + BasicFilterSet basicFilterSet, String userName) throws InvalidFeatureCodeValueException, DataAccessLayerException { List featureNonCompliantDevicesWithDetails; try { GadgetDataServiceDAOFactory.openConnection(); featureNonCompliantDevicesWithDetails = GadgetDataServiceDAOFactory.getGadgetDataServiceDAO(). - getFeatureNonCompliantDevicesWithDetails(featureCode, basicFilterSet); + getFeatureNonCompliantDevicesWithDetails(featureCode, basicFilterSet, userName); } catch (SQLException e) { throw new DataAccessLayerException("Error in either opening a database connection or " + "accessing the database to fetch corresponding results.", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/util/APIUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/util/APIUtil.java new file mode 100644 index 00000000000..8d304bb4e2f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/util/APIUtil.java @@ -0,0 +1,50 @@ +package org.wso2.carbon.device.mgt.analytics.dashboard.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; + +import java.net.SocketException; + + +/** + * This class provides utility functions used by REST-API. + */ +public class APIUtil { + + private static Log log = LogFactory.getLog(APIUtil.class); + + public static String getAuthenticatedUser() { + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + String username = threadLocalCarbonContext.getUsername(); + String tenantDomain = threadLocalCarbonContext.getTenantDomain(); + return username + "@" + tenantDomain; + } + + public static String getAuthenticatedUserTenantDomain() { + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + return threadLocalCarbonContext.getTenantDomain(); + } + + public static DeviceManagementProviderService getDeviceManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceManagementProviderService deviceManagementProviderService = + (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); + if (deviceManagementProviderService == null) { + String msg = "Device Management service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceManagementProviderService; + } + + public static String getServerUrl() { + try { + return org.apache.axis2.util.Utils.getIpAddress(); + } catch (SocketException e) { + log.warn("Failed retrieving the hostname, therefore set to localhost", e); + return "localhost"; + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DashboardImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DashboardImpl.java index ba5e00d0d81..c6940599ed1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DashboardImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DashboardImpl.java @@ -38,6 +38,8 @@ import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; +import static org.wso2.carbon.device.mgt.analytics.dashboard.util.APIUtil.getAuthenticatedUser; + /** * This class consists of dashboard related REST APIs * to be consumed by individual client gadgets such as @@ -82,7 +84,8 @@ public class DashboardImpl implements Dashboard { // getting total device count DeviceCountByGroup totalDeviceCount; try { - totalDeviceCount = gadgetDataService.getTotalDeviceCount(); + String userName = getAuthenticatedUser(); + totalDeviceCount = gadgetDataService.getTotalDeviceCount(userName); } catch (DataAccessLayerException e) { log.error("An internal error occurred while trying to execute relevant data service function " + "@ Dashboard API layer to retrieve total device count.", e); @@ -100,7 +103,8 @@ public class DashboardImpl implements Dashboard { // getting device counts by connectivity statuses List deviceCountsByConnectivityStatuses; try { - deviceCountsByConnectivityStatuses = gadgetDataService.getDeviceCountsByConnectivityStatuses(); + String userName = getAuthenticatedUser(); + deviceCountsByConnectivityStatuses = gadgetDataService.getDeviceCountsByConnectivityStatuses(userName); } catch (DataAccessLayerException e) { log.error("An internal error occurred while trying to execute relevant data service function " + "@ Dashboard API layer to retrieve device counts by connectivity statuses.", e); @@ -128,7 +132,8 @@ public class DashboardImpl implements Dashboard { List deviceCountsByPotentialVulnerabilities; try { - deviceCountsByPotentialVulnerabilities = gadgetDataService.getDeviceCountsByPotentialVulnerabilities(); + String userName = getAuthenticatedUser(); + deviceCountsByPotentialVulnerabilities = gadgetDataService.getDeviceCountsByPotentialVulnerabilities(userName); } catch (DataAccessLayerException e) { log.error("An internal error occurred while trying to execute relevant data service function " + "@ Dashboard API layer to retrieve device counts by potential vulnerabilities.", e); @@ -158,8 +163,9 @@ public class DashboardImpl implements Dashboard { PaginationResult paginationResult; try { + String userName = getAuthenticatedUser(); paginationResult = gadgetDataService. - getNonCompliantDeviceCountsByFeatures(startIndex, resultCount); + getNonCompliantDeviceCountsByFeatures(startIndex, resultCount, userName); } catch (InvalidStartIndexValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -210,7 +216,8 @@ public class DashboardImpl implements Dashboard { // creating device-Counts-by-platforms Data Wrapper List deviceCountsByPlatforms; try { - deviceCountsByPlatforms = gadgetDataService.getDeviceCountsByPlatforms(filterSet); + String userName = getAuthenticatedUser(); + deviceCountsByPlatforms = gadgetDataService.getDeviceCountsByPlatforms(filterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -232,7 +239,8 @@ public class DashboardImpl implements Dashboard { // creating device-Counts-by-ownership-types Data Wrapper List deviceCountsByOwnerships; try { - deviceCountsByOwnerships = gadgetDataService.getDeviceCountsByOwnershipTypes(filterSet); + String userName = getAuthenticatedUser(); + deviceCountsByOwnerships = gadgetDataService.getDeviceCountsByOwnershipTypes(filterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -274,8 +282,9 @@ public class DashboardImpl implements Dashboard { // creating feature-non-compliant-device-Counts-by-platforms Data Wrapper List featureNonCompliantDeviceCountsByPlatforms; try { + String userName = getAuthenticatedUser(); featureNonCompliantDeviceCountsByPlatforms = gadgetDataService. - getFeatureNonCompliantDeviceCountsByPlatforms(nonCompliantFeatureCode, filterSet); + getFeatureNonCompliantDeviceCountsByPlatforms(nonCompliantFeatureCode, filterSet, userName); } catch (InvalidFeatureCodeValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -299,8 +308,9 @@ public class DashboardImpl implements Dashboard { // creating feature-non-compliant-device-Counts-by-ownership-types Data Wrapper List featureNonCompliantDeviceCountsByOwnerships; try { + String userName = getAuthenticatedUser(); featureNonCompliantDeviceCountsByOwnerships = gadgetDataService. - getFeatureNonCompliantDeviceCountsByOwnershipTypes(nonCompliantFeatureCode, filterSet); + getFeatureNonCompliantDeviceCountsByOwnershipTypes(nonCompliantFeatureCode, filterSet, userName); } catch (InvalidFeatureCodeValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service function " + @@ -348,7 +358,8 @@ public class DashboardImpl implements Dashboard { // creating filteredDeviceCount Data Wrapper DeviceCountByGroup filteredDeviceCount; try { - filteredDeviceCount = gadgetDataService.getDeviceCount(filterSet); + String userName = getAuthenticatedUser(); + filteredDeviceCount = gadgetDataService.getDeviceCount(filterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -365,7 +376,8 @@ public class DashboardImpl implements Dashboard { // creating TotalDeviceCount Data Wrapper DeviceCountByGroup totalDeviceCount; try { - totalDeviceCount = gadgetDataService.getTotalDeviceCount(); + String userName = getAuthenticatedUser(); + totalDeviceCount = gadgetDataService.getTotalDeviceCount(userName); } catch (DataAccessLayerException e) { log.error("An internal error occurred while trying to execute relevant data service function " + "@ Dashboard API layer to retrieve the total device count over filtered.", e); @@ -405,8 +417,9 @@ public class DashboardImpl implements Dashboard { // creating featureNonCompliantDeviceCount Data Wrapper DeviceCountByGroup featureNonCompliantDeviceCount; try { + String userName = getAuthenticatedUser(); featureNonCompliantDeviceCount = gadgetDataService. - getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet); + getFeatureNonCompliantDeviceCount(nonCompliantFeatureCode, filterSet, userName); } catch (InvalidFeatureCodeValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service function " + @@ -423,7 +436,8 @@ public class DashboardImpl implements Dashboard { // creating TotalDeviceCount Data Wrapper DeviceCountByGroup totalDeviceCount; try { - totalDeviceCount = gadgetDataService.getTotalDeviceCount(); + String userName = getAuthenticatedUser(); + totalDeviceCount = gadgetDataService.getTotalDeviceCount(userName); } catch (DataAccessLayerException e) { log.error("An internal error occurred while trying to execute relevant data service function " + "@ Dashboard API layer to retrieve the total device count over filtered feature non-compliant.", e); @@ -477,8 +491,9 @@ public class DashboardImpl implements Dashboard { PaginationResult paginationResult; try { + String userName = getAuthenticatedUser(); paginationResult = gadgetDataService. - getDevicesWithDetails(filterSet, startIndex, resultCount); + getDevicesWithDetails(filterSet, startIndex, resultCount, userName); } catch (InvalidPotentialVulnerabilityValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -530,7 +545,8 @@ public class DashboardImpl implements Dashboard { List devicesWithDetails; try { - devicesWithDetails = gadgetDataService.getDevicesWithDetails(filterSet); + String userName = getAuthenticatedUser(); + devicesWithDetails = gadgetDataService.getDevicesWithDetails(filterSet, userName); } catch (InvalidPotentialVulnerabilityValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -591,9 +607,10 @@ public class DashboardImpl implements Dashboard { PaginationResult paginationResult; try { + String userName = getAuthenticatedUser(); paginationResult = gadgetDataService. getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, - filterSet, startIndex, resultCount); + filterSet, startIndex, resultCount, userName); } catch (InvalidFeatureCodeValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " + @@ -648,8 +665,9 @@ public class DashboardImpl implements Dashboard { List featureNonCompliantDevicesWithDetails; try { + String userName = getAuthenticatedUser(); featureNonCompliantDevicesWithDetails = gadgetDataService. - getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet); + getFeatureNonCompliantDevicesWithDetails(nonCompliantFeatureCode, filterSet, userName); } catch (InvalidFeatureCodeValueException e) { log.error("Bad request and error occurred @ Gadget Data Service layer due to " + "invalid (query) parameter value. This was while trying to execute relevant data service " +