From 9f6dd552236c03e13772da19d6f259d8a2eb9289 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Wed, 26 Aug 2015 14:05:21 +0530 Subject: [PATCH 1/5] fixed issue in modify enrollment --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 3 +-- .../mgt/core/dao/impl/DeviceDAOImpl.java | 18 ++++++++---------- .../DeviceManagementProviderServiceImpl.java | 5 +++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 5b5d0c66aa6..70c403a2e8d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -22,7 +22,6 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import java.util.List; @@ -33,7 +32,7 @@ public interface DeviceDAO { int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; - int updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; + boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 6d8d4107bf6..35e84667126 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -67,11 +67,11 @@ public class DeviceDAOImpl implements DeviceDAO { } @Override - public int updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { + public boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; - int deviceId = -1; + boolean status = false; + int rows; try { conn = this.getConnection(); String sql = @@ -83,18 +83,16 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setString(3, device.getDeviceIdentifier()); stmt.setInt(4, typeId); stmt.setInt(5, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - deviceId = rs.getInt(1); + rows = stmt.executeUpdate(); + if (rows>0) { + status = true; } - return deviceId; + return status; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + device.getName() + "'", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 197a9321e3f..172fa386337 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -139,6 +139,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + device.setId(existingDevice.getId()); device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); this.modifyEnrollment(device); status = true; @@ -210,8 +211,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - int deviceId = deviceDAO.updateDevice(type.getId(), device, tenantId); - enrolmentDAO.updateEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + deviceDAO.updateDevice(type.getId(), device, tenantId); + enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException | TransactionManagementException e) { From 7642112a9fdae0affe571b8a99ed1d16c1f901c2 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Wed, 2 Sep 2015 17:27:28 +0530 Subject: [PATCH 2/5] code clean up --- .../org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 814084aa70a..4441c25f2bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -84,7 +84,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(4, typeId); stmt.setInt(5, tenantId); rows = stmt.executeUpdate(); - if (rows>0) { + if (rows > 0) { status = true; } return status; From dbf95bcc7c41080508f682429d971286f6298f14 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 3 Sep 2015 20:14:45 +0530 Subject: [PATCH 3/5] Fixing an issue in the logic of authentication incoming requests --- .../framework/WebappAuthenticationHandler.java | 6 +++--- 1 file changed, 3 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/WebappAuthenticationHandler.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationHandler.java index 97683448320..c700fb304f3 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationHandler.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationHandler.java @@ -35,7 +35,7 @@ public class WebappAuthenticationHandler extends CarbonTomcatValve { @Override public void invoke(Request request, Response response, CompositeValve compositeValve) { - if (this.isContextSkipped(request) || (this.isNonAdminService(request) && this.skipAuthentication(request))) { + if (this.isContextSkipped(request) || (!this.isAdminService(request) && this.skipAuthentication(request))) { this.getNext().invoke(request, response, compositeValve); return; } @@ -49,9 +49,9 @@ public class WebappAuthenticationHandler extends CarbonTomcatValve { this.processResponse(request, response, compositeValve, status); } - private boolean isNonAdminService(Request request) { + private boolean isAdminService(Request request) { String param = request.getContext().findParameter("isAdminService"); - return !(param != null && Boolean.parseBoolean(param)); + return (param != null && Boolean.parseBoolean(param)); } private boolean skipAuthentication(Request request) { From 11ead87a5e2f5fcaf09e48497028dbf0a163772c Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 3 Sep 2015 20:49:26 +0530 Subject: [PATCH 4/5] Fixing the policy listing issue --- .../mgt/core/cache/impl/PolicyCacheManagerImpl.java | 10 ++++++++++ .../mgt/core/impl/PolicyAdministratorPointImpl.java | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java index 624b5ac9d4b..2e955469106 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java @@ -85,7 +85,17 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { for (Policy policy : cachedPolicy) { log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy. getPolicyName() + " Activated : " + policy.isActive()); + + List users = policy.getUsers(); + for (String user : users) { + log.debug("Users in cached policy : " + user); + } + List roles = policy.getRoles(); + for (String role : roles) { + log.debug("Roles in cached policy : " + role); + } } + } return lCache.get(1); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index b17fd2bd6e4..c536987bd43 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -68,7 +68,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { // } catch (PolicyDelegationException e) { // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // } - PolicyCacheManagerImpl.getInstance().addPolicy(resultantPolicy); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); return resultantPolicy; } @@ -80,7 +80,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { // } catch (PolicyDelegationException e) { // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // } - PolicyCacheManagerImpl.getInstance().updatePolicy(resultantPolicy); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); return resultantPolicy; } From 4500aacd38fdc8f6b11cf39210c0855e031a4ab3 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 3 Sep 2015 23:38:27 +0530 Subject: [PATCH 5/5] Adding the scep support for getting the device tenant id --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 9 ++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 32 +++++++ .../device/mgt/core/scep/SCEPException.java | 58 +++++++++++++ .../device/mgt/core/scep/SCEPManager.java | 27 ++++++ .../device/mgt/core/scep/SCEPManagerImpl.java | 85 +++++++++++++++++++ .../mgt/core/scep/TenantedDeviceWrapper.java | 53 ++++++++++++ 6 files changed, 264 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPException.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/TenantedDeviceWrapper.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 0e56262c14e..c709f562fe9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; +import java.util.HashMap; import java.util.List; /** @@ -64,6 +65,14 @@ public interface DeviceDAO { */ Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException; + /** + * + * @param deviceIdentifier device id. + * @return HashMap + * @throws DeviceManagementDAOException + */ + HashMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; + /** * This method is used to retrieve a device of a given id. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 9f4ca53f4c6..401557e1957 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.sql.*; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; public class DeviceDAOImpl implements DeviceDAO { @@ -131,6 +132,37 @@ public class DeviceDAOImpl implements DeviceDAO { return device; } + @Override + public HashMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + Device device; + HashMap deviceHashMap = new HashMap<>(); + try { + conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.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 d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID "; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceIdentifier.getType()); + stmt.setString(2, deviceIdentifier.getId()); + rs = stmt.executeQuery(); + if (rs.next()) { + device = this.loadDevice(rs); + deviceHashMap.put(rs.getInt("TENANT_ID"), device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type " + + "'" + deviceIdentifier.getType() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return deviceHashMap; + } + @Override public Device getDevice(int deviceId, int tenantId) throws DeviceManagementDAOException { Connection conn; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPException.java new file mode 100644 index 00000000000..aed87dd1c60 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPException.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2015, 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.core.scep; + +public class SCEPException extends Exception { + + private static final long serialVersionUID = -3151279390702337L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public SCEPException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public SCEPException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public SCEPException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public SCEPException() { + super(); + } + + public SCEPException(Throwable cause) { + super(cause); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManager.java new file mode 100644 index 00000000000..d84f5e16c04 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManager.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015, 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.core.scep; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + +public interface SCEPManager { + + TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManagerImpl.java new file mode 100644 index 00000000000..8a59b1789e4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/SCEPManagerImpl.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2015, 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.core.scep; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.sql.SQLException; +import java.util.HashMap; + +public class SCEPManagerImpl implements SCEPManager { + + private DeviceDAO deviceDAO; + private static final Log log = LogFactory.getLog(SCEPManagerImpl.class); + + public SCEPManagerImpl() { + deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + } + + @Override + public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException { + + TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper(); + try { + DeviceManagementDAOFactory.openConnection(); + HashMap deviceHashMap = deviceDAO.getDevice(deviceIdentifier); + Integer tenantId = (Integer) deviceHashMap.keySet().toArray()[0]; + tenantedDeviceWrapper.setDevice(deviceHashMap.get(tenantId)); + tenantedDeviceWrapper.setTenantId(tenantId); + + + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID); + + RealmService realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); + if (realmService == null) { + String msg = "RealmService is not initialized"; + log.error(msg); + throw new SCEPException(msg); + } + + String tenantDomain = realmService.getTenantManager().getDomain(tenantId); + tenantedDeviceWrapper.setTenantDomain(tenantDomain); + + } catch (SQLException e) { + throw new SCEPException("Error occurred while getting the datasource connection.", e); + } catch (DeviceManagementDAOException e) { + throw new SCEPException("Error occurred while reading the device dao.", e); + } catch (UserStoreException e) { + throw new SCEPException("Error occurred while getting the tenant domain.", e); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + DeviceManagementDAOFactory.closeConnection(); + } + return tenantedDeviceWrapper; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/TenantedDeviceWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/TenantedDeviceWrapper.java new file mode 100644 index 00000000000..ee7d4bb9f59 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/scep/TenantedDeviceWrapper.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2015, 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.core.scep; + +import org.wso2.carbon.device.mgt.common.Device; + +public class TenantedDeviceWrapper { + + private Device device; + private int tenantId; + private String tenantDomain; + + public Device getDevice() { + return device; + } + + public void setDevice(Device device) { + this.device = device; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public void setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + } +}