Fix code review suggestions

uddhika-device-mgt
Pahansith Gunathilake 5 months ago
parent c38aea743b
commit 23d31fd38c

@ -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);

@ -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) {

@ -405,11 +405,6 @@
<version>${io.entgra.device.mgt.core.version}</version>
</dependency>
<!-- 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 -->
<dependency>
<groupId>org.wso2.carbon</groupId>
@ -1923,7 +1918,7 @@
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.20.0</version>
<version>${com.google.auth.library.auth2.http.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.orbit.com.google.http-client</groupId>
@ -1933,7 +1928,7 @@
<dependency>
<groupId>org.wso2.orbit.com.google.auth-library-oauth2-http</groupId>
<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>
<groupId>org.wso2.orbit.io.opencensus</groupId>
@ -2356,7 +2351,8 @@
<orbit.h2.version>1.4.199.wso2v1</orbit.h2.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.failureaccess.version>1.0.1</com.google.failureaccess.version>
<com.google.http.client.gson.version>1.43.3</com.google.http.client.gson.version>

Loading…
Cancel
Save