From 7f8f6d6f75598a99ab99c7f4a01ae75cbc1b771a Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 9 Aug 2024 09:32:15 +0530 Subject: [PATCH] Add FCM request improvements --- .../pom.xml | 4 ++ .../provider/fcm/FCMNotificationStrategy.java | 42 +++++++++---------- .../provider/fcm/util/FCMUtil.java | 22 ++++++++++ 3 files changed, 45 insertions(+), 23 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 8453f05e9f..a36ec650b6 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 @@ -151,6 +151,10 @@ org.wso2.carbon org.wso2.carbon.utils + + com.squareup.okhttp3 + okhttp + 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 39bd1ba7e5..66ee2ebb00 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 @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provid import com.google.gson.JsonObject; import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util.FCMUtil; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Device; @@ -89,8 +92,6 @@ public class FCMNotificationStrategy implements NotificationStrategy { */ private void sendWakeUpCall(String accessToken, String registrationId) throws IOException, PushNotificationExecutionFailedException { - HttpURLConnection conn = null; - String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties() .getProperty(FCM_ENDPOINT_KEY); if(fcmServerEndpoint == null) { @@ -99,26 +100,21 @@ public class FCMNotificationStrategy implements NotificationStrategy { 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); - - try (OutputStream os = conn.getOutputStream()) { - os.write(bytes); - } - - int status = conn.getResponseCode(); - if (status != 200) { - log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage()); + RequestBody fcmRequest = getFCMRequest(registrationId); + Request request = new Request.Builder() + .url(fcmServerEndpoint) + .post(fcmRequest) + .addHeader("Authorization", "Bearer " + accessToken) + .build(); + try (Response response = FCMUtil.getInstance().getHttpClient().newCall(request).execute()) { + if (log.isDebugEnabled()) { + log.debug("FCM message sent to the FCM server. Response code: " + response.code() + + " Response message : " + response.message()); } - } finally { - if (conn != null) { - conn.disconnect(); + if(!response.isSuccessful()) { + String msg = "Response Status: " + response.code() + ", Response Message: " + response.message(); + log.error(msg); + throw new IOException(msg); } } } @@ -128,14 +124,14 @@ public class FCMNotificationStrategy implements NotificationStrategy { * @param registrationId Registration ID of the device * @return FCM request as a JSON string */ - private static String getFCMRequest(String registrationId) { + private static RequestBody getFCMRequest(String registrationId) { JsonObject messageObject = new JsonObject(); messageObject.addProperty("token", registrationId); JsonObject fcmRequest = new JsonObject(); fcmRequest.add("message", messageObject); - return fcmRequest.toString(); + return RequestBody.create(fcmRequest.toString(), okhttp3.MediaType.parse("application/json")); } @Override 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 0c6c433cc7..e3d15d8974 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 @@ -22,6 +22,8 @@ import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManag 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 okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.utils.CarbonUtils; @@ -33,6 +35,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Properties; +import java.util.concurrent.TimeUnit; public class FCMUtil { @@ -43,10 +46,29 @@ public class FCMUtil { "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 static ConnectionPool connectionPool; + private static OkHttpClient client; private FCMUtil() { initContextConfigs(); initDefaultOAuthApplication(); + initPooledConnection(); + } + + /** + * Initialize the connection pool for the OkHttpClient instance. + */ + private void initPooledConnection() { + connectionPool = new ConnectionPool(25, 1, TimeUnit.MINUTES); + client = new OkHttpClient.Builder().connectionPool(connectionPool).build(); + } + + /** + * Get the Pooled OkHttpClient instance + * @return OkHttpClient instance + */ + public OkHttpClient getHttpClient() { + return client; } private void initDefaultOAuthApplication() {