From 24e353698f0714894ef4e37eb175091b424fb911 Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 16 May 2016 12:12:47 +0530 Subject: [PATCH 1/6] Added permission annotations to virtual fire alarm API --- .../impl/AndroidSenseControllerService.java | 5 +++++ .../impl/AndroidSenseManagerService.java | 5 +++++ .../VirtualFireAlarmControllerService.java | 4 ++-- .../impl/VirtualFireAlarmManagerService.java | 6 ++++++ .../src/main/webapp/META-INF/permissions.xml | 20 +++---------------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java index 43792b859..bbd8358a8 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.androidsense.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; import javax.ws.rs.Consumes; @@ -44,6 +45,7 @@ public interface AndroidSenseControllerService { @Path("device/{deviceId}/words") @POST @Feature(code = "keywords", name = "Add Keywords", description = "Send keywords to the device", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords); /** @@ -56,12 +58,14 @@ public interface AndroidSenseControllerService { @POST @Feature(code = "threshold", name = "Add a Threshold", description = "Set a threshold for word in the device", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold); @Path("device/{deviceId}/words") @DELETE @Feature(code = "remove", name = "Remove Keywords", description = "Remove the keywords", type = "operation") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"}) Response removeKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("words") String words); /** @@ -71,6 +75,7 @@ public interface AndroidSenseControllerService { @GET @Consumes("application/json") @Produces("application/json") + @Permission(scope = "android_sense_device", permissions = {"/permission/admin/device-mgt/user/stats"}) Response getAndroidSenseDeviceStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensor, @QueryParam("from") long from, @QueryParam("to") long to); diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java index 68e48074a..1e03db16e 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.androidsense.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -36,20 +37,24 @@ public interface AndroidSenseManagerService { @Path("/devices/{device_id}") @POST + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices"}) Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName); @Path("/devices/{device_id}") @DELETE + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) Response removeDevice(@PathParam("device_id") String deviceId); @Path("/devices/{device_id}") @PUT + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); @Path("/devices/{device_id}") @GET @Consumes("application/json") @Produces("application/json") + @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) Response getDevice(@PathParam("device_id") String deviceId); } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java index 3bbbe3ad4..30523c444 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java @@ -49,7 +49,7 @@ public interface VirtualFireAlarmControllerService { */ @POST @Path("device/{deviceId}/buzz") - @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"}) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/operation"}) @Feature(code = "buzz", name = "Buzzer On / Off", type = "operation", description = "Switch on/off Virtual Fire Alarm Buzzer. (On / Off)") Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol, @@ -61,7 +61,7 @@ public interface VirtualFireAlarmControllerService { */ @Path("device/stats/{deviceId}") @GET - @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"}) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/stats"}) @Consumes("application/json") @Produces("application/json") Response getVirtualFirealarmStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java index 7b5bdaf09..4b0f3c13f 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import javax.ws.rs.Consumes; @@ -38,28 +39,33 @@ public interface VirtualFireAlarmManagerService { @Path("/devices/{device_id}") @DELETE + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) Response removeDevice(@PathParam("device_id") String deviceId); @Path("/devices/{device_id}") @PUT + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); @Path("/devices/{device_id}") @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) Response getDevice(@PathParam("device_id") String deviceId); @Path("/devices") @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "virtual_firealarm_user", permissions = {"/device-mgt/user/devices/list"}) Response getFirealarmDevices(); @Path("/devices/download") @GET @Produces("application/zip") + @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user"}) Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType); } \ No newline at end of file diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml index 8b8957385..072cd1e74 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml @@ -44,7 +44,7 @@ Download device - /device-mgt/virtual_firealarm/user + /device-mgt/user /enrollment/devices/download GET virtual_firealarm_user @@ -63,32 +63,18 @@ GET virtual_firealarm_user - - Register Device - /device-mgt/user/operations - /device/register/*/*/* - POST - virtual_firealarm_device - Control Buzz - /device-mgt/user/operations + /device-mgt/user/operation /device/*/buzz POST virtual_firealarm_user - - Push Temperature - /device-mgt/user/stats - /device/temperature - POST - virtual_firealarm_device - Get Stats /device-mgt/user/stats /device/stats/* GET - virtual_firealarm_device + virtual_firealarm_user \ No newline at end of file From d7b84cf9e4bd17337af509702d143329fe816cdc Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 16 May 2016 14:14:39 +0530 Subject: [PATCH 2/6] Fixed conflicts --- .../impl/AndroidSenseManagerService.java | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java deleted file mode 100644 index ffb9a2d49..000000000 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2016, 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.iot.androidsense.service.impl; - -import org.wso2.carbon.apimgt.annotations.api.API; -import org.wso2.carbon.apimgt.annotations.api.Permission; - -import javax.ws.rs.*; -import javax.ws.rs.core.Response; -@Path("enrollment") -@API(name = "android_sense_mgt", version = "1.0.0", context = "/android_sense_mgt", tags = {"android_sense"}) -public interface AndroidSenseManagerService { - - @Path("/devices/{device_id}") - @POST - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices"}) - Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName); - - @Path("/devices/{device_id}") - @DELETE - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/remove"}) - Response removeDevice(@PathParam("device_id") String deviceId); - - @Path("/devices/{device_id}") - @PUT - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/update"}) - Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); - - @Path("/devices/{device_id}") - @GET - @Consumes("application/json") - @Produces("application/json") - @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices/list"}) - Response getDevice(@PathParam("device_id") String deviceId); - -} - From 4001dbf8877b68120952c7518d551932b80ccfb7 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 18 May 2016 19:32:39 +0530 Subject: [PATCH 3/6] fixed exponential backup logic --- .../iot/input/adapter/mqtt/util/MQTTAdapterListener.java | 6 +++--- .../input/adapter/mqtt/util/MQTTEventAdapterConstants.java | 2 +- .../iot/input/adapter/xmpp/util/XMPPAdapterListener.java | 6 +++--- .../input/adapter/xmpp/util/XMPPEventAdapterConstants.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java index 305367141..2562a00ef 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -263,11 +263,11 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { @Override public void run() { + int connectionDuration = MQTTEventAdapterConstants.initialReconnectDuration; while (!connectionSucceeded) { try { - MQTTEventAdapterConstants.initialReconnectDuration = MQTTEventAdapterConstants.initialReconnectDuration - * MQTTEventAdapterConstants.reconnectionProgressionFactor; - Thread.sleep(MQTTEventAdapterConstants.initialReconnectDuration); + connectionDuration = connectionDuration * MQTTEventAdapterConstants.reconnectionProgressionFactor; + Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; log.info("MQTT Connection successful"); diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java index 4625c433a..88ac43171 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java @@ -48,7 +48,7 @@ public class MQTTEventAdapterConstants { public static final int ADAPTER_CONF_DEFAULT_KEEP_ALIVE = 60000; public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 4; + public static final int reconnectionProgressionFactor = 2; public static final String EMPTY_STRING = ""; public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer refresh_token"; diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java index bf5c6fde8..b31ac945c 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java @@ -203,10 +203,10 @@ public class XMPPAdapterListener implements Runnable { @Override public void run() { while (!connectionSucceeded) { + int connectionDuration = XMPPEventAdapterConstants.initialReconnectDuration; try { - XMPPEventAdapterConstants.initialReconnectDuration = XMPPEventAdapterConstants.initialReconnectDuration - * XMPPEventAdapterConstants.reconnectionProgressionFactor; - Thread.sleep(XMPPEventAdapterConstants.initialReconnectDuration); + connectionDuration = connectionDuration * XMPPEventAdapterConstants.reconnectionProgressionFactor; + Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; log.info("XMPP Connection successful"); diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java index 304f4578a..d62f78ab2 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java @@ -50,7 +50,7 @@ public class XMPPEventAdapterConstants { public static final int DEFAULT_TIMEOUT_INTERVAL = 5000; public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 4; + public static final int reconnectionProgressionFactor = 2; public static final String DEFAULT = "default"; From 0fee083c4b2205bf218f2e8ad21468dc00599c2b Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 18 May 2016 22:03:16 +0530 Subject: [PATCH 4/6] Added a null check to virtualfire-alarm --- .../service/impl/VirtualFireAlarmServiceImpl.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index bc075f22c..8af28181d 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -62,6 +62,7 @@ import java.util.*; public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { private static final String XMPP_PROTOCOL = "XMPP"; + private static final String MQTT_PROTOCOL = "MQTT"; private static final String KEY_TYPE = "PRODUCTION"; private static ApiApplicationKey apiApplicationKey; private static Log log = LogFactory.getLog(VirtualFireAlarmServiceImpl.class); @@ -70,13 +71,24 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { @Path("device/{deviceId}/buzz") public Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol, @FormParam("state") String state) { + if (state == null || state.isEmpty()) { + log.error("State is not defined for the buzzer operation"); + return Response.status(Response.Status.BAD_REQUEST).build(); + } String switchToState = state.toUpperCase(); if (!switchToState.equals(VirtualFireAlarmConstants.STATE_ON) && !switchToState.equals( VirtualFireAlarmConstants.STATE_OFF)) { log.error("The requested state change shoud be either - 'ON' or 'OFF'"); return Response.status(Response.Status.BAD_REQUEST).build(); } - String protocolString = protocol.toUpperCase(); + String protocolString; + + if (protocol == null || protocol.isEmpty()) { + protocolString = MQTT_PROTOCOL; + } else { + protocolString = protocol.toUpperCase(); + } + if (log.isDebugEnabled()) { log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " + protocolString); From 5b0748489d27de648a7ca3327a1e664b3f23cd43 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 19 May 2016 00:22:01 +0530 Subject: [PATCH 5/6] Improving virtual fire alarm plugin to use newly introduced push notification framework --- .../impl/VirtualFireAlarmServiceImpl.java | 27 ++++-- .../pom.xml | 6 +- .../constants/VirtualFireAlarmConstants.java | 2 + .../impl/VirtualFireAlarmManagerService.java | 19 +++- ...alFirealarmManagementServiceComponent.java | 4 + .../internal/config/DataSourceConfig.java | 41 ++++++++ .../DeviceManagementConfigRepository.java | 40 ++++++++ .../internal/config/JNDILookupDefinition.java | 79 ++++++++++++++++ .../config/PushNotificationConfig.java | 65 +++++++++++++ .../config/VirtualFireAlarmConfig.java | 93 +++++++++++++++++++ .../InvalidConfigurationStateException.java | 53 +++++++++++ ...irtualFireAlarmConfigurationException.java | 53 +++++++++++ .../internal/util/VirtualFireAlarmUtil.java | 43 +++++++++ .../android/util/AndroidAPIUtils.java | 46 ++++----- .../windows/common/util/WindowsAPIUtils.java | 2 +- .../conf/virtual-fire-alarm-config.xml | 40 ++++++++ .../src/main/resources/p2.inf | 1 + 17 files changed, 582 insertions(+), 32 deletions(-) create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DataSourceConfig.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DeviceManagementConfigRepository.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/JNDILookupDefinition.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/PushNotificationConfig.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/VirtualFireAlarmConfig.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/InvalidConfigurationStateException.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/VirtualFireAlarmConfigurationException.java create mode 100644 components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/util/VirtualFireAlarmUtil.java create mode 100644 features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual-fire-alarm-config.xml diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index 0548ff9d6..16304986c 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -34,6 +34,9 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.iot.util.ZipArchive; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException; @@ -65,12 +68,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.PrivateKey; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { @@ -122,6 +120,19 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { dynamicProperties.put(VirtualFireAlarmConstants.ADAPTER_TOPIC_PROPERTY, publishTopic); APIUtil.getOutputEventAdapterService().publish(VirtualFireAlarmConstants.MQTT_ADAPTER_NAME, dynamicProperties, encryptedMsg); + Operation commandOp = new CommandOperation(); + commandOp.setCode("buzz"); + commandOp.setType(Operation.Type.COMMAND); + commandOp.setEnabled(true); + commandOp.setPayLoad(encryptedMsg); + + Properties props = new Properties(); + props.setProperty(VirtualFireAlarmConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic); + commandOp.setProperties(props); + + List deviceIdentifiers = new ArrayList<>(); + deviceIdentifiers.add(new DeviceIdentifier(VirtualFireAlarmConstants.DEVICE_TYPE, deviceId)); + APIUtil.getDeviceManagementService().addOperation(VirtualFireAlarmConstants.DEVICE_TYPE, commandOp, deviceIdentifiers); break; } return Response.ok().build(); @@ -132,6 +143,10 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { String errorMsg = "Preparing Secure payload failed for device - [" + deviceId + "]"; log.error(errorMsg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } catch (OperationManagementException e) { + String msg = "Error occurred while executing command operation upon ringing the buzzer"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/pom.xml b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/pom.xml index 25c1b8b2a..d13a2e1ae 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/pom.xml +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/pom.xml @@ -88,7 +88,11 @@ org.wso2.carbon.device.mgt.analytics.data.publisher.service, org.wso2.carbon.event.input.adapter.core, org.wso2.carbon.event.input.adapter.core.exception, - org.jivesoftware.smack.* + org.jivesoftware.smack.*, + javax.xml.bind, + javax.xml.bind.annotation, + javax.xml.parsers, + org.w3c.dom !org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal, diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java index 693eb3524..e890b1f01 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java @@ -90,4 +90,6 @@ public class VirtualFireAlarmConstants { + "mqtt.properties"; public static final String XMPP_CONFIG_LOCATION = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "xmpp.properties"; + + public static final String MQTT_ADAPTER_TOPIC_PROPERTY_NAME = "mqtt.adapter.topic"; } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/VirtualFireAlarmManagerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/VirtualFireAlarmManagerService.java index 00536d67b..c0b5bd3f2 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/VirtualFireAlarmManagerService.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/VirtualFireAlarmManagerService.java @@ -25,10 +25,15 @@ import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.VirtualFireAlarmConfig; + +import java.util.HashMap; +import java.util.Map; public class VirtualFireAlarmManagerService implements DeviceManagementService { private DeviceManager deviceManager; + private PushNotificationConfig pushNotificationConfig; @Override public String getType() { @@ -38,6 +43,18 @@ public class VirtualFireAlarmManagerService implements DeviceManagementService { @Override public void init() throws DeviceManagementException { this.deviceManager = new VirtualFireAlarmManager(); + this.pushNotificationConfig = this.populatePushNotificationConfig(); + } + + private PushNotificationConfig populatePushNotificationConfig() { + org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.PushNotificationConfig sourceConfig = + VirtualFireAlarmConfig.getInstance().getPushNotificationConfig(); + Map staticProps = new HashMap<>(); + for (org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.PushNotificationConfig.Property + property : sourceConfig.getProperties()) { + staticProps.put(property.getName(), property.getValue()); + } + return new PushNotificationConfig("MQTT", staticProps); } @Override @@ -57,7 +74,7 @@ public class VirtualFireAlarmManagerService implements DeviceManagementService { @Override public PushNotificationConfig getPushNotificationConfig() { - return null; + return pushNotificationConfig; } } diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/VirtualFirealarmManagementServiceComponent.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/VirtualFirealarmManagementServiceComponent.java index c6c8ee64b..303d5cba1 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/VirtualFirealarmManagementServiceComponent.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/VirtualFirealarmManagementServiceComponent.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.VirtualFireAl import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFireAlarmUtils; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFirealarmSecurityManager; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFirealarmStartupListener; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.VirtualFireAlarmConfig; import org.wso2.carbon.event.input.adapter.core.InputEventAdapterService; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; @@ -74,6 +75,9 @@ public class VirtualFirealarmManagementServiceComponent { log.debug("Activating Virtual Firealarm Device Management Service Component"); } try { + /* Initializing Virtual Fire Alarm Configuration */ + VirtualFireAlarmConfig.init(); + VirtualFireAlarmManagerService virtualFireAlarmManagerService = new VirtualFireAlarmManagerService(); BundleContext bundleContext = ctx.getBundleContext(); firealarmServiceRegRef = bundleContext.registerService(DeviceManagementService.class.getName() diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DataSourceConfig.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DataSourceConfig.java new file mode 100644 index 000000000..bdf5de635 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DataSourceConfig.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014, 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.iot.virtualfirealarm.plugin.internal.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding data source configuration in malformed-cdm-config-no-mgt-repo.xml at parsing + * with JAXB. + */ +@XmlRootElement(name = "DataSourceConfiguration") +public class DataSourceConfig { + + private JNDILookupDefinition jndiLookupDefinition; + + @XmlElement(name = "JndiLookupDefinition", required = true) + public JNDILookupDefinition getJndiLookupDefinition() { + return jndiLookupDefinition; + } + + public void setJndiLookupDefinition(JNDILookupDefinition jndiLookupDefinition) { + this.jndiLookupDefinition = jndiLookupDefinition; + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DeviceManagementConfigRepository.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DeviceManagementConfigRepository.java new file mode 100644 index 000000000..a77264649 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/DeviceManagementConfigRepository.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014, 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.iot.virtualfirealarm.plugin.internal.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding management repository data. + */ +@XmlRootElement(name = "ManagementRepository") +public class DeviceManagementConfigRepository { + + private DataSourceConfig dataSourceConfig; + + @XmlElement(name = "DataSourceConfiguration", required = true) + public DataSourceConfig getDataSourceConfig() { + return dataSourceConfig; + } + + public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { + this.dataSourceConfig = dataSourceConfig; + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/JNDILookupDefinition.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/JNDILookupDefinition.java new file mode 100644 index 000000000..07caa0636 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/JNDILookupDefinition.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2014, 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.iot.virtualfirealarm.plugin.internal.config; + +import javax.xml.bind.annotation.*; +import java.util.List; + +/** + * Class for hold JndiLookupDefinition of rss-manager.xml at parsing with JAXB. + */ +@XmlRootElement(name = "JndiLookupDefinition") +public class JNDILookupDefinition { + + private String jndiName; + private List jndiProperties; + + @XmlElement(name = "Name", required = false) + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + + @XmlElementWrapper(name = "Environment", required = false) + @XmlElement(name = "Property", nillable = false) + public List getJndiProperties() { + return jndiProperties; + } + + public void setJndiProperties(List jndiProperties) { + this.jndiProperties = jndiProperties; + } + + @XmlRootElement(name = "Property") + public static class JNDIProperty { + + private String name; + + private String value; + + @XmlAttribute(name = "Name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } + +} + diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/PushNotificationConfig.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/PushNotificationConfig.java new file mode 100644 index 000000000..df04b34a9 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/PushNotificationConfig.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016, 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.iot.virtualfirealarm.plugin.internal.config; + +import javax.xml.bind.annotation.*; +import java.util.List; + +@XmlRootElement(name = "PushNotificationConfiguration") +public class PushNotificationConfig { + + private List properties; + + @XmlElementWrapper(name = "Properties", required = true) + @XmlElement(name = "Property", required = true) + public List getProperties() { + return properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + @XmlRootElement(name = "Property") + public static class Property { + + private String name; + private String value; + + @XmlAttribute(name = "Name", required = true) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/VirtualFireAlarmConfig.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/VirtualFireAlarmConfig.java new file mode 100644 index 000000000..718436e29 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/VirtualFireAlarmConfig.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016, 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.iot.virtualfirealarm.plugin.internal.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.exception.InvalidConfigurationStateException; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.exception.VirtualFireAlarmConfigurationException; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.util.VirtualFireAlarmUtil; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.File; + +@XmlRootElement(name = "DeviceManagementConfiguration") +public class VirtualFireAlarmConfig { + + private DeviceManagementConfigRepository deviceManagementConfigRepository; + private PushNotificationConfig pushNotificationConfig; + private static VirtualFireAlarmConfig config; + + private static final Log log = LogFactory.getLog(VirtualFireAlarmConfig.class); + private static final String VIRTUAL_FIRE_ALARM_CONFIG_PATH = + CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "device-mgt-plugins" + File.separator + + "virtual-fire-alarm" + File.separator + "virtual-fire-alarm-config.xml"; + + private VirtualFireAlarmConfig() { + } + + public static VirtualFireAlarmConfig getInstance() { + if (config == null) { + throw new InvalidConfigurationStateException("Webapp Authenticator Configuration is not " + + "initialized properly"); + } + return config; + } + + @XmlElement(name = "ManagementRepository", required = true) + public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { + return deviceManagementConfigRepository; + } + + public void setDeviceManagementConfigRepository(DeviceManagementConfigRepository deviceManagementConfigRepository) { + this.deviceManagementConfigRepository = deviceManagementConfigRepository; + } + + @XmlElement(name = "PushNotificationConfiguration", required = false) + public PushNotificationConfig getPushNotificationConfig() { + return pushNotificationConfig; + } + + public void setPushNotificationConfig(PushNotificationConfig pushNotificationConfig) { + this.pushNotificationConfig = pushNotificationConfig; + } + + public static void init() throws VirtualFireAlarmConfigurationException { + try { + File authConfig = new File(VirtualFireAlarmConfig.VIRTUAL_FIRE_ALARM_CONFIG_PATH); + Document doc = VirtualFireAlarmUtil.convertToDocument(authConfig); + + /* Un-marshaling Webapp Authenticator configuration */ + JAXBContext ctx = JAXBContext.newInstance(VirtualFireAlarmConfig.class); + Unmarshaller unmarshaller = ctx.createUnmarshaller(); + //unmarshaller.setSchema(getSchema()); + config = (VirtualFireAlarmConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new VirtualFireAlarmConfigurationException("Error occurred while un-marshalling Virtual Fire Alarm " + + " Config", e); + } + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/InvalidConfigurationStateException.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/InvalidConfigurationStateException.java new file mode 100644 index 000000000..6c0961e7c --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/InvalidConfigurationStateException.java @@ -0,0 +1,53 @@ +/* + * 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.iot.virtualfirealarm.plugin.internal.config.exception; + +public class InvalidConfigurationStateException extends RuntimeException { + + private static final long serialVersionUID = -3151279411229070297L; + + public InvalidConfigurationStateException(int errorCode, String message) { + super(message); + } + + public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationStateException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public InvalidConfigurationStateException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationStateException(String msg) { + super(msg); + } + + public InvalidConfigurationStateException() { + super(); + } + + public InvalidConfigurationStateException(Throwable cause) { + super(cause); + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/VirtualFireAlarmConfigurationException.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/VirtualFireAlarmConfigurationException.java new file mode 100644 index 000000000..3b00ba3a8 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/config/exception/VirtualFireAlarmConfigurationException.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, 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.iot.virtualfirealarm.plugin.internal.config.exception; + +public class VirtualFireAlarmConfigurationException extends Exception { + + private static final long serialVersionUID = -3151279431229070297L; + + public VirtualFireAlarmConfigurationException(int errorCode, String message) { + super(message); + } + + public VirtualFireAlarmConfigurationException(int errorCode, String message, Throwable cause) { + super(message, cause); + } + + public VirtualFireAlarmConfigurationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public VirtualFireAlarmConfigurationException(String message, Throwable cause) { + super(message, cause); + } + + public VirtualFireAlarmConfigurationException(String msg) { + super(msg); + } + + public VirtualFireAlarmConfigurationException() { + super(); + } + + public VirtualFireAlarmConfigurationException(Throwable cause) { + super(cause); + } + +} diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/util/VirtualFireAlarmUtil.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/util/VirtualFireAlarmUtil.java new file mode 100644 index 000000000..6d5e51ca6 --- /dev/null +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/internal/util/VirtualFireAlarmUtil.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, 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.iot.virtualfirealarm.plugin.internal.util; + +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.internal.config.exception.VirtualFireAlarmConfigurationException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +public class VirtualFireAlarmUtil { + + public static Document convertToDocument(File file) throws VirtualFireAlarmConfigurationException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new VirtualFireAlarmConfigurationException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); + } + } + + +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java index 0e321b1e0..01ee8d029 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/util/AndroidAPIUtils.java @@ -24,7 +24,6 @@ import com.google.gson.JsonParser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -35,7 +34,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.device.mgt.mobile.android.impl.gcm.GCMService; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -80,16 +78,16 @@ public class AndroidAPIUtils { return deviceManagementProviderService; } - public static GCMService getGCMService() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - GCMService gcmService = (GCMService) ctx.getOSGiService(GCMService.class, null); - if (gcmService == null) { - String msg = "GCM service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } - return gcmService; - } +// public static GCMService getGCMService() { +// PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); +// GCMService gcmService = (GCMService) ctx.getOSGiService(GCMService.class, null); +// if (gcmService == null) { +// String msg = "GCM service has not initialized."; +// log.error(msg); +// throw new IllegalStateException(msg); +// } +// return gcmService; +// } public static MediaType getResponseMediaType(String acceptHeader) { MediaType responseMediaType; @@ -108,17 +106,19 @@ public class AndroidAPIUtils { AndroidDeviceUtils deviceUtils = new AndroidDeviceUtils(); DeviceIDHolder deviceIDHolder = deviceUtils.validateDeviceIdentifiers(deviceIDs, message, responseMediaType); - int status = getDeviceManagementService().addOperation(operation, deviceIDHolder.getValidDeviceIDList()); - if (status > 0) { - GCMService gcmService = getGCMService(); - if (gcmService.isGCMEnabled()) { - List devices = new ArrayList(); - for (DeviceIdentifier deviceIdentifier : deviceIDHolder.getValidDeviceIDList()) { - devices.add(getDeviceManagementService().getDevice(deviceIdentifier)); - } - getGCMService().sendNotification(operation.getCode(), devices); - } - } + + int status = + getDeviceManagementService().addOperation("android", operation, deviceIDHolder.getValidDeviceIDList()); +// if (status > 0) { +// GCMService gcmService = getGCMService(); +// if (gcmService.isGCMEnabled()) { +// List devices = new ArrayList(); +// for (DeviceIdentifier deviceIdentifier : deviceIDHolder.getValidDeviceIDList()) { +// devices.add(getDeviceManagementService().getDevice(deviceIdentifier)); +// } +// getGCMService().sendNotification(operation.getCode(), devices); +// } +// } if (!deviceIDHolder.getErrorDeviceIdList().isEmpty()) { return javax.ws.rs.core.Response.status(AndroidConstants.StatusCodes. MULTI_STATUS_HTTP_CODE).type( diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/mdm/mobileservices/windows/common/util/WindowsAPIUtils.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/mdm/mobileservices/windows/common/util/WindowsAPIUtils.java index 9606e2d32..6b8462e9f 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/mdm/mobileservices/windows/common/util/WindowsAPIUtils.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/mdm/mobileservices/windows/common/util/WindowsAPIUtils.java @@ -134,7 +134,7 @@ public class WindowsAPIUtils { WindowsDeviceUtils deviceUtils = new WindowsDeviceUtils(); DeviceIDHolder deviceIDHolder = deviceUtils.validateDeviceIdentifiers(deviceIDs, message, responseMediaType); - getDeviceManagementService().addOperation(operation, deviceIDHolder.getValidDeviceIDList()); + getDeviceManagementService().addOperation("windows", operation, deviceIDHolder.getValidDeviceIDList()); if (!deviceIDHolder.getErrorDeviceIdList().isEmpty()) { return javax.ws.rs.core.Response.status(PluginConstants.StatusCodes. MULTI_STATUS_HTTP_CODE).type( diff --git a/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual-fire-alarm-config.xml b/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual-fire-alarm-config.xml new file mode 100644 index 000000000..6ffef4eee --- /dev/null +++ b/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/conf/virtual-fire-alarm-config.xml @@ -0,0 +1,40 @@ + + + + + + + + + jdbc/VirtualFireAlarmDM_DB + + + + + + + tcp://${mqtt.broker.host}:${mqtt.broker.port} + admin + https://localhost:${carbon.https.port}/dynamic-client-web/register + 0 + + true + + + diff --git a/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf b/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf index d40a7cdba..d4d1e3586 100644 --- a/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf +++ b/features/iot-plugins-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.feature/src/main/resources/p2.inf @@ -15,6 +15,7 @@ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../featur org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../resources/security/);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm_${feature.version}/certs/,target:${installFolder}/../../resources/security/,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../resources/device-types/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm_${feature.version}/conf/virtual-fire-alarm-config.xml,target:${installFolder}/../../conf/etc/device-mgt-plugins/virtual-fire-alarm/virtual-fire-alarm-config.xml,overwrite:true);\ instructions.unconfigure = \ org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/webapps/virtual_firealarm.war);\ From d7b423d694c59d1ef9883a39caf1343ea51948c1 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 19 May 2016 00:28:08 +0530 Subject: [PATCH 6/6] fixed exponential backup logic --- .../mqtt/util/MQTTAdapterListener.java | 41 ++++++++++--------- .../mqtt/util/MQTTEventAdapterConstants.java | 4 +- .../xmpp/util/XMPPAdapterListener.java | 4 +- .../xmpp/util/XMPPEventAdapterConstants.java | 4 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java index 2562a00ef..ecd1b9345 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -69,7 +69,6 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { private InputEventAdapterListener eventAdapterListener = null; - public MQTTAdapterListener(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, String mqttClientId, InputEventAdapterListener inputEventAdapterListener, int tenantId) { @@ -173,22 +172,26 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON); postMethod.setEntity(requestEntity); HttpResponse httpResponse = httpClient.execute(postMethod); - String response = MQTTUtil.getResponseString(httpResponse); - try { - JSONParser jsonParser = new JSONParser(); - JSONObject jsonPayload = (JSONObject) jsonParser.parse(response); - String clientId = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_ID); - String clientSecret = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_SECRET); - JWTClientManagerService jwtClientManagerService = MQTTUtil.getJWTClientManagerService(); - AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( - clientId, clientSecret, username, scopes); - connectionOptions.setUserName(accessTokenInfo.getAccessToken()); - } catch (ParseException e) { - String msg = "error occurred while parsing client credential payload"; - log.error(msg, e); - } catch (JWTClientException e) { - String msg = "error occurred while parsing the response from JWT Client"; - log.error(msg, e); + if (httpResponse != null) { + String response = MQTTUtil.getResponseString(httpResponse); + try { + if (response != null) { + JSONParser jsonParser = new JSONParser(); + JSONObject jsonPayload = (JSONObject) jsonParser.parse(response); + String clientId = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_ID); + String clientSecret = (String) jsonPayload.get(MQTTEventAdapterConstants.CLIENT_SECRET); + JWTClientManagerService jwtClientManagerService = MQTTUtil.getJWTClientManagerService(); + AccessTokenInfo accessTokenInfo = jwtClientManagerService.getJWTClient().getAccessToken( + clientId, clientSecret, username, scopes); + connectionOptions.setUserName(accessTokenInfo.getAccessToken()); + } + } catch (ParseException e) { + String msg = "error occurred while parsing client credential payload"; + log.error(msg, e); + } catch (JWTClientException e) { + String msg = "error occurred while parsing the response from JWT Client"; + log.error(msg, e); + } } } catch (MalformedURLException e) { log.error("Invalid dcrUrl : " + dcrUrlString); @@ -263,10 +266,10 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { @Override public void run() { - int connectionDuration = MQTTEventAdapterConstants.initialReconnectDuration; + int connectionDuration = MQTTEventAdapterConstants.INITIAL_RECONNECTION_DURATION; while (!connectionSucceeded) { try { - connectionDuration = connectionDuration * MQTTEventAdapterConstants.reconnectionProgressionFactor; + connectionDuration = connectionDuration * MQTTEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR; Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java index 88ac43171..1ac0ee949 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/mqtt/util/MQTTEventAdapterConstants.java @@ -47,8 +47,8 @@ public class MQTTEventAdapterConstants { public static final String ADAPTER_CONF_KEEP_ALIVE = "keepAlive"; public static final int ADAPTER_CONF_DEFAULT_KEEP_ALIVE = 60000; - public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 2; + public static final int INITIAL_RECONNECTION_DURATION = 4000; + public static final int RECONNECTION_PROGRESS_FACTOR = 2; public static final String EMPTY_STRING = ""; public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer refresh_token"; diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java index b31ac945c..168b9e6a8 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPAdapterListener.java @@ -203,9 +203,9 @@ public class XMPPAdapterListener implements Runnable { @Override public void run() { while (!connectionSucceeded) { - int connectionDuration = XMPPEventAdapterConstants.initialReconnectDuration; + int connectionDuration = XMPPEventAdapterConstants.INITIAL_RECONNECTION_DURATION; try { - connectionDuration = connectionDuration * XMPPEventAdapterConstants.reconnectionProgressionFactor; + connectionDuration = connectionDuration * XMPPEventAdapterConstants.RECONNECTION_PROGRESS_FACTOR; Thread.sleep(connectionDuration); startListener(); connectionSucceeded = true; diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java index d62f78ab2..d823d8ba1 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.input.adapter.xmpp/src/main/java/org/wso2/carbon/device/mgt/iot/input/adapter/xmpp/util/XMPPEventAdapterConstants.java @@ -49,8 +49,8 @@ public class XMPPEventAdapterConstants { public static final int DEFAULT_XMPP_PORT = 5222; public static final int DEFAULT_TIMEOUT_INTERVAL = 5000; - public static int initialReconnectDuration = 2000; - public static final int reconnectionProgressionFactor = 2; + public static int INITIAL_RECONNECTION_DURATION = 4000; + public static final int RECONNECTION_PROGRESS_FACTOR = 2; public static final String DEFAULT = "default";