From 55d97dd3355f5c9432e7c9d70f63377df270b4de Mon Sep 17 00:00:00 2001 From: Ace Date: Tue, 26 Jan 2016 08:41:04 +0530 Subject: [PATCH] Implementing null checks --- .../impl/transport/ArduinoMQTTConnector.java | 80 ++++++++++--------- .../api/util/DigitalDisplayMQTTConnector.java | 40 +++++----- .../transport/RaspberryPiMQTTConnector.java | 54 +++++++------ .../VirtualFireAlarmMQTTConnector.java | 78 +++++++++--------- 4 files changed, 130 insertions(+), 122 deletions(-) diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java index 60676875a3..6c6c1a9668 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java @@ -88,58 +88,60 @@ public class ArduinoMQTTConnector extends MQTTTransportHandler { @Override public void processIncomingMessage(MqttMessage message, String... messageParams) throws TransportHandlerException { - String topic = messageParams[0]; - // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. - String ownerAndId = topic.replace(serverName + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + ArduinoConstants.DEVICE_TYPE + File.separator, ":"); + if(messageParams.length != 0) { + String topic = messageParams[0]; + // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. + String ownerAndId = topic.replace(serverName + File.separator, ""); + ownerAndId = ownerAndId.replace(File.separator + ArduinoConstants.DEVICE_TYPE + File.separator, ":"); - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; - if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); - } + if (log.isDebugEnabled()) { + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); + } - int lastIndex = message.toString().lastIndexOf(":"); - String msgContext = message.toString().substring(lastIndex + 1); + int lastIndex = message.toString().lastIndexOf(":"); + String msgContext = message.toString().substring(lastIndex + 1); - LinkedList deviceControlList; - LinkedList replyMessageList; + LinkedList deviceControlList; + LinkedList replyMessageList; - if (msgContext.equals(MESSAGE_TO_SEND) || msgContext.equals(ArduinoConstants.STATE_ON) || msgContext.equals( - ArduinoConstants.STATE_OFF)) { + if (msgContext.equals(MESSAGE_TO_SEND) || msgContext.equals(ArduinoConstants.STATE_ON) || msgContext.equals( + ArduinoConstants.STATE_OFF)) { - if (log.isDebugEnabled()) { - log.debug("Received a control message: "); - log.debug("Control message topic: " + topic); - log.debug("Control message: " + message.toString()); - } + if (log.isDebugEnabled()) { + log.debug("Received a control message: "); + log.debug("Control message topic: " + topic); + log.debug("Control message: " + message.toString()); + } - synchronized (ArduinoControllerService.getInternalControlsQueue()) { - deviceControlList = ArduinoControllerService.getInternalControlsQueue().get(deviceId); - if (deviceControlList == null) { - ArduinoControllerService.getInternalControlsQueue() - .put(deviceId, deviceControlList = new LinkedList()); + synchronized (ArduinoControllerService.getInternalControlsQueue()) { + deviceControlList = ArduinoControllerService.getInternalControlsQueue().get(deviceId); + if (deviceControlList == null) { + ArduinoControllerService.getInternalControlsQueue() + .put(deviceId, deviceControlList = new LinkedList()); + } } - } - deviceControlList.add(message.toString()); + deviceControlList.add(message.toString()); - } else if (msgContext.equals(MESSAGE_RECEIVED)) { + } else if (msgContext.equals(MESSAGE_RECEIVED)) { - if (log.isDebugEnabled()) { - log.debug("Received reply from a device: "); - log.debug("Reply message topic: " + topic); - log.debug("Reply message: " + message.toString().substring(0, lastIndex)); - } + if (log.isDebugEnabled()) { + log.debug("Received reply from a device: "); + log.debug("Reply message topic: " + topic); + log.debug("Reply message: " + message.toString().substring(0, lastIndex)); + } - synchronized (ArduinoControllerService.getReplyMsgQueue()) { - replyMessageList = ArduinoControllerService.getReplyMsgQueue().get(deviceId); - if (replyMessageList == null) { - ArduinoControllerService.getReplyMsgQueue() - .put(deviceId, replyMessageList = new LinkedList<>()); + synchronized (ArduinoControllerService.getReplyMsgQueue()) { + replyMessageList = ArduinoControllerService.getReplyMsgQueue().get(deviceId); + if (replyMessageList == null) { + ArduinoControllerService.getReplyMsgQueue() + .put(deviceId, replyMessageList = new LinkedList<>()); + } } + replyMessageList.add(message.toString()); } - replyMessageList.add(message.toString()); } } diff --git a/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/controller/api/util/DigitalDisplayMQTTConnector.java b/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/controller/api/util/DigitalDisplayMQTTConnector.java index d30328daca..dbc8419f24 100644 --- a/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/controller/api/util/DigitalDisplayMQTTConnector.java +++ b/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/controller/api/util/DigitalDisplayMQTTConnector.java @@ -65,28 +65,30 @@ public class DigitalDisplayMQTTConnector extends MQTTTransportHandler { @Override public void processIncomingMessage(MqttMessage message, String... messageParams) { - String topic = messageParams[0]; - String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + DigitalDisplayConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "digital_display_publisher", ""); - - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; - String[] messageData = message.toString().split(":"); - - if (log.isDebugEnabled()){ - log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); - } + if(messageParams.length != 0) { + String topic = messageParams[0]; + String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, ""); + ownerAndId = ownerAndId.replace(File.separator + DigitalDisplayConstants.DEVICE_TYPE + File.separator, ":"); + ownerAndId = ownerAndId.replace(File.separator + "digital_display_publisher", ""); + + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; + String[] messageData = message.toString().split(":"); + + if (log.isDebugEnabled()) { + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); + } - if (messageData.length == 3) { - String randomId = messageData[0]; - String requestMessage = messageData[1]; - String result = messageData[2]; + if (messageData.length == 3) { + String randomId = messageData[0]; + String requestMessage = messageData[1]; + String result = messageData[2]; - if(log.isDebugEnabled()){ - log.debug("Return result " + result + " for Request " + requestMessage); + if (log.isDebugEnabled()) { + log.debug("Return result " + result + " for Request " + requestMessage); + } + DigitalDisplayWebSocketServerEndPoint.sendMessage(randomId, result); } - DigitalDisplayWebSocketServerEndPoint.sendMessage(randomId, result); } } diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java index 19a2c08dfa..5c7e18ebff 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java @@ -86,37 +86,39 @@ public class RaspberryPiMQTTConnector extends MQTTTransportHandler { @Override public void processIncomingMessage(MqttMessage message, String... messageParams) throws TransportHandlerException { - String topic = messageParams[0]; - // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. - String ownerAndId = topic.replace(serverName + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + RaspberrypiConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); - - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; - String receivedMessage = message.toString(); - - if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); - log.debug("Message [" + receivedMessage + "] topic: [" + topic + "]"); - } - - if (receivedMessage.contains("PUBLISHER")) { - float temperature = Float.parseFloat(receivedMessage.split(":")[2]); + if(messageParams.length != 0) { + String topic = messageParams[0]; + // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. + String ownerAndId = topic.replace(serverName + File.separator, ""); + ownerAndId = ownerAndId.replace(File.separator + RaspberrypiConstants.DEVICE_TYPE + File.separator, ":"); + ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); - if (!RaspberrypiServiceUtils.publishToDAS(owner, deviceId, temperature)) { - log.error("MQTT Subscriber: Publishing data to DAS failed."); - } + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; + String receivedMessage = message.toString(); if (log.isDebugEnabled()) { - log.debug("MQTT Subscriber: Published data to DAS successfully."); + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); + log.debug("Message [" + receivedMessage + "] topic: [" + topic + "]"); } - } else if (receivedMessage.contains("TEMPERATURE")) { - String temperatureValue = receivedMessage.split(":")[1]; - SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE, - temperatureValue, - Calendar.getInstance().getTimeInMillis()); + if (receivedMessage.contains("PUBLISHER")) { + float temperature = Float.parseFloat(receivedMessage.split(":")[2]); + + if (!RaspberrypiServiceUtils.publishToDAS(owner, deviceId, temperature)) { + log.error("MQTT Subscriber: Publishing data to DAS failed."); + } + + if (log.isDebugEnabled()) { + log.debug("MQTT Subscriber: Published data to DAS successfully."); + } + + } else if (receivedMessage.contains("TEMPERATURE")) { + String temperatureValue = receivedMessage.split(":")[1]; + SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE, + temperatureValue, + Calendar.getInstance().getTimeInMillis()); + } } } diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/controller/service/impl/transport/VirtualFireAlarmMQTTConnector.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/controller/service/impl/transport/VirtualFireAlarmMQTTConnector.java index 8782317909..9adaf23d9b 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/controller/service/impl/transport/VirtualFireAlarmMQTTConnector.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/controller/service/impl/transport/VirtualFireAlarmMQTTConnector.java @@ -122,54 +122,56 @@ public class VirtualFireAlarmMQTTConnector extends MQTTTransportHandler { */ @Override public void processIncomingMessage(MqttMessage mqttMessage, String... messageParams) { - String topic = messageParams[0]; - // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. - String ownerAndId = topic.replace(serverName + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + VirtualFireAlarmConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); + if(messageParams.length != 0) { + String topic = messageParams[0]; + // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. + String ownerAndId = topic.replace(serverName + File.separator, ""); + ownerAndId = ownerAndId.replace(File.separator + VirtualFireAlarmConstants.DEVICE_TYPE + File.separator, ":"); + ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; - if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); - } - - String actualMessage; - try { - // the hash-code of the deviceId is used as the alias for device certificates during SCEP enrollment. - // hence, the same is used here to fetch the device-specific-certificate from the key store. - PublicKey clientPublicKey = VirtualFireAlarmServiceUtils.getDevicePublicKey(deviceId); - PrivateKey serverPrivateKey = SecurityManager.getServerPrivateKey(); - - // the MQTT-messages from VirtualFireAlarm devices are in the form {"Msg":, "Sig":} - actualMessage = VirtualFireAlarmServiceUtils.extractMessageFromPayload(mqttMessage.toString(), - serverPrivateKey, clientPublicKey); if (log.isDebugEnabled()) { - log.debug("MQTT: Received Message [" + actualMessage + "] topic: [" + topic + "]"); + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); } - if (actualMessage.contains("PUBLISHER")) { - float temperature = Float.parseFloat(actualMessage.split(":")[2]); - - if (!VirtualFireAlarmServiceUtils.publishToDAS(owner, deviceId, temperature)) { - log.error("MQTT Subscriber: Publishing data to DAS failed."); - } + String actualMessage; + try { + // the hash-code of the deviceId is used as the alias for device certificates during SCEP enrollment. + // hence, the same is used here to fetch the device-specific-certificate from the key store. + PublicKey clientPublicKey = VirtualFireAlarmServiceUtils.getDevicePublicKey(deviceId); + PrivateKey serverPrivateKey = SecurityManager.getServerPrivateKey(); + // the MQTT-messages from VirtualFireAlarm devices are in the form {"Msg":, "Sig":} + actualMessage = VirtualFireAlarmServiceUtils.extractMessageFromPayload(mqttMessage.toString(), + serverPrivateKey, clientPublicKey); if (log.isDebugEnabled()) { - log.debug("MQTT Subscriber: Published data to DAS successfully."); + log.debug("MQTT: Received Message [" + actualMessage + "] topic: [" + topic + "]"); } - } else if (actualMessage.contains("TEMPERATURE")) { - String temperatureValue = actualMessage.split(":")[1]; - SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMP, - temperatureValue, - Calendar.getInstance().getTimeInMillis()); + if (actualMessage.contains("PUBLISHER")) { + float temperature = Float.parseFloat(actualMessage.split(":")[2]); + + if (!VirtualFireAlarmServiceUtils.publishToDAS(owner, deviceId, temperature)) { + log.error("MQTT Subscriber: Publishing data to DAS failed."); + } + + if (log.isDebugEnabled()) { + log.debug("MQTT Subscriber: Published data to DAS successfully."); + } + + } else if (actualMessage.contains("TEMPERATURE")) { + String temperatureValue = actualMessage.split(":")[1]; + SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMP, + temperatureValue, + Calendar.getInstance().getTimeInMillis()); + } + } catch (VirtualFireAlarmException e) { + String errorMsg = + "CertificateManagementService failure oo Signature-Verification/Decryption was unsuccessful."; + log.error(errorMsg, e); } - } catch (VirtualFireAlarmException e) { - String errorMsg = - "CertificateManagementService failure oo Signature-Verification/Decryption was unsuccessful."; - log.error(errorMsg, e); } }