From 23d31fd38c8f7cc24e7d3740ab109dbc3ebb1ab7 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 5 Jul 2024 20:56:03 +0530 Subject: [PATCH] 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 033ef30293..39bd1ba7e5 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 11cef37c0f..f7520801f6 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 2c7c6c6af3..0120cb38cb 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