Change methods to get device info if necessary

This change was done in order to fix a issue while reading
IOS operations which uses agent(eg: location).
Now it's possible to get devices with there properties
feature/appm-store/pbac
Yohan Avishke 5 years ago committed by Charitha Goonetilleke
parent ff2691f5bb
commit 2665243aa3

@ -16,6 +16,23 @@
~ specific language governing permissions and limitations
~ under the License.
-->
<!--
~ Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
~
~ Entgra (pvt) Ltd. 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
@ -58,6 +75,7 @@
org.osgi.framework,
org.osgi.service.component,
org.apache.commons.logging,
org.apache.commons.collections.map,
javax.security.auth.x500,
javax.xml.*,
javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional,

@ -15,10 +15,40 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.certificate.mgt.core.scep;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.user.api.UserStoreException;
public interface SCEPManager {
TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException;
/**
* This method is used to retrieve a device of a given identifier wrapped by a
* {@link TenantedDeviceWrapper} with it's tenant info
*
* @param deviceIdentifier device identifier
* @return {@link TenantedDeviceWrapper} with a device's info and tenant info
* @throws SCEPException will be thrown in case device is null or if a
* {@link DeviceManagementException} or a {@link UserStoreException} is thrown
*/
TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier)
throws SCEPException;
}

@ -15,8 +15,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.certificate.mgt.core.scep;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.certificate.mgt.core.internal.CertificateManagementDataHolder;
@ -29,8 +48,6 @@ import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.util.HashMap;
public class SCEPManagerImpl implements SCEPManager {
private static final Log log = LogFactory.getLog(SCEPManagerImpl.class);
DeviceManagementProviderService dms;
@ -40,21 +57,26 @@ public class SCEPManagerImpl implements SCEPManager {
}
@Override
public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException {
TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper();
try {
HashMap<Integer, Device> deviceHashMap = dms.getTenantedDevice(deviceIdentifier);
Object[] keySet = deviceHashMap.keySet().toArray();
public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier)
throws SCEPException {
SingletonMap deviceMap;
if (keySet.length == 0) {
throw new SCEPException("Lookup device not found for the device identifier");
try {
deviceMap = dms.getTenantedDevice(deviceIdentifier, false);
if (deviceMap == null) {
String msg = "Lookup device not found for the device identifier " + deviceIdentifier.getId() +
" of device type " + deviceIdentifier.getType();
log.error(msg);
throw new SCEPException(msg);
}
Integer tenantId = (Integer) keySet[0];
tenantedDeviceWrapper.setDevice(deviceHashMap.get(tenantId));
tenantedDeviceWrapper.setTenantId(tenantId);
} catch (DeviceManagementException e) {
String msg = "Error occurred while getting device " + deviceIdentifier;
log.error(msg);
throw new SCEPException(msg, e);
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
@ -66,16 +88,22 @@ public class SCEPManagerImpl implements SCEPManager {
log.error(msg);
throw new SCEPException(msg);
}
TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper();
int tenantId = (int) deviceMap.getKey();
String tenantDomain = realmService.getTenantManager().getDomain(tenantId);
tenantedDeviceWrapper.setTenantId(tenantId);
tenantedDeviceWrapper.setTenantDomain(tenantDomain);
tenantedDeviceWrapper.setDevice((Device) deviceMap.getValue());
return tenantedDeviceWrapper;
} catch (UserStoreException e) {
throw new SCEPException("Error occurred while getting the tenant domain.", e);
} catch (DeviceManagementException e) {
throw new SCEPException("Error occurred while getting device '" + deviceIdentifier + "'.", e);
String msg = "Error occurred while getting the tenant domain.";
log.error(msg);
throw new SCEPException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return tenantedDeviceWrapper;
}
}

@ -32,9 +32,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.core.dao;
import org.apache.commons.collections.map.SingletonMap;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -50,7 +68,6 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -258,12 +275,13 @@ public interface DeviceDAO {
throws DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given identifier with it's tenant id
*
* @param deviceIdentifier device id.
* @return HashMap
* @throws DeviceManagementDAOException
* @return {@link SingletonMap} with device and corresponding tenant id
* @throws DeviceManagementDAOException will be thrown in case of a {@link SQLException}
*/
HashMap<Integer, Device> getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException;
SingletonMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given tenant id.

@ -33,9 +33,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.core.dao.impl;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
@ -45,7 +63,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
@ -64,7 +81,6 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
@ -647,34 +663,58 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public HashMap<Integer, Device> getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
Device device;
HashMap<Integer, Device> deviceHashMap = new HashMap<>();
public SingletonMap getDevice(DeviceIdentifier deviceIdentifier)
throws DeviceManagementDAOException {
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, e.TENANT_ID, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID ORDER BY e.DATE_OF_LAST_UPDATE DESC";
stmt = conn.prepareStatement(sql);
Connection conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, " +
"d1.DESCRIPTION, " +
"d1.NAME AS DEVICE_NAME, " +
"d1.DEVICE_TYPE, " +
"e.TENANT_ID, " +
"d1.DEVICE_IDENTIFICATION, " +
"e.OWNER, " +
"e.OWNERSHIP, " +
"e.STATUS, " +
"e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, " +
"e.ID AS ENROLMENT_ID " +
"FROM DM_ENROLMENT e, " +
"(SELECT d.ID, " +
"d.DESCRIPTION, " +
"d.NAME, " +
"t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION " +
"FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t " +
"WHERE t.NAME = ? " +
"AND t.ID = d.DEVICE_TYPE_ID " +
"AND d.DEVICE_IDENTIFICATION = ?) d1 " +
"WHERE d1.ID = e.DEVICE_ID " +
"ORDER BY e.DATE_OF_LAST_UPDATE DESC";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, deviceIdentifier.getType());
stmt.setString(2, deviceIdentifier.getId());
rs = stmt.executeQuery();
if (rs.next()) {
device = DeviceManagementDAOUtil.loadDevice(rs);
deviceHashMap.put(rs.getInt("TENANT_ID"), device);
try (ResultSet rs = stmt.executeQuery()) {
SingletonMap deviceMap = null;
if (rs.next()) {
deviceMap = new SingletonMap(
rs.getInt("TENANT_ID"),
DeviceManagementDAOUtil.loadDevice(rs)
);
}
return deviceMap;
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
"'" + deviceIdentifier.getType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
String msg = "Error occurred while listing devices (with tenant id) for type " +
deviceIdentifier.getType();
log.error(msg);
throw new DeviceManagementDAOException(msg, e);
}
return deviceHashMap;
}
@Override

@ -32,9 +32,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager;
@ -67,13 +85,14 @@ import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecu
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.common.ui.policy.mgt.PolicyConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -564,7 +583,18 @@ public interface DeviceManagementProviderService {
*/
int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException;
HashMap<Integer, Device> getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException;
/**
* This method is used to retrieve a device of a given identifier with it's tenant id
*
* @param deviceIdentifier device identifier
* @param requireDeviceInfo boolean indicating whether the device info and properties
* is also required
* @return {@link SingletonMap} with a device and it's corresponding tenant id
* @throws DeviceManagementException will be thrown in case of a {@link SQLException}
* or {@link DeviceManagementDAOException}
*/
SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo)
throws DeviceManagementException;
void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException,
ConfigurationManagementException;

@ -32,8 +32,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -1174,43 +1193,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public HashMap<Integer, Device> getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
public SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo)
throws DeviceManagementException {
if (deviceIdentifier == null) {
String msg = "Received null deviceIdentifier for getTenantedDevice";
log.error(msg);
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" +
deviceIdentifier.getType() + "'");
log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type " +
deviceIdentifier.getType());
}
HashMap<Integer, Device> deviceHashMap;
SingletonMap deviceMap;
try {
DeviceManagementDAOFactory.openConnection();
deviceHashMap = deviceDAO.getDevice(deviceIdentifier);
if (deviceHashMap == null) {
deviceMap = deviceDAO.getDevice(deviceIdentifier);
if (deviceMap == null) {
if (log.isDebugEnabled()) {
log.debug("No device is found upon the type '" + deviceIdentifier.getType() + "' and id '" +
deviceIdentifier.getId() + "'");
log.debug("Unable to find device for type " + deviceIdentifier.getType() +
" and id " + deviceIdentifier.getId());
}
return new HashMap<>();
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while obtaining the device for id '" + deviceIdentifier.getId() + "'";
String msg = "Error occurred while obtaining the device for id " + deviceIdentifier.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getTenantedDevice device: " + deviceIdentifier.getId();
String msg = "Error occurred while opening a connection for the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return deviceHashMap;
if (requireDeviceInfo && deviceMap != null) {
getAllDeviceInfo((Device) deviceMap.getValue());
}
return deviceMap;
}
@Override

@ -33,9 +33,27 @@
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
*
* Entgra (pvt) Ltd. 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.core.service;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mockito.Mockito;
@ -82,7 +100,6 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
@ -732,11 +749,11 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
public void testGetTenantedDevice() throws DeviceManagementException {
HashMap<Integer, Device> deviceMap = deviceMgtService.getTenantedDevice(new
SingletonMap deviceMap = deviceMgtService.getTenantedDevice(new
DeviceIdentifier
(DEVICE_ID, DEVICE_TYPE));
(DEVICE_ID, DEVICE_TYPE), false);
if (!isMock()) {
Assert.assertTrue(!deviceMap.isEmpty());
Assert.assertFalse(deviceMap.isEmpty());
}
}

Loading…
Cancel
Save