From a287d31829e425c685e0ebc5e3ab32f1dc66b891 Mon Sep 17 00:00:00 2001 From: charitha Date: Sat, 10 Feb 2018 22:18:21 +0530 Subject: [PATCH] Fixed https://github.com/wso2/product-iots/issues/1671 --- .../mobile/android/impl/fcm/FCMResult.java | 53 ----- .../mobile/android/impl/fcm/FCMService.java | 64 ------ .../mgt/mobile/android/impl/fcm/FCMUtil.java | 204 ------------------ .../AndroidDeviceManagementDataHolder.java | 9 - ...droidDeviceManagementServiceComponent.java | 30 --- pom.xml | 2 +- 6 files changed, 1 insertion(+), 361 deletions(-) delete mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMResult.java delete mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMService.java delete mode 100644 components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMUtil.java diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMResult.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMResult.java deleted file mode 100644 index 530a56e30a..0000000000 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMResult.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.device.mgt.mobile.android.impl.fcm; - -/** - * Represents model object for holding FCM response data. - */ -public class FCMResult { - - private String errorMsg; - private String msg; - private int statusCode; - - public String getErrorMsg() { - return errorMsg; - } - - public void setErrorMsg(String errorMsg) { - this.errorMsg = errorMsg; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public int getStatusCode() { - return statusCode; - } - - public void setStatusCode(int statusCode) { - this.statusCode = statusCode; - } -} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMService.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMService.java deleted file mode 100644 index 15978f7a9d..0000000000 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMService.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.device.mgt.mobile.android.impl.fcm; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; - -import java.util.ArrayList; -import java.util.List; - -/** - * FCM notification service implementation for Android platform. - */ -public class FCMService { - - private static final Log log = LogFactory.getLog(FCMService.class); - private static final String NOTIFIER_TYPE = "notifierType"; - private static final String FCM_NOTIFIER_CODE = "2"; - - public boolean isFCMEnabled() { - String notifierType = FCMUtil.getConfigurationProperty(NOTIFIER_TYPE); - if (FCM_NOTIFIER_CODE.equals(notifierType)) { - return true; - } - return false; - } - - public void sendNotification(String messageData, Device device) { - List devices = new ArrayList<>(1); - devices.add(device); - FCMResult result = FCMUtil.sendWakeUpCall(messageData, devices); - if (result.getStatusCode() != 200) { - log.error("Exception occurred while sending the FCM notification : " + result.getErrorMsg()); - } - } - - public void sendNotification(String messageData, List devices) { - FCMResult result = FCMUtil.sendWakeUpCall(messageData, devices); - if (result.getStatusCode() != 200) { - log.error("Exception occurred while sending the FCM notification : " + result.getErrorMsg()); - } - } - - public void resetTenantConfigCache() { - FCMUtil.resetTenantConfigCache(); - } -} \ No newline at end of file diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMUtil.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMUtil.java deleted file mode 100644 index c561022684..0000000000 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/fcm/FCMUtil.java +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.device.mgt.mobile.android.impl.fcm; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; -import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -import org.wso2.carbon.device.mgt.mobile.android.impl.util.AndroidPluginConstants; -import org.wso2.carbon.device.mgt.mobile.android.internal.AndroidDeviceManagementDataHolder; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.ProtocolException; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -/** - * Implements utility methods used by FCMService. - */ -public class FCMUtil { - - private static final Log log = LogFactory.getLog(FCMService.class); - - private final static String FCM_ENDPOINT = "https://fcm.googleapis.com/fcm/send"; - private static final String FCM_API_KEY = "fcmAPIKey"; - private static final int TIME_TO_LIVE = 60; - private static final int HTTP_STATUS_CODE_OK = 200; - - private static HashMap tenantConfigurationCache = new HashMap<>(); - - public static FCMResult sendWakeUpCall(String message, List devices) { - FCMResult result = new FCMResult(); - - byte[] bytes = getFCMRequest(message, getFCMTokens(devices)).getBytes(); - HttpURLConnection conn; - try { - conn = (HttpURLConnection) (new URL(FCM_ENDPOINT)).openConnection(); - conn.setDoOutput(true); - conn.setUseCaches(false); - conn.setFixedLengthStreamingMode(bytes.length); - conn.setRequestMethod("POST"); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Authorization", "key=" + getConfigurationProperty(FCM_API_KEY)); - - OutputStream out = conn.getOutputStream(); - out.write(bytes); - out.close(); - - int status = conn.getResponseCode(); - result.setStatusCode(status); - if (status != HTTP_STATUS_CODE_OK) { - result.setErrorMsg(getString(conn.getErrorStream())); - } else { - result.setMsg(getString(conn.getInputStream())); - } - } catch (ProtocolException e) { - log.error("Exception occurred while setting the HTTP protocol.", e); - } catch (IOException ex) { - log.error("Exception occurred while sending the FCM request.", ex); - } - - return result; - } - - private static String getString(InputStream stream) throws IOException { - if (stream != null) { - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - StringBuilder content = new StringBuilder(); - - String newLine; - do { - newLine = reader.readLine(); - if (newLine != null) { - content.append(newLine).append('\n'); - } - } while (newLine != null); - - if (content.length() > 0) { - content.setLength(content.length() - 1); - } - - return content.toString(); - } - return null; - } - - private static String getFCMRequest(String message, List registrationIds) { - JsonObject fcmRequest = new JsonObject(); - fcmRequest.addProperty("delay_while_idle", false); - fcmRequest.addProperty("time_to_live", TIME_TO_LIVE); - - //Add message to FCM request - JsonObject data = new JsonObject(); - if (message != null && !message.isEmpty()) { - data.addProperty("data", message); - fcmRequest.add("data", data); - } - - //Set device reg-ids - JsonArray regIds = new JsonArray(); - for (String regId : registrationIds) { - if (regId == null || regId.isEmpty()) { - continue; - } - regIds.add(new JsonPrimitive(regId)); - } - - fcmRequest.add("registration_ids", regIds); - return fcmRequest.toString(); - } - - private static List getFCMTokens(List devices) { - List tokens = new ArrayList<>(devices.size()); - for (Device device : devices) { - tokens.add(getFCMToken(device.getProperties())); - } - return tokens; - } - - private static String getFCMToken(List properties) { - String fcmToken = null; - for (Device.Property property : properties) { - if (AndroidPluginConstants.FCM_TOKEN.equals(property.getName())) { - fcmToken = property.getValue(); - break; - } - } - return fcmToken; - } - - public static String getConfigurationProperty(String property) { - DeviceManagementService androidDMService = AndroidDeviceManagementDataHolder.getInstance(). - getAndroidDeviceManagementService(); - try { - //Get the TenantConfiguration from cache if not we'll get it from DM service - PlatformConfiguration tenantConfiguration = getTenantConfigurationFromCache(); - if (tenantConfiguration == null) { - tenantConfiguration = androidDMService.getDeviceManager().getConfiguration(); - if (tenantConfiguration != null) { - addTenantConfigurationToCache(tenantConfiguration); - } - } - - if (tenantConfiguration != null) { - List configs = tenantConfiguration.getConfiguration(); - for (ConfigurationEntry entry : configs) { - if (property.equals(entry.getName())) { - return (String) entry.getValue(); - } - } - } - return ""; - } catch (DeviceManagementException e) { - log.error("Exception occurred while fetching the tenant-config.",e); - } - return null; - } - - public static void resetTenantConfigCache() { - tenantConfigurationCache.remove(getTenantId()); - } - - private static void addTenantConfigurationToCache(PlatformConfiguration tenantConfiguration) { - tenantConfigurationCache.put(getTenantId(), tenantConfiguration); - } - - private static PlatformConfiguration getTenantConfigurationFromCache() { - return tenantConfigurationCache.get(getTenantId()); - } - - private static int getTenantId() { - return CarbonContext.getThreadLocalCarbonContext().getTenantId(); - } -} \ No newline at end of file diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementDataHolder.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementDataHolder.java index 028c6aeda3..94efdfbf7c 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementDataHolder.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementDataHolder.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.mobile.android.internal; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -import org.wso2.carbon.device.mgt.mobile.android.impl.fcm.FCMService; import org.wso2.carbon.registry.core.service.RegistryService; /** @@ -29,7 +28,6 @@ public class AndroidDeviceManagementDataHolder { private RegistryService registryService; private DeviceManagementService androidDeviceManagementService; - private FCMService fcmService; private static AndroidDeviceManagementDataHolder thisInstance = new AndroidDeviceManagementDataHolder(); @@ -57,11 +55,4 @@ public class AndroidDeviceManagementDataHolder { this.androidDeviceManagementService = androidDeviceManagementService; } - public FCMService getFCMService() { - return fcmService; - } - - public void setFCMService(FCMService fcmService) { - this.fcmService = fcmService; - } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementServiceComponent.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementServiceComponent.java index c1ecae0a48..84fbb3c17c 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementServiceComponent.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/internal/AndroidDeviceManagementServiceComponent.java @@ -20,12 +20,9 @@ package org.wso2.carbon.device.mgt.mobile.android.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.mobile.android.impl.AndroidDeviceManagementService; -import org.wso2.carbon.device.mgt.mobile.android.impl.fcm.FCMService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -49,8 +46,6 @@ import org.wso2.carbon.registry.core.service.RegistryService; public class AndroidDeviceManagementServiceComponent { private static final Log log = LogFactory.getLog(AndroidDeviceManagementServiceComponent.class); - private ServiceRegistration androidServiceRegRef; - private ServiceRegistration fcmServiceRegRef; protected void activate(ComponentContext ctx) { @@ -58,28 +53,9 @@ public class AndroidDeviceManagementServiceComponent { log.debug("Activating Android Mobile Device Management Service Component"); } try { - BundleContext bundleContext = ctx.getBundleContext(); - DeviceManagementService androidDeviceManagementService = new AndroidDeviceManagementService(); - FCMService fcmService = new FCMService(); - -// androidServiceRegRef = -// bundleContext.registerService(DeviceManagementService.class.getName(), -// androidDeviceManagementService, null); - - fcmServiceRegRef = - bundleContext.registerService(FCMService.class.getName(), fcmService, null); - - - // Policy management service - -// bundleContext.registerService(PolicyMonitoringManager.class, -// new AndroidPolicyMonitoringManager(), null); - AndroidDeviceManagementDataHolder.getInstance().setAndroidDeviceManagementService( androidDeviceManagementService); - AndroidDeviceManagementDataHolder.getInstance().setFCMService(fcmService); - if (log.isDebugEnabled()) { log.debug("Android Mobile Device Management Service Component has been successfully activated"); } @@ -93,12 +69,6 @@ public class AndroidDeviceManagementServiceComponent { log.debug("De-activating Android Mobile Device Management Service Component"); } try { - if (androidServiceRegRef != null) { - androidServiceRegRef.unregister(); - } - if (fcmServiceRegRef != null) { - fcmServiceRegRef.unregister(); - } if (log.isDebugEnabled()) { log.debug( "Android Mobile Device Management Service Component has been successfully de-activated"); diff --git a/pom.xml b/pom.xml index de405b4d64..5a6479aaba 100644 --- a/pom.xml +++ b/pom.xml @@ -1215,7 +1215,7 @@ 1.1.1 - 3.0.219 + 3.0.224 [3.0.0, 4.0.0)