From 5c73277c32d80d629ab69e94035f5860727e0441 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 12 Jun 2024 12:37:25 +0530 Subject: [PATCH 1/6] Add FCM changes --- .../pom.xml | 8 ++- .../provider/fcm/FCMNotificationStrategy.java | 69 ++++++++++++++++--- pom.xml | 5 ++ 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml index ffb529ef292..ea7aec37a04 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml @@ -111,6 +111,10 @@ org.wso2.carbon.analytics-common org.wso2.carbon.event.output.adapter.core + + com.google.auth + google-auth-library-oauth2-http + @@ -141,7 +145,9 @@ io.entgra.device.mgt.core.device.mgt.common.push.notification, org.apache.commons.logging, io.entgra.device.mgt.core.device.mgt.common.*, - io.entgra.device.mgt.core.device.mgt.core.service + io.entgra.device.mgt.core.device.mgt.core.service, + io.entgra.device.mgt.core.device.mgt.extensions.logger.spi, + io.entgra.device.mgt.core.notification.logger.* diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index c6100aa4183..669edd314a4 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -17,9 +17,14 @@ */ package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm; +import com.google.auth.oauth2.GoogleCredentials; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import com.hazelcast.aws.utility.Environment; +import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.OperationManagerImpl; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceConnectivityLoggerImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Device; @@ -30,10 +35,14 @@ import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotific import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException; import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Arrays; import java.util.List; public class FCMNotificationStrategy implements NotificationStrategy { @@ -42,7 +51,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { private static final String NOTIFIER_TYPE_FCM = "FCM"; private static final String FCM_TOKEN = "FCM_TOKEN"; - private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/fcm/send"; + private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send"; private static final String FCM_API_KEY = "fcmAPIKey"; private static final int TIME_TO_LIVE = 2419199; // 1 second less that 28 days private static final int HTTP_STATUS_CODE_OK = 200; @@ -59,12 +68,13 @@ public class FCMNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { + String token = getFcmOauthToken(); try { if (NOTIFIER_TYPE_FCM.equals(config.getType())) { Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService() .getDeviceWithTypeProperties(ctx.getDeviceId()); if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) { - this.sendWakeUpCall(ctx.getOperation().getCode(), device); + this.sendWakeUpCall(ctx.getOperation().getCode(), device, token); } } else { if (log.isDebugEnabled()) { @@ -79,6 +89,25 @@ public class FCMNotificationStrategy implements NotificationStrategy { } } + private String getFcmOauthToken() { + GoogleCredentials googleCredentials = null; + try { + googleCredentials = GoogleCredentials + .fromStream(new FileInputStream("/etc/service-account.json")) + .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging")); + googleCredentials.refresh(); + if (null != googleCredentials) { + writeLog("========= Google Credentials created " + googleCredentials.getAccessToken()); + } else { + writeLog("========= Google Credentials is null"); + } + return googleCredentials.getAccessToken().getTokenValue(); + } catch (IOException e) { + log.error("Error occurred while getting the FCM OAuth token.", e); + throw new RuntimeException(e); + } + } + @Override public NotificationContext buildContext() { return null; @@ -89,9 +118,10 @@ public class FCMNotificationStrategy implements NotificationStrategy { } - private void sendWakeUpCall(String message, Device device) throws IOException, + private void sendWakeUpCall(String message, Device device, String token) throws IOException, PushNotificationExecutionFailedException { if (device.getProperties() != null) { + writeLog("===== Calling senWakeupCall " + device); OutputStream os = null; byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes(); @@ -99,7 +129,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { try { conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection(); conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY)); + conn.setRequestProperty("Authorization", "Bearer " + token); conn.setRequestMethod("POST"); conn.setDoOutput(true); os = conn.getOutputStream(); @@ -125,7 +155,16 @@ public class FCMNotificationStrategy implements NotificationStrategy { private static String getFCMRequest(String message, String registrationId) { JsonObject fcmRequest = new JsonObject(); - fcmRequest.addProperty("delay_while_idle", false); + JsonObject messageObject = new JsonObject(); + messageObject.addProperty("token", registrationId); + JsonObject notification = new JsonObject(); + notification.addProperty("title", "FCM Message"); + notification.addProperty("body", message); + messageObject.add("notification", notification); + fcmRequest.add("message", messageObject); + + + /*fcmRequest.addProperty("delay_while_idle", false); fcmRequest.addProperty("time_to_live", TIME_TO_LIVE); fcmRequest.addProperty("priority", "high"); @@ -140,7 +179,9 @@ public class FCMNotificationStrategy implements NotificationStrategy { JsonArray regIds = new JsonArray(); regIds.add(new JsonPrimitive(registrationId)); - fcmRequest.add("registration_ids", regIds); + fcmRequest.add("registration_ids", regIds);*/ + + writeLog("========= FCM Request " + fcmRequest); return fcmRequest.toString(); } @@ -155,9 +196,21 @@ public class FCMNotificationStrategy implements NotificationStrategy { return fcmToken; } + private static void writeLog(String message) { + try (FileWriter fw = new FileWriter("/opt/entgra/migration/entgra-uem-ultimate-6.0.3.0/log.txt", true); + BufferedWriter bw = new BufferedWriter(fw)) { + bw.write(message); + bw.newLine(); + bw.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override public PushNotificationConfig getConfig() { return config; } + } diff --git a/pom.xml b/pom.xml index 680a4ced226..516bd8e9826 100644 --- a/pom.xml +++ b/pom.xml @@ -1916,6 +1916,11 @@ mockito-inline ${mokito.version} + + com.google.auth + google-auth-library-oauth2-http + 1.20.0 + From aafa824f0838cd64c86c11cd5b54bd79a3966c01 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 16 Jun 2024 15:51:56 +0530 Subject: [PATCH 2/6] Migrate from legacy FCM APIs to HTTP v1 --- .../pom.xml | 53 ++++- .../fcm/FCMBasedPushNotificationProvider.java | 4 +- .../provider/fcm/FCMNotificationStrategy.java | 189 ++++++++---------- .../push/notification/ContextMetadata.java | 30 +++ .../PushNotificationConfiguration.java | 11 + .../repository/conf/cdm-config.xml.j2 | 5 + pom.xml | 55 ++++- 7 files changed, 241 insertions(+), 106 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml index ea7aec37a04..4d4bf35cf96 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml @@ -112,9 +112,45 @@ org.wso2.carbon.event.output.adapter.core - com.google.auth + org.wso2.orbit.com.google.http-client + google-http-client + + + org.wso2.orbit.com.google.auth-library-oauth2-http google-auth-library-oauth2-http + + org.wso2.orbit.io.opencensus + opencensus + + + io.opencensus + opencensus-api + + + io.opencensus + opencensus-contrib-http-util + + + org.wso2.orbit.io.grpc + grpc-context + + + com.google.http-client + google-http-client-gson + + + com.google.guava + failureaccess + + + com.google.guava + guava + + + org.wso2.carbon + org.wso2.carbon.utils + @@ -141,14 +177,27 @@ com.google.gson, org.osgi.framework.*;version="${imp.package.version.osgi.framework}", org.osgi.service.*;version="${imp.package.version.osgi.service}", + org.wso2.carbon.utils.*, io.entgra.device.mgt.core.device.mgt.common.operation.mgt, io.entgra.device.mgt.core.device.mgt.common.push.notification, org.apache.commons.logging, io.entgra.device.mgt.core.device.mgt.common.*, io.entgra.device.mgt.core.device.mgt.core.service, + io.entgra.device.mgt.core.device.mgt.core.config.*, + io.entgra.device.mgt.core.device.mgt.core.config.push.notification.*, io.entgra.device.mgt.core.device.mgt.extensions.logger.spi, - io.entgra.device.mgt.core.notification.logger.* + io.entgra.device.mgt.core.notification.logger.*, + com.google.auth.oauth2.* + + google-auth-library-oauth2-http;scope=compile|runtime, + google-http-client;scope=compile|runtime, + grpc-context;scope=compile|runtime, + guava;scope=compile|runtime, + opencensus;scope=compile|runtime, + failureaccess;scope=compile|runtime + + true diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMBasedPushNotificationProvider.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMBasedPushNotificationProvider.java index 1dbbacd720f..2f849a43664 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMBasedPushNotificationProvider.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMBasedPushNotificationProvider.java @@ -32,7 +32,9 @@ public class FCMBasedPushNotificationProvider implements PushNotificationProvide @Override public NotificationStrategy getNotificationStrategy(PushNotificationConfig config) { - return new FCMNotificationStrategy(config); + FCMNotificationStrategy fcmNotificationStrategy = new FCMNotificationStrategy(config); + fcmNotificationStrategy.init(); + return fcmNotificationStrategy; } } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index 669edd314a4..f0039833608 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -18,13 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm; import com.google.auth.oauth2.GoogleCredentials; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.hazelcast.aws.utility.Environment; -import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.OperationManagerImpl; -import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; -import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceConnectivityLoggerImpl; +import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; +import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata; +import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Device; @@ -34,28 +31,34 @@ import io.entgra.device.mgt.core.device.mgt.common.push.notification.Notificatio import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationConfig; import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException; import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder; +import org.wso2.carbon.utils.CarbonUtils; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.Arrays; import java.util.List; +import java.util.Properties; public class FCMNotificationStrategy implements NotificationStrategy { private static final Log log = LogFactory.getLog(FCMNotificationStrategy.class); - private static final String NOTIFIER_TYPE_FCM = "FCM"; private static final String FCM_TOKEN = "FCM_TOKEN"; - private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send"; private static final String FCM_API_KEY = "fcmAPIKey"; - private static final int TIME_TO_LIVE = 2419199; // 1 second less that 28 days + private static final int TIME_TO_LIVE = 2419199; // 1 second less than 28 days private static final int HTTP_STATUS_CODE_OK = 200; private final PushNotificationConfig config; + private static final String FCM_SERVICE_ACCOUNT_PATH = CarbonUtils.getCarbonHome() + File.separator + + "repository" + File.separator + "resources" + File.separator + "service-account.json"; + private static final String[] FCM_SCOPES = { "https://www.googleapis.com/auth/firebase.messaging" }; + private static final String FCM_ENDPOINT_KEY = "FCM_SERVER_ENDPOINT"; + private Properties contextMetadataProperties; + private volatile GoogleCredentials defaultApplication; public FCMNotificationStrategy(PushNotificationConfig config) { this.config = config; @@ -63,23 +66,25 @@ public class FCMNotificationStrategy implements NotificationStrategy { @Override public void init() { - + initContextConfigs(); + initDefaultOAuthApplication(); } @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { - String token = getFcmOauthToken(); try { if (NOTIFIER_TYPE_FCM.equals(config.getType())) { Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService() .getDeviceWithTypeProperties(ctx.getDeviceId()); if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) { - this.sendWakeUpCall(ctx.getOperation().getCode(), device, token); + defaultApplication.refresh(); + sendWakeUpCall(defaultApplication.getAccessToken().getTokenValue(), + getFCMToken(device.getProperties())); } } else { if (log.isDebugEnabled()) { log.debug("Not using FCM notifier as notifier type is set to " + config.getType() + - " in Platform Configurations."); + " in Platform Configurations."); } } } catch (DeviceManagementException e) { @@ -89,100 +94,93 @@ public class FCMNotificationStrategy implements NotificationStrategy { } } - private String getFcmOauthToken() { - GoogleCredentials googleCredentials = null; - try { - googleCredentials = GoogleCredentials - .fromStream(new FileInputStream("/etc/service-account.json")) - .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging")); - googleCredentials.refresh(); - if (null != googleCredentials) { - writeLog("========= Google Credentials created " + googleCredentials.getAccessToken()); - } else { - writeLog("========= Google Credentials is null"); + private void initDefaultOAuthApplication() { + if (defaultApplication == null) { + synchronized (FCMNotificationStrategy.class) { + if (defaultApplication == null) { + Path serviceAccountPath = Paths.get(FCM_SERVICE_ACCOUNT_PATH); + try { + this.defaultApplication = GoogleCredentials. + fromStream(Files.newInputStream(serviceAccountPath)). + createScoped(FCM_SCOPES); + } catch (IOException e) { + log.error("Fail to initialize default OAuth application for FCM communication"); + throw new IllegalStateException(e); + } + } } - return googleCredentials.getAccessToken().getTokenValue(); - } catch (IOException e) { - log.error("Error occurred while getting the FCM OAuth token.", e); - throw new RuntimeException(e); } } - @Override - public NotificationContext buildContext() { - return null; + private void initContextConfigs() { + PushNotificationConfiguration pushNotificationConfiguration = DeviceConfigurationManager.getInstance(). + getDeviceManagementConfig().getPushNotificationConfiguration(); + List contextMetadata = pushNotificationConfiguration.getContextMetadata(); + Properties properties = new Properties(); + if (contextMetadata != null) { + for (ContextMetadata metadata : contextMetadata) { + properties.setProperty(metadata.getKey(), metadata.getValue()); + } + } + contextMetadataProperties = properties; } - @Override - public void undeploy() { + private void sendWakeUpCall(String accessToken, String registrationId) throws IOException, + PushNotificationExecutionFailedException { + OutputStream os = null; + HttpURLConnection conn = null; - } + String fcmServerEndpoint = contextMetadataProperties.getProperty(FCM_ENDPOINT_KEY); + if(fcmServerEndpoint == null) { + String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined"; + log.error(msg); + throw new PushNotificationExecutionFailedException(msg); + } + + try { + byte[] bytes = getFCMRequest(registrationId).getBytes(); + URL url = new URL(fcmServerEndpoint); + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Authorization", "Bearer " + accessToken); + conn.setRequestMethod("POST"); + conn.setDoOutput(true); + + os = conn.getOutputStream(); + os.write(bytes); - private void sendWakeUpCall(String message, Device device, String token) throws IOException, - PushNotificationExecutionFailedException { - if (device.getProperties() != null) { - writeLog("===== Calling senWakeupCall " + device); - 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", "Bearer " + token); - conn.setRequestMethod("POST"); - conn.setDoOutput(true); - os = conn.getOutputStream(); - os.write(bytes); - } finally { - if (os != null) { - os.close(); - } - if (conn != null) { - conn.disconnect(); - } - } int status = conn.getResponseCode(); - if (log.isDebugEnabled()) { - log.debug("Result code: " + status + ", Message: " + conn.getResponseMessage()); + if (status != 200) { + log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage()); + } + } finally { + if (os != null) { + os.close(); } - if (status != HTTP_STATUS_CODE_OK) { - throw new PushNotificationExecutionFailedException("Push notification sending failed with the HTTP " + - "error code '" + status + "'"); + if (conn != null) { + conn.disconnect(); } } } - private static String getFCMRequest(String message, String registrationId) { - JsonObject fcmRequest = new JsonObject(); + private static String getFCMRequest(String registrationId) { JsonObject messageObject = new JsonObject(); messageObject.addProperty("token", registrationId); - JsonObject notification = new JsonObject(); - notification.addProperty("title", "FCM Message"); - notification.addProperty("body", message); - messageObject.add("notification", notification); - fcmRequest.add("message", messageObject); - - /*fcmRequest.addProperty("delay_while_idle", false); - fcmRequest.addProperty("time_to_live", TIME_TO_LIVE); - fcmRequest.addProperty("priority", "high"); + JsonObject fcmRequest = new JsonObject(); + fcmRequest.add("message", messageObject); - //Add message to FCM request - JsonObject data = new JsonObject(); - if (message != null && !message.isEmpty()) { - data.addProperty("data", message); - fcmRequest.add("data", data); - } + return fcmRequest.toString(); + } - //Set device reg-id - JsonArray regIds = new JsonArray(); - regIds.add(new JsonPrimitive(registrationId)); + @Override + public NotificationContext buildContext() { + return null; + } - fcmRequest.add("registration_ids", regIds);*/ + @Override + public void undeploy() { - writeLog("========= FCM Request " + fcmRequest); - return fcmRequest.toString(); } private static String getFCMToken(List properties) { @@ -196,21 +194,8 @@ public class FCMNotificationStrategy implements NotificationStrategy { return fcmToken; } - private static void writeLog(String message) { - try (FileWriter fw = new FileWriter("/opt/entgra/migration/entgra-uem-ultimate-6.0.3.0/log.txt", true); - BufferedWriter bw = new BufferedWriter(fw)) { - bw.write(message); - bw.newLine(); - bw.flush(); - } catch (IOException e) { - e.printStackTrace(); - } - } - @Override public PushNotificationConfig getConfig() { return config; } - - } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java new file mode 100644 index 00000000000..9f894747661 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java @@ -0,0 +1,30 @@ +package io.entgra.device.mgt.core.device.mgt.core.config.push.notification; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +@XmlRootElement(name = "ContextMetadata") +public class ContextMetadata { + private String key; + private String value; + + @XmlAttribute(name = "key") + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/PushNotificationConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/PushNotificationConfiguration.java index 90c6639cb1b..0d64e45cdde 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/PushNotificationConfiguration.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/PushNotificationConfiguration.java @@ -33,6 +33,7 @@ public class PushNotificationConfiguration { private int schedulerTaskInitialDelay; private boolean schedulerTaskEnabled; private List pushNotificationProviders; + private List contextMetadata; @XmlElement(name = "SchedulerBatchSize", required = true) public int getSchedulerBatchSize() { @@ -79,4 +80,14 @@ public class PushNotificationConfiguration { public void setPushNotificationProviders(List pushNotificationProviders) { this.pushNotificationProviders = pushNotificationProviders; } + + @XmlElementWrapper(name = "ProviderContextMetadata") + @XmlElement(name = "ContextMetadata", required = true) + public List getContextMetadata() { + return contextMetadata; + } + + public void setContextMetadata(List contextMetadata) { + this.contextMetadata = contextMetadata; + } } diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 index 59e026f6793..2d5f7639f80 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 @@ -48,6 +48,11 @@ {% endfor %} {% endif %} + {% if device_mgt_conf.push_notification_conf.fcm_server_endpoint is defined %} + + {{device_mgt_conf.push_notification_conf.fcm_server_endpoint}} + + {% endif %} {% if device_mgt_conf.pull_notification_conf is defined %} diff --git a/pom.xml b/pom.xml index 516bd8e9826..2c7c6c6af35 100644 --- a/pom.xml +++ b/pom.xml @@ -405,7 +405,11 @@ ${io.entgra.device.mgt.core.version} - + + org.wso2.orbit.com.google.auth-library-oauth2-http + google-auth-library-oauth2-http + 1.20.0.wso2v1 + org.wso2.carbon @@ -1921,6 +1925,46 @@ google-auth-library-oauth2-http 1.20.0 + + org.wso2.orbit.com.google.http-client + google-http-client + ${com.google.http.client.version} + + + org.wso2.orbit.com.google.auth-library-oauth2-http + google-auth-library-oauth2-http + ${com.google.auth.library.auth2.http.version} + + + org.wso2.orbit.io.opencensus + opencensus + ${io.opencensus.version} + + + io.opencensus + opencensus-api + ${io.opencensus.api.version} + + + io.opencensus + opencensus-contrib-http-util + ${io.opencensus.contrib.http.util.version} + + + org.wso2.orbit.io.grpc + grpc-context + ${io.grpc.context.version} + + + com.google.http-client + google-http-client-gson + ${com.google.http.client.gson.version} + + + com.google.guava + failureaccess + ${com.google.failureaccess.version} + @@ -2311,6 +2355,15 @@ 4.3.1.wso2v1 1.4.199.wso2v1 1.1.3 + + 1.20.0.wso2v1 + 1.41.2.wso2v2 + 1.0.1 + 1.43.3 + 1.27.2.wso2v1 + 0.30.0.wso2v1 + 0.30.0 + 0.30.0 From b1d3503e8b3505907116d1fe4ead9f97f9bb99f1 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Sun, 16 Jun 2024 18:11:19 +0530 Subject: [PATCH 3/6] Add improvements to Google API token generation in FCM --- .../provider/fcm/FCMNotificationStrategy.java | 44 ++-------- .../provider/fcm/util/FCMUtil.java | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index f0039833608..033ef302938 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -22,6 +22,7 @@ import com.google.gson.JsonObject; import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata; import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration; +import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util.FCMUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Device; @@ -53,12 +54,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { private static final int TIME_TO_LIVE = 2419199; // 1 second less than 28 days private static final int HTTP_STATUS_CODE_OK = 200; private final PushNotificationConfig config; - private static final String FCM_SERVICE_ACCOUNT_PATH = CarbonUtils.getCarbonHome() + File.separator + - "repository" + File.separator + "resources" + File.separator + "service-account.json"; - private static final String[] FCM_SCOPES = { "https://www.googleapis.com/auth/firebase.messaging" }; private static final String FCM_ENDPOINT_KEY = "FCM_SERVER_ENDPOINT"; - private Properties contextMetadataProperties; - private volatile GoogleCredentials defaultApplication; public FCMNotificationStrategy(PushNotificationConfig config) { this.config = config; @@ -66,8 +62,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { @Override public void init() { - initContextConfigs(); - initDefaultOAuthApplication(); + } @Override @@ -77,8 +72,8 @@ public class FCMNotificationStrategy implements NotificationStrategy { Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService() .getDeviceWithTypeProperties(ctx.getDeviceId()); if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) { - defaultApplication.refresh(); - sendWakeUpCall(defaultApplication.getAccessToken().getTokenValue(), + FCMUtil.getInstance().getDefaultApplication().refresh(); + sendWakeUpCall(FCMUtil.getInstance().getDefaultApplication().getAccessToken().getTokenValue(), getFCMToken(device.getProperties())); } } else { @@ -94,43 +89,14 @@ public class FCMNotificationStrategy implements NotificationStrategy { } } - private void initDefaultOAuthApplication() { - if (defaultApplication == null) { - synchronized (FCMNotificationStrategy.class) { - if (defaultApplication == null) { - Path serviceAccountPath = Paths.get(FCM_SERVICE_ACCOUNT_PATH); - try { - this.defaultApplication = GoogleCredentials. - fromStream(Files.newInputStream(serviceAccountPath)). - createScoped(FCM_SCOPES); - } catch (IOException e) { - log.error("Fail to initialize default OAuth application for FCM communication"); - throw new IllegalStateException(e); - } - } - } - } - } - private void initContextConfigs() { - PushNotificationConfiguration pushNotificationConfiguration = DeviceConfigurationManager.getInstance(). - getDeviceManagementConfig().getPushNotificationConfiguration(); - List contextMetadata = pushNotificationConfiguration.getContextMetadata(); - Properties properties = new Properties(); - if (contextMetadata != null) { - for (ContextMetadata metadata : contextMetadata) { - properties.setProperty(metadata.getKey(), metadata.getValue()); - } - } - contextMetadataProperties = properties; - } private void sendWakeUpCall(String accessToken, String registrationId) throws IOException, PushNotificationExecutionFailedException { OutputStream os = null; HttpURLConnection conn = null; - String fcmServerEndpoint = contextMetadataProperties.getProperty(FCM_ENDPOINT_KEY); + String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties().getProperty(FCM_ENDPOINT_KEY); if(fcmServerEndpoint == null) { String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined"; log.error(msg); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java new file mode 100644 index 00000000000..66212fc49b5 --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java @@ -0,0 +1,80 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util; + +import com.google.auth.oauth2.GoogleCredentials; +import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; +import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata; +import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration; +import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.FCMNotificationStrategy; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.utils.CarbonUtils; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; + +public class FCMUtil { + + private static final Log log = LogFactory.getLog(FCMUtil.class); + private static volatile FCMUtil instance; + private static GoogleCredentials defaultApplication; + private static final String FCM_SERVICE_ACCOUNT_PATH = CarbonUtils.getCarbonHome() + File.separator + + "repository" + File.separator + "resources" + File.separator + "service-account.json"; + private static final String[] FCM_SCOPES = { "https://www.googleapis.com/auth/firebase.messaging" }; + private Properties contextMetadataProperties; + + private FCMUtil() { + initContextConfigs(); + initDefaultOAuthApplication(); + } + + private void initDefaultOAuthApplication() { + if (defaultApplication == null) { + Path serviceAccountPath = Paths.get(FCM_SERVICE_ACCOUNT_PATH); + try { + defaultApplication = GoogleCredentials. + fromStream(Files.newInputStream(serviceAccountPath)). + createScoped(FCM_SCOPES); + } catch (IOException e) { + log.error("Fail to initialize default OAuth application for FCM communication"); + throw new IllegalStateException(e); + } + } + } + + private void initContextConfigs() { + PushNotificationConfiguration pushNotificationConfiguration = DeviceConfigurationManager.getInstance(). + getDeviceManagementConfig().getPushNotificationConfiguration(); + List contextMetadata = pushNotificationConfiguration.getContextMetadata(); + Properties properties = new Properties(); + if (contextMetadata != null) { + for (ContextMetadata metadata : contextMetadata) { + properties.setProperty(metadata.getKey(), metadata.getValue()); + } + } + contextMetadataProperties = properties; + } + + public static FCMUtil getInstance() { + if (instance == null) { + synchronized (FCMUtil.class) { + if (instance == null) { + instance = new FCMUtil(); + } + } + } + return instance; + } + + public GoogleCredentials getDefaultApplication() { + return defaultApplication; + } + + public Properties getContextMetadataProperties() { + return contextMetadataProperties; + } +} From c38aea743bb6b69e62fda0cf4fb2c63f508857c6 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Tue, 25 Jun 2024 17:30:33 +0530 Subject: [PATCH 4/6] Add missing license headers --- .../notification/provider/fcm/util/FCMUtil.java | 17 +++++++++++++++++ .../push/notification/ContextMetadata.java | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java index 66212fc49b5..11cef37c0fe 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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 io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util; import com.google.auth.oauth2.GoogleCredentials; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java index 9f894747661..a5ead67f0a9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/push/notification/ContextMetadata.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.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 io.entgra.device.mgt.core.device.mgt.core.config.push.notification; import javax.xml.bind.annotation.XmlAttribute; From 23d31fd38c8f7cc24e7d3740ab109dbc3ebb1ab7 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 5 Jul 2024 20:56:03 +0530 Subject: [PATCH 5/6] Fix code review suggestions --- .../provider/fcm/FCMNotificationStrategy.java | 35 +++++++++---------- .../provider/fcm/util/FCMUtil.java | 6 ++++ pom.xml | 12 +++---- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index 033ef302938..39bd1ba7e52 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -17,11 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm; -import com.google.auth.oauth2.GoogleCredentials; import com.google.gson.JsonObject; -import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; -import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata; -import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration; import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util.FCMUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -32,18 +28,12 @@ import io.entgra.device.mgt.core.device.mgt.common.push.notification.Notificatio import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationConfig; import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException; import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder; -import org.wso2.carbon.utils.CarbonUtils; -import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; -import java.util.Properties; public class FCMNotificationStrategy implements NotificationStrategy { @@ -90,13 +80,19 @@ public class FCMNotificationStrategy implements NotificationStrategy { } - + /** + * Send FCM message to the FCM server to initiate the push notification + * @param accessToken Access token to authenticate with the FCM server + * @param registrationId Registration ID of the device + * @throws IOException If an error occurs while sending the request + * @throws PushNotificationExecutionFailedException If an error occurs while sending the push notification + */ private void sendWakeUpCall(String accessToken, String registrationId) throws IOException, PushNotificationExecutionFailedException { - OutputStream os = null; HttpURLConnection conn = null; - String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties().getProperty(FCM_ENDPOINT_KEY); + String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties() + .getProperty(FCM_ENDPOINT_KEY); if(fcmServerEndpoint == null) { String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined"; log.error(msg); @@ -112,23 +108,26 @@ public class FCMNotificationStrategy implements NotificationStrategy { conn.setRequestMethod("POST"); conn.setDoOutput(true); - os = conn.getOutputStream(); - os.write(bytes); + try (OutputStream os = conn.getOutputStream()) { + os.write(bytes); + } int status = conn.getResponseCode(); if (status != 200) { log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage()); } } finally { - if (os != null) { - os.close(); - } if (conn != null) { conn.disconnect(); } } } + /** + * Get the FCM request as a JSON string + * @param registrationId Registration ID of the device + * @return FCM request as a JSON string + */ private static String getFCMRequest(String registrationId) { JsonObject messageObject = new JsonObject(); messageObject.addProperty("token", registrationId); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java index 11cef37c0fe..f7520801f67 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java @@ -76,6 +76,12 @@ public class FCMUtil { contextMetadataProperties = properties; } + /** + * Get the instance of FCMUtil. FCMUtil is a singleton class which should not be + * instantiating more than once. Instantiating the class requires to read the service account file from + * the filesystem and instantiation of the GoogleCredentials object which are costly operations. + * @return FCMUtil instance + */ public static FCMUtil getInstance() { if (instance == null) { synchronized (FCMUtil.class) { diff --git a/pom.xml b/pom.xml index 2c7c6c6af35..0120cb38cbd 100644 --- a/pom.xml +++ b/pom.xml @@ -405,11 +405,6 @@ ${io.entgra.device.mgt.core.version} - - org.wso2.orbit.com.google.auth-library-oauth2-http - google-auth-library-oauth2-http - 1.20.0.wso2v1 - org.wso2.carbon @@ -1923,7 +1918,7 @@ com.google.auth google-auth-library-oauth2-http - 1.20.0 + ${com.google.auth.library.auth2.http.version} org.wso2.orbit.com.google.http-client @@ -1933,7 +1928,7 @@ org.wso2.orbit.com.google.auth-library-oauth2-http google-auth-library-oauth2-http - ${com.google.auth.library.auth2.http.version} + ${com.google.auth.library.wso2.auth2.http.version} org.wso2.orbit.io.opencensus @@ -2356,7 +2351,8 @@ 1.4.199.wso2v1 1.1.3 - 1.20.0.wso2v1 + 1.20.0.wso2v1 + 1.20.0 1.41.2.wso2v2 1.0.1 1.43.3 From 529d1aec01e8a0e904b67b3235f0523e91718b44 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 5 Jul 2024 21:05:07 +0530 Subject: [PATCH 6/6] Add doc comments --- .../push/notification/provider/fcm/util/FCMUtil.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java index f7520801f67..0c6c433cc7c 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/util/FCMUtil.java @@ -57,12 +57,17 @@ public class FCMUtil { fromStream(Files.newInputStream(serviceAccountPath)). createScoped(FCM_SCOPES); } catch (IOException e) { - log.error("Fail to initialize default OAuth application for FCM communication"); - throw new IllegalStateException(e); + String msg = "Fail to initialize default OAuth application for FCM communication"; + log.error(msg); + throw new IllegalStateException(msg, e); } } } + /** + * Initialize the context metadata properties from the cdm-config.xml. This file includes the fcm server URL + * to be invoked when sending the wakeup call to the device. + */ private void initContextConfigs() { PushNotificationConfiguration pushNotificationConfiguration = DeviceConfigurationManager.getInstance(). getDeviceManagementConfig().getPushNotificationConfiguration();