From 8b058945c96b014af6369cab3179f115824768b7 Mon Sep 17 00:00:00 2001 From: "amalka.subasinghe" Date: Wed, 8 May 2024 22:34:15 +0530 Subject: [PATCH] fixed NPE --- .../mgt/plugins/emqx/exhook/ExServer.java | 113 ++++++++++-------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/components/extensions/emqx-extensions/io.entgra.device.mgt.plugins.emqx.exhook/src/main/java/io/entgra/device/mgt/plugins/emqx/exhook/ExServer.java b/components/extensions/emqx-extensions/io.entgra.device.mgt.plugins.emqx.exhook/src/main/java/io/entgra/device/mgt/plugins/emqx/exhook/ExServer.java index 0d5d3d3a7..c297c885d 100644 --- a/components/extensions/emqx-extensions/io.entgra.device.mgt.plugins.emqx.exhook/src/main/java/io/entgra/device/mgt/plugins/emqx/exhook/ExServer.java +++ b/components/extensions/emqx-extensions/io.entgra.device.mgt.plugins.emqx.exhook/src/main/java/io/entgra/device/mgt/plugins/emqx/exhook/ExServer.java @@ -44,13 +44,14 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import java.io.IOException; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; public class ExServer { private static final Log logger = LogFactory.getLog(ExServer.class.getName()); - private static Map accessTokenMap = new HashMap<>(); - private static Map authorizedScopeMap = new HashMap<>(); + private static Map accessTokenMap = new ConcurrentHashMap<>(); + private static Map authorizedScopeMap = new ConcurrentHashMap<>(); private Server server; public ExServer() { @@ -177,25 +178,27 @@ public class ExServer { if (request.getResultCode().equals("success")) { String accessToken = accessTokenMap.get(request.getConninfo().getClientid()); String scopeString = authorizedScopeMap.get(accessToken); - String[] scopeArray = scopeString.split(" "); - String deviceType = null; - String deviceId = null; - for (String scope : scopeArray) { - if (scope.startsWith("device_")) { - String[] scopeParts = scope.split("_"); - deviceType = scopeParts[1]; - deviceId = scopeParts[2]; - break; + if (!StringUtils.isEmpty(scopeString)) { + String[] scopeArray = scopeString.split(" "); + String deviceType = null; + String deviceId = null; + for (String scope : scopeArray) { + if (scope.startsWith("device_")) { + String[] scopeParts = scope.split("_"); + deviceType = scopeParts[1]; + deviceId = scopeParts[2]; + break; + } } - } - if (!StringUtils.isEmpty(deviceType) && !StringUtils.isEmpty(deviceId)) { - try { - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super"); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(-1234); - DeviceManagementProviderService deviceManagementProviderService = getDeviceManagementService(); - deviceManagementProviderService.changeDeviceStatus(new DeviceIdentifier(deviceId, deviceType), EnrolmentInfo.Status.ACTIVE); - } catch (DeviceManagementException e) { - logger.error("onClientConnack: Error while setting device status"); + if (!StringUtils.isEmpty(deviceType) && !StringUtils.isEmpty(deviceId)) { + try { + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super"); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(-1234); + DeviceManagementProviderService deviceManagementProviderService = getDeviceManagementService(); + deviceManagementProviderService.changeDeviceStatus(new DeviceIdentifier(deviceId, deviceType), EnrolmentInfo.Status.ACTIVE); + } catch (DeviceManagementException e) { + logger.error("onClientConnack: Error while setting device status"); + } } } } @@ -315,53 +318,57 @@ public class ExServer { if (StringUtils.isEmpty(accessToken) || !accessToken.startsWith(request.getClientinfo().getUsername())) { logger.info("Valid access token not found"); responseObserver.onError(new Exception("not authorized")); + return; } String authorizedScopeList = authorizedScopeMap.get(accessToken); - String[] scopeArray = authorizedScopeList.split(" "); - List scopeList = Arrays.asList(scopeArray); boolean isFound = false; + if (!StringUtils.isEmpty(authorizedScopeList)) { + String[] scopeArray = authorizedScopeList.split(" "); + List scopeList = Arrays.asList(scopeArray); - String tempScope = null; - String requestTopic = request.getTopic(); - if (request.getType().equals(ClientCheckAclRequest.AclReqType.PUBLISH)) { - requestTopic = requestTopic.replace("/", ":"); + String tempScope = null; + String requestTopic = request.getTopic(); - String[] requestTopicParts = requestTopic.split(":"); + if (request.getType().equals(ClientCheckAclRequest.AclReqType.PUBLISH)) { + requestTopic = requestTopic.replace("/", ":"); - if (requestTopicParts.length >= 4 && "operation".equals(requestTopicParts[3])) { - // publishing operation from iot server to emqx - tempScope = "perm:topic:pub:" + requestTopicParts[0] + ":+:+:operation"; - } else { - // publishing operation response from device to emqx - // publishing events from device to emqx - tempScope = "perm:topic:pub:" + requestTopic; - } + String[] requestTopicParts = requestTopic.split(":"); - for (String scope : scopeList) { - if (scope.startsWith(tempScope)) { - isFound = true; - break; + if (requestTopicParts.length >= 4 && "operation".equals(requestTopicParts[3])) { + // publishing operation from iot server to emqx + tempScope = "perm:topic:pub:" + requestTopicParts[0] + ":+:+:operation"; + } else { + // publishing operation response from device to emqx + // publishing events from device to emqx + tempScope = "perm:topic:pub:" + requestTopic; } - } - } - if (request.getType().equals(ClientCheckAclRequest.AclReqType.SUBSCRIBE)) { - if (requestTopic.endsWith("/#")) { - requestTopic = requestTopic.substring(0, requestTopic.indexOf("/#")); + for (String scope : scopeList) { + if (scope.startsWith(tempScope)) { + isFound = true; + break; + } + } } - requestTopic = requestTopic.replace("/", ":"); - // subscribing for events from iotserver to emqx - // subscribing for operation from device to emqx - // subscribing for operation response from iotserver to emqx - tempScope = "perm:topic:sub:" + requestTopic; + if (request.getType().equals(ClientCheckAclRequest.AclReqType.SUBSCRIBE)) { + if (requestTopic.endsWith("/#")) { + requestTopic = requestTopic.substring(0, requestTopic.indexOf("/#")); + } - for (String scope : scopeList) { - if (scope.startsWith(tempScope)) { - isFound = true; - break; + requestTopic = requestTopic.replace("/", ":"); + // subscribing for events from iotserver to emqx + // subscribing for operation from device to emqx + // subscribing for operation response from iotserver to emqx + tempScope = "perm:topic:sub:" + requestTopic; + + for (String scope : scopeList) { + if (scope.startsWith(tempScope)) { + isFound = true; + break; + } } } }