Fix code review suggestions

pull/426/head
Pahansith Gunathilake 3 months ago
parent c38aea743b
commit 23d31fd38c

@ -17,11 +17,7 @@
*/ */
package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm; 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 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 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.Log;
import org.apache.commons.logging.LogFactory; 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.PushNotificationConfig;
import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException; 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 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.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; 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.List;
import java.util.Properties;
public class FCMNotificationStrategy implements NotificationStrategy { 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, private void sendWakeUpCall(String accessToken, String registrationId) throws IOException,
PushNotificationExecutionFailedException { PushNotificationExecutionFailedException {
OutputStream os = null;
HttpURLConnection conn = 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) { if(fcmServerEndpoint == null) {
String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined"; String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined";
log.error(msg); log.error(msg);
@ -112,23 +108,26 @@ public class FCMNotificationStrategy implements NotificationStrategy {
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setDoOutput(true); conn.setDoOutput(true);
os = conn.getOutputStream(); try (OutputStream os = conn.getOutputStream()) {
os.write(bytes); os.write(bytes);
}
int status = conn.getResponseCode(); int status = conn.getResponseCode();
if (status != 200) { if (status != 200) {
log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage()); log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage());
} }
} finally { } finally {
if (os != null) {
os.close();
}
if (conn != null) { if (conn != null) {
conn.disconnect(); 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) { private static String getFCMRequest(String registrationId) {
JsonObject messageObject = new JsonObject(); JsonObject messageObject = new JsonObject();
messageObject.addProperty("token", registrationId); messageObject.addProperty("token", registrationId);

@ -76,6 +76,12 @@ public class FCMUtil {
contextMetadataProperties = properties; 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() { public static FCMUtil getInstance() {
if (instance == null) { if (instance == null) {
synchronized (FCMUtil.class) { synchronized (FCMUtil.class) {

@ -405,11 +405,6 @@
<version>${io.entgra.device.mgt.core.version}</version> <version>${io.entgra.device.mgt.core.version}</version>
</dependency> </dependency>
<!-- End of Conditional Email Access dependencies --> <!-- End of Conditional Email Access dependencies -->
<dependency>
<groupId>org.wso2.orbit.com.google.auth-library-oauth2-http</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.20.0.wso2v1</version>
</dependency>
<!-- Governance dependencies --> <!-- Governance dependencies -->
<dependency> <dependency>
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
@ -1923,7 +1918,7 @@
<dependency> <dependency>
<groupId>com.google.auth</groupId> <groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId> <artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.20.0</version> <version>${com.google.auth.library.auth2.http.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.orbit.com.google.http-client</groupId> <groupId>org.wso2.orbit.com.google.http-client</groupId>
@ -1933,7 +1928,7 @@
<dependency> <dependency>
<groupId>org.wso2.orbit.com.google.auth-library-oauth2-http</groupId> <groupId>org.wso2.orbit.com.google.auth-library-oauth2-http</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId> <artifactId>google-auth-library-oauth2-http</artifactId>
<version>${com.google.auth.library.auth2.http.version}</version> <version>${com.google.auth.library.wso2.auth2.http.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.orbit.io.opencensus</groupId> <groupId>org.wso2.orbit.io.opencensus</groupId>
@ -2356,7 +2351,8 @@
<orbit.h2.version>1.4.199.wso2v1</orbit.h2.version> <orbit.h2.version>1.4.199.wso2v1</orbit.h2.version>
<securevault.version>1.1.3</securevault.version> <securevault.version>1.1.3</securevault.version>
<com.google.auth.library.auth2.http.version>1.20.0.wso2v1</com.google.auth.library.auth2.http.version> <com.google.auth.library.wso2.auth2.http.version>1.20.0.wso2v1</com.google.auth.library.wso2.auth2.http.version>
<com.google.auth.library.auth2.http.version>1.20.0</com.google.auth.library.auth2.http.version>
<com.google.http.client.version>1.41.2.wso2v2</com.google.http.client.version> <com.google.http.client.version>1.41.2.wso2v2</com.google.http.client.version>
<com.google.failureaccess.version>1.0.1</com.google.failureaccess.version> <com.google.failureaccess.version>1.0.1</com.google.failureaccess.version>
<com.google.http.client.gson.version>1.43.3</com.google.http.client.gson.version> <com.google.http.client.gson.version>1.43.3</com.google.http.client.gson.version>

Loading…
Cancel
Save