From f6ede45046c206c595e29a9c47422e6d8144f8bb Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 17 Jul 2017 15:59:18 +0530 Subject: [PATCH] Fixing the download agent issue for virtual fire alarm --- .../impl/VirtualFireAlarmServiceImpl.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index 0e540978b..367cd248b 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -233,15 +233,27 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { throw new DeviceManagementException(msg); } if (apiApplicationKey == null) { + String adminUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() + .getRealmConfiguration().getAdminUserName(); + String tenantAdminDomainName = PrivilegedCarbonContext.getThreadLocalCarbonContext() + .getTenantDomain(); String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() .getAdminUserName() + "@" + PrivilegedCarbonContext.getThreadLocalCarbonContext() .getTenantDomain(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE}; - apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( - VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, - VirtualFireAlarmConstants.APIM_APPLICATION_TOKEN_VALIDITY_PERIOD); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantAdminDomainName); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(adminUsername); + + apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( + VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, + VirtualFireAlarmConstants.APIM_APPLICATION_TOKEN_VALIDITY_PERIOD); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } } JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient(); String scopes = " device_" + deviceId;