Search in sources :

Example 6 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project nifi by apache.

the class PublishMQTT method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowfile = session.get();
    if (flowfile == null) {
        return;
    }
    if (mqttClient == null || !mqttClient.isConnected()) {
        logger.info("Was disconnected from client or was never connected, attempting to connect.");
        try {
            reconnect();
        } catch (MqttException e) {
            context.yield();
            session.transfer(flowfile, REL_FAILURE);
            logger.error("MQTT client is disconnected and re-connecting failed. Transferring FlowFile to fail and yielding", e);
            return;
        }
    }
    // get the MQTT topic
    String topic = context.getProperty(PROP_TOPIC).evaluateAttributeExpressions(flowfile).getValue();
    if (topic == null || topic.isEmpty()) {
        logger.warn("Evaluation of the topic property returned null or evaluated to be empty, routing to failure");
        session.transfer(flowfile, REL_FAILURE);
        return;
    }
    // do the read
    final byte[] messageContent = new byte[(int) flowfile.getSize()];
    session.read(flowfile, new InputStreamCallback() {

        @Override
        public void process(final InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, messageContent, true);
        }
    });
    int qos = context.getProperty(PROP_QOS).evaluateAttributeExpressions(flowfile).asInteger();
    final MqttMessage mqttMessage = new MqttMessage(messageContent);
    mqttMessage.setQos(qos);
    mqttMessage.setPayload(messageContent);
    mqttMessage.setRetained(context.getProperty(PROP_RETAIN).evaluateAttributeExpressions(flowfile).asBoolean());
    try {
        mqttClientConnectLock.readLock().lock();
        final StopWatch stopWatch = new StopWatch(true);
        try {
            /*
                 * Underlying method waits for the message to publish (according to set QoS), so it executes synchronously:
                 *     MqttClient.java:361 aClient.publish(topic, message, null, null).waitForCompletion(getTimeToWait());
                 */
            mqttClient.publish(topic, mqttMessage);
        } finally {
            mqttClientConnectLock.readLock().unlock();
        }
        session.getProvenanceReporter().send(flowfile, broker, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(flowfile, REL_SUCCESS);
    } catch (MqttException me) {
        logger.error("Failed to publish message.", me);
        session.transfer(flowfile, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) InputStream(java.io.InputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) IOException(java.io.IOException) StopWatch(org.apache.nifi.util.StopWatch)

Example 7 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class MQTTService method messageArrived.

/**
 * When a message from server arrive
 * @param topic String topic where the message from
 * @param message MqttMessage message content
 * @throws Exception error
 */
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    FlyveLog.d(TAG, "Topic " + topic);
    FlyveLog.d(TAG, "Message " + new String(message.getPayload()));
    String messageBody = new String(message.getPayload());
    storeLog(Helpers.broadCastMessage("MQTT Message", "Body", messageBody));
    if (topic.isEmpty()) {
        // exit if the topic if empty
        return;
    }
    if (messageBody.isEmpty()) {
        // exit if the message if empty
        return;
    }
    // Command/Ping
    if (topic.toLowerCase().contains("ping")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(QUERY) && "Ping".equalsIgnoreCase(jsonObj.getString(QUERY))) {
                policiesController.sendKeepAlive();
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Geolocate
    if (topic.toLowerCase().contains("geolocate")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(QUERY) && "Geolocate".equalsIgnoreCase(jsonObj.getString(QUERY))) {
                policiesController.sendGPS();
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Inventory
    if (topic.toLowerCase().contains("inventory")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(QUERY) && "Inventory".equalsIgnoreCase(jsonObj.getString(QUERY))) {
                policiesController.createInventory();
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Lock
    if (topic.toLowerCase().contains("lock")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has("lock")) {
                String lock = jsonObj.getString("lock");
                policiesController.lockDevice(lock.equalsIgnoreCase("now"));
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Wipe
    if (topic.toLowerCase().contains("wipe")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has("wipe") && "NOW".equalsIgnoreCase(jsonObj.getString("wipe"))) {
                policiesController.wipe();
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Unenroll
    if (topic.toLowerCase().contains("unenroll")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has("unenroll") && "NOW".equalsIgnoreCase(jsonObj.getString("unenroll"))) {
                FlyveLog.d("unroll");
                policiesController.unenroll();
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Command/Subscribe
    if (topic.toLowerCase().contains("subscribe")) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has("subscribe")) {
                JSONArray jsonTopics = jsonObj.getJSONArray("subscribe");
                for (int i = 0; i < jsonTopics.length(); i++) {
                    JSONObject jsonTopic = jsonTopics.getJSONObject(i);
                    String channel = jsonTopic.getString("topic") + "/#";
                    FlyveLog.d(channel);
                    // Add new channel
                    policiesController.subscribe(channel);
                }
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordEnabled
    String PASSWORD_ENABLE = "passwordEnabled";
    if (topic.toLowerCase().contains(PASSWORD_ENABLE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_ENABLE)) {
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordEnabled(taskId);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordEnabled
    String PASSWORD_QUALITY = "passwordQuality";
    if (topic.toLowerCase().contains(PASSWORD_QUALITY.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_QUALITY)) {
                String quality = jsonObj.getString(PASSWORD_QUALITY);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordQuality(taskId, quality);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinLength
    String PASSWORD_MIN_LENGTH = "passwordMinLength";
    if (topic.toLowerCase().contains(PASSWORD_MIN_LENGTH.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_LENGTH)) {
                int length = jsonObj.getInt(PASSWORD_MIN_LENGTH);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinLength(taskId, length);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinLowerCase
    String PASSWORD_MIN_LOWERCASE = "passwordMinLowerCase";
    if (topic.toLowerCase().contains(PASSWORD_MIN_LOWERCASE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_LOWERCASE)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_LOWERCASE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinLowerCase(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinUpperCase
    String PASSWORD_MIN_UPPERCASE = "passwordMinUpperCase";
    if (topic.toLowerCase().contains(PASSWORD_MIN_UPPERCASE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_UPPERCASE)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_UPPERCASE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinUpperCase(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinNonLetter
    String PASSWORD_MIN_NON_LETTER = "passwordMinNonLetter";
    if (topic.toLowerCase().contains(PASSWORD_MIN_NON_LETTER.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_NON_LETTER)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_NON_LETTER);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinNonLetter(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinLetters
    String PASSWORD_MIN_LETTERS = "passwordMinLetters";
    if (topic.toLowerCase().contains(PASSWORD_MIN_LETTERS.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_LETTERS)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_LETTERS);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinLetter(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinNumeric
    String PASSWORD_MIN_NUMERIC = "passwordMinNumeric";
    if (topic.toLowerCase().contains(PASSWORD_MIN_NUMERIC.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_NUMERIC)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_NUMERIC);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinNumeric(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/passwordMinSymbols
    String PASSWORD_MIN_SYMBOLS = "passwordMinSymbols";
    if (topic.toLowerCase().contains(PASSWORD_MIN_SYMBOLS.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(PASSWORD_MIN_SYMBOLS)) {
                int minimum = jsonObj.getInt(PASSWORD_MIN_SYMBOLS);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.passwordMinSymbols(taskId, minimum);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/MaximumFailedPasswordsForWipe
    String MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = "maximumFailedPasswordsForWipe";
    if (topic.toLowerCase().contains(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE)) {
                int max = jsonObj.getInt(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.maximumFailedPasswordsForWipe(taskId, max);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/MaximumTimeToLock
    String MAXIMUM_TIME_TO_LOCK = "maximumTimeToLock";
    if (topic.toLowerCase().contains(MAXIMUM_TIME_TO_LOCK.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(MAXIMUM_TIME_TO_LOCK)) {
                int max = jsonObj.getInt(MAXIMUM_TIME_TO_LOCK);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.maximumTimeToLock(taskId, max);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/storageEncryption
    String STORAGE_ENCRYPTION = "storageEncryption";
    if (topic.toLowerCase().contains(STORAGE_ENCRYPTION.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(STORAGE_ENCRYPTION)) {
                boolean enable = jsonObj.getBoolean(STORAGE_ENCRYPTION);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.storageEncryption(taskId, enable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableCamera
    String DISABLE_CAMERA = "disableCamera";
    if (topic.toLowerCase().contains(DISABLE_CAMERA.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_CAMERA)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_CAMERA);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableCamera(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableBluetooth
    String DISABLE_BLUETOOTH = "disableBluetooth";
    if (topic.toLowerCase().contains(DISABLE_BLUETOOTH.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_BLUETOOTH)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_BLUETOOTH);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableBluetooth(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/deployApp
    String DEPLOY_APP = "deployApp";
    if (topic.toLowerCase().contains(DEPLOY_APP.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DEPLOY_APP)) {
                String deployApp = jsonObj.getString(DEPLOY_APP);
                String id = jsonObj.getString("id");
                String versionCode = jsonObj.getString("versionCode");
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.installPackage(deployApp, id, versionCode, taskId);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/deployApp
    String REMOVE_APP = "removeApp";
    if (topic.toLowerCase().contains(REMOVE_APP.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(REMOVE_APP)) {
                String removeApp = jsonObj.getString(REMOVE_APP);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.removePackage(taskId, removeApp);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/deployFile
    String DEPLOY_FILE = "deployFile";
    if (topic.toLowerCase().contains(DEPLOY_FILE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DEPLOY_FILE)) {
                String deployFile = jsonObj.getString(DEPLOY_FILE);
                String id = jsonObj.getString("id");
                String versionCode = jsonObj.getString("version");
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.downloadFile(deployFile, id, versionCode, taskId);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/deployFile
    String REMOVE_FILE = "removeFile";
    if (topic.toLowerCase().contains(REMOVE_FILE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(REMOVE_FILE)) {
                String removeFile = jsonObj.getString(REMOVE_FILE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.removeFile(taskId, removeFile);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableScreenCapture
    // ROOT REQUIRED
    String DISABLE_SCREEN_CAPTURE = "disableScreenCapture";
    if (topic.toLowerCase().contains(DISABLE_SCREEN_CAPTURE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_SCREEN_CAPTURE)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_SCREEN_CAPTURE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableScreenCapture(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableAirplaneMode
    // ROOT REQUIRED
    String DISABLE_AIRPLANE_MODE = "disableAirplaneMode";
    if (topic.toLowerCase().contains(DISABLE_AIRPLANE_MODE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_AIRPLANE_MODE)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_AIRPLANE_MODE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableAirplaneMode(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableGPS
    // ROOT REQUIRED
    String DISABLE_GPS = "disableGPS";
    if (topic.toLowerCase().contains(DISABLE_GPS.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_GPS)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_GPS);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableGPS(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableHostpotTethering
    String DISABLE_HOSTPOT_TETHERING = "disableHostpotTethering";
    if (topic.toLowerCase().contains(DISABLE_HOSTPOT_TETHERING.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_HOSTPOT_TETHERING)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_HOSTPOT_TETHERING);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableHostpotTethering(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableRoaming
    String DISABLE_ROAMING = "disableRoaming";
    if (topic.toLowerCase().contains(DISABLE_ROAMING.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_ROAMING)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_ROAMING);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableRoaming(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableWifi
    String DISABLE_WIFI = "disableWifi";
    if (topic.toLowerCase().contains(DISABLE_WIFI.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_WIFI)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_WIFI);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableWifi(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/useTLS
    String USE_TLS = "useTLS";
    if (topic.toLowerCase().contains(USE_TLS.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(USE_TLS)) {
                Boolean enable = jsonObj.getBoolean(USE_TLS);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.useTLS(taskId, enable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableMobileLine
    // ROOT
    String DISABLE_MOBILE_LINE = "disableMobileLine";
    if (topic.toLowerCase().contains(DISABLE_MOBILE_LINE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_MOBILE_LINE)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_MOBILE_LINE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableMobileLine(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableNfc
    // ROOT
    String DISABLE_NFC = "disableNfc";
    if (topic.toLowerCase().contains(DISABLE_NFC.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_NFC)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_NFC);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableNFC(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableStatusBar
    // ROOT
    String DISABLE_STATUS_BAR = "disableStatusBar";
    if (topic.toLowerCase().contains(DISABLE_STATUS_BAR.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_STATUS_BAR)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_STATUS_BAR);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableStatusBar(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/resetPassword
    // ROOT
    String RESET_PASSWORD = "resetPassword";
    if (topic.toLowerCase().contains(RESET_PASSWORD.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(RESET_PASSWORD)) {
                Boolean disable = jsonObj.getBoolean(RESET_PASSWORD);
                String taskId = jsonObj.getString("taskId");
            // execute the policy
            // policiesController.resetPassword(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableUsbMtp
    // ROOT
    String DISABLE_USB_MTP = "disableUsbMtp";
    if (topic.toLowerCase().contains(DISABLE_USB_MTP.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_USB_MTP)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_USB_MTP);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableMTPUsbFileTransferProtocols(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableUsbPtp
    // ROOT
    String DISABLE_USB_PTP = "disableUsbPtp";
    if (topic.toLowerCase().contains(DISABLE_USB_PTP.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_USB_PTP)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_USB_PTP);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disablePTPUsbFileTransferProtocols(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableUsbAdb
    // ROOT
    String DISABLE_USB_ADB = "disableUsbAdb";
    if (topic.toLowerCase().contains(DISABLE_USB_ADB.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_USB_ADB)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_USB_ADB);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableADBUsbFileTransferProtocols(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableSpeakerphone
    String DISABLE_SPEAKER_PHONE = "disableSpeakerphone";
    if (topic.toLowerCase().contains(DISABLE_SPEAKER_PHONE.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_SPEAKER_PHONE)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_SPEAKER_PHONE);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableSpeakerphone(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableUsbOnTheGo
    String DISABLE_SMSMMS = "disableSmsMms";
    if (topic.toLowerCase().contains(DISABLE_SMSMMS.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_SMSMMS)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_SMSMMS);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableSmsMms(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
    // Policy/disableCreateVpnProfiles
    String DISABLE_CREATE_VPN_PROFILES = "disableCreateVpnProfiles";
    if (topic.toLowerCase().contains(DISABLE_CREATE_VPN_PROFILES.toLowerCase())) {
        try {
            JSONObject jsonObj = new JSONObject(messageBody);
            if (jsonObj.has(DISABLE_CREATE_VPN_PROFILES)) {
                Boolean disable = jsonObj.getBoolean(DISABLE_CREATE_VPN_PROFILES);
                String taskId = jsonObj.getString("taskId");
                // execute the policy
                policiesController.disableCreateVpnProfiles(taskId, disable);
            }
        } catch (Exception ex) {
            FlyveLog.e(ex.getMessage());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 8 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendGPS.

/**
 * Send the GPS information to MQTT
 * payload: {"latitude":"10.2485486","longitude":"-67.5904498","datetime":1499364642}
 */
public void sendGPS() {
    new FastLocationProvider().getLocation(context, new FastLocationProvider.LocationResult() {

        @Override
        public void gotLocation(Location location) {
            if (location == null) {
                FlyveLog.d("without location yet...");
            } else {
                FlyveLog.d("lat: " + location.getLatitude() + " lon: " + location.getLongitude());
                try {
                    String latitude = String.valueOf(location.getLatitude());
                    String longitude = String.valueOf(location.getLongitude());
                    JSONObject jsonGPS = new JSONObject();
                    jsonGPS.put("latitude", latitude);
                    jsonGPS.put("longitude", longitude);
                    jsonGPS.put("datetime", Helpers.getUnixTime());
                    String topic = mTopic + "/Status/Geolocation";
                    String payload = jsonGPS.toString();
                    byte[] encodedPayload;
                    encodedPayload = payload.getBytes(UTF_8);
                    MqttMessage message = new MqttMessage(encodedPayload);
                    IMqttDeliveryToken token = client.publish(topic, message);
                    // send broadcast
                    broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Send Geolocation", "ID: " + token.getMessageId()));
                } catch (Exception ex) {
                    FlyveLog.e(ex.getMessage());
                    broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on GPS location", ex.getMessage()));
                }
            }
        }
    });
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) FastLocationProvider(org.flyve.mdm.agent.utils.FastLocationProvider) JSONObject(org.json.JSONObject) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) Location(android.location.Location)

Example 9 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendStatusVersion.

/**
 * Send the Status version of the agent
 * payload: {"version": "0.99.0"}
 */
public void sendStatusVersion() {
    String topic = mTopic + "/FlyvemdmManifest/Status/Version";
    String payload = "{\"version\":\"" + BuildConfig.VERSION_NAME + "\"}";
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        IMqttDeliveryToken token = client.publish(topic, message);
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Send Status Version", "ID: " + token.getMessageId()));
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on sendStatusVersion", ex.getMessage()));
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Example 10 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project android-mdm-agent by flyve-mdm.

the class PoliciesController method sendTaskStatus.

public void sendTaskStatus(String taskId, String status) {
    String topic = mTopic + "/Status/Task/" + taskId;
    byte[] encodedPayload;
    try {
        String payload = "{ \"status\": \"" + status + "\" }";
        encodedPayload = payload.getBytes(UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        IMqttDeliveryToken token = client.publish(topic, message);
        // send broadcast
        broadcastReceivedLog(Helpers.broadCastMessage(MQTT_SEND, "Send Inventory", "ID: " + token.getMessageId()));
    } catch (Exception ex) {
        FlyveLog.e(ex.getMessage());
        // send broadcast
        broadcastReceivedLog(Helpers.broadCastMessage(ERROR, "Error on sendKeepAlive", ex.getMessage()));
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)65 MqttException (org.eclipse.paho.client.mqttv3.MqttException)28 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)19 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)16 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)16 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)15 Test (org.junit.Test)14 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 JsonParser (com.google.gson.JsonParser)3 IOException (java.io.IOException)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)3 JSONObject (org.json.JSONObject)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 MISTEdge (edu.snu.mist.common.graph.MISTEdge)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CamelContext (org.apache.camel.CamelContext)2