From 28e0c88222e8574b30882c57606f00b0d8830ec5 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 27 Feb 2021 10:02:19 +0530 Subject: [PATCH] Fix FCM and configuration fetching issues --- .../provider/fcm/FCMNotificationStrategy.java | 54 ++++++++++--------- .../core/operation/mgt/ProfileOperation.java | 1 + .../template/DeviceTypeManagerService.java | 15 +++++- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index 2894b04326e..5c80aa62f96 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -64,7 +64,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { if (NOTIFIER_TYPE_FCM.equals(config.getType())) { Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService() .getDeviceWithTypeProperties(ctx.getDeviceId()); - if(getFCMToken(device.getProperties()) != null) { + if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) { this.sendWakeUpCall(ctx.getOperation().getCode(), device); } } else { @@ -92,33 +92,35 @@ public class FCMNotificationStrategy implements NotificationStrategy { private void sendWakeUpCall(String message, Device device) throws IOException, PushNotificationExecutionFailedException { - OutputStream os = null; - byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes(); - - HttpURLConnection conn = null; - try { - conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection(); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY)); - conn.setRequestMethod("POST"); - conn.setDoOutput(true); - os = conn.getOutputStream(); - os.write(bytes); - } finally { - if (os != null) { - os.close(); + if (device.getProperties() != null) { + OutputStream os = null; + byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes(); + + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection(); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY)); + conn.setRequestMethod("POST"); + conn.setDoOutput(true); + os = conn.getOutputStream(); + os.write(bytes); + } finally { + if (os != null) { + os.close(); + } + if (conn != null) { + conn.disconnect(); + } } - if (conn != null) { - conn.disconnect(); + int status = conn.getResponseCode(); + if (log.isDebugEnabled()) { + log.debug("Result code: " + status + ", Message: " + conn.getResponseMessage()); + } + if (status != HTTP_STATUS_CODE_OK) { + throw new PushNotificationExecutionFailedException("Push notification sending failed with the HTTP " + + "error code '" + status + "'"); } - } - int status = conn.getResponseCode(); - if (log.isDebugEnabled()) { - log.debug("Result code: " + status + ", Message: " + conn.getResponseMessage()); - } - if (status != HTTP_STATUS_CODE_OK) { - throw new PushNotificationExecutionFailedException("Push notification sending failed with the HTTP " + - "error code '" + status + "'"); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java index 03efcd8dedb..34235246668 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; public class ProfileOperation extends ConfigOperation implements Serializable { + private static final long serialVersionUID = -3322674908775087365L; private List correctiveActionIds; private List reactiveActionIds; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 587752458dd..0d4a6249e81 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -36,6 +36,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; @@ -336,7 +337,19 @@ public class DeviceTypeManagerService implements DeviceManagementService { private Map getConfigProperty(List configs) { Map propertMap = new HashMap<>(); for (ConfigurationEntry entry : configs) { - propertMap.put(entry.getName(), entry.getValue().toString()); + if (entry != null && entry.getValue() != null && entry.getName() != null) { + propertMap.put(entry.getName(), entry.getValue().toString()); + } else { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + String domain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String message = "Could not find a property in tenant id: " + tenantId + ", " + + "domain: " + domain; + if (entry != null && entry.getName() != null) { + message += ", missing value for properly: " + entry.getName(); + } + + log.error(message); + } } return propertMap; }