Search in sources :

Example 11 with JSONStringer

use of org.json.JSONStringer in project quorrabot by GloriousEggroll.

the class SoundBoard method handleMessage.

private void handleMessage(WebSocket webSocket, String jsonString) {
    JSONObject jsonObject;
    JSONArray jsonArray;
    wsSession sessionData;
    Boolean authenticated;
    String dataString;
    String uniqueID;
    int dataInt;
    try {
        jsonObject = new JSONObject(jsonString);
    } catch (JSONException ex) {
        com.gmt2001.Console.err.println("SoundBoard: Bad JSON passed [" + jsonString + "]");
        com.gmt2001.Console.err.printStackTrace(ex);
        return;
    } catch (Exception ex) {
        com.gmt2001.Console.err.println("SoundBoard: Exception Occurred");
        com.gmt2001.Console.err.printStackTrace(ex);
        return;
    }
    sessionData = wsSessionMap.get(genSessionKey(webSocket));
    if (jsonObject.has("authenticate")) {
        authenticated = jsonObject.getString("authenticate").equals(authString);
        if (authenticated) {
            sessionData.setAuthenticated(authenticated);
            sessionData.setReadOnly(false);
        } else {
            authenticated = jsonObject.getString("authenticate").equals(authStringRO);
            sessionData.setAuthenticated(authenticated);
            sessionData.setReadOnly(true);
            if (!authenticated) {
                authenticated = authenticateOauth(jsonObject.getString("authenticate"));
                sessionData.setAuthenticated(authenticated);
                sessionData.setReadOnly(false);
            }
        }
        return;
    }
    if (!sessionData.isAuthenticated()) {
        JSONStringer jsonStringer = new JSONStringer();
        jsonStringer.object().key("autherror").value("not authenticated").endObject();
        webSocket.send(jsonStringer.toString());
        return;
    }
    // debugMsg("PanelSocketServer::onMessage(" + jsonString + ")");
    try {
        if (jsonObject.has("command")) {
            dataString = jsonObject.getString("command");
            if (jsonObject.has("username")) {
            //Quorrabot.instance().handleCommand(jsonObject.getString("username"), dataString);
            } else {
            //Quorrabot.instance().handleCommand(Quorrabot.instance().getBotName(), dataString);
            }
            return;
        } else if (jsonObject.has("version")) {
            uniqueID = jsonObject.getString("version");
            doVersion(webSocket, uniqueID);
        } else if (jsonObject.has("dbquery")) {
            uniqueID = jsonObject.getString("dbquery");
            String table = jsonObject.getJSONObject("query").getString("table");
            String key = jsonObject.getJSONObject("query").getString("key");
            doDBQuery(webSocket, uniqueID, table, key);
            return;
        } else if (jsonObject.has("dbkeys")) {
            uniqueID = jsonObject.getString("dbkeys");
            String table = jsonObject.getJSONObject("query").getString("table");
            doDBKeysQuery(webSocket, uniqueID, table);
        } else if (jsonObject.has("dbupdate") && !sessionData.isReadOnly()) {
            uniqueID = jsonObject.getString("dbupdate");
            String table = jsonObject.getJSONObject("update").getString("table");
            String key = jsonObject.getJSONObject("update").getString("key");
            String value = jsonObject.getJSONObject("update").getString("value");
            doDBUpdate(webSocket, uniqueID, table, key, value);
        } else if (jsonObject.has("dbincr") && !sessionData.isReadOnly()) {
            uniqueID = jsonObject.getString("dbincr");
            String table = jsonObject.getJSONObject("incr").getString("table");
            String key = jsonObject.getJSONObject("incr").getString("key");
            String value = jsonObject.getJSONObject("incr").getString("value");
            doDBIncr(webSocket, uniqueID, table, key, value);
        } else if (jsonObject.has("dbdecr") && !sessionData.isReadOnly()) {
            uniqueID = jsonObject.getString("dbdecr");
            String table = jsonObject.getJSONObject("decr").getString("table");
            String key = jsonObject.getJSONObject("decr").getString("key");
            String value = jsonObject.getJSONObject("decr").getString("value");
            doDBDecr(webSocket, uniqueID, table, key, value);
        } else if (jsonObject.has("dbdelkey") && !sessionData.isReadOnly()) {
            uniqueID = jsonObject.getString("dbdelkey");
            String table = jsonObject.getJSONObject("delkey").getString("table");
            String key = jsonObject.getJSONObject("delkey").getString("key");
            doDBDelKey(webSocket, uniqueID, table, key);
        } else if (jsonObject.has("soundboard_hooks") && !sessionData.isReadOnly()) {
            doSoundBoardsUpdate(jsonObject);
        } else {
            com.gmt2001.Console.err.println("SoundBoard: Unknown JSON passed [" + jsonString + "]");
            return;
        }
    } catch (JSONException ex) {
        com.gmt2001.Console.err.println("SoundBoard::JSONException(" + ex.getMessage() + "): " + jsonString);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONStringer(org.json.JSONStringer) IOException(java.io.IOException) JSONException(org.json.JSONException)

Example 12 with JSONStringer

use of org.json.JSONStringer in project quorrabot by GloriousEggroll.

the class SoundBoard method alertImage.

public void alertImage(String imageInfo) {
    JSONStringer jsonObject = new JSONStringer();
    jsonObject.object().key("alert_image").value(imageInfo).endObject();
    debugMsg("alertImage(" + imageInfo + ")");
    sendToAll(jsonObject.toString());
}
Also used : JSONStringer(org.json.JSONStringer)

Example 13 with JSONStringer

use of org.json.JSONStringer in project quorrabot by GloriousEggroll.

the class SoundBoard method doDBQuery.

private void doDBQuery(WebSocket webSocket, String id, String table, String key) {
    JSONStringer jsonObject = new JSONStringer();
    String value = "";
    try {
        value = Quorrabot.instance().getDataStore().GetString(table, "", key);
    } catch (NullPointerException ex) {
        if (!dbCallNull) {
            dbCallNull = true;
            debugMsg("NULL returned from DB. DB Object not created yet.");
        }
        return;
    }
    dbCallNull = false;
    jsonObject.object().key("query_id").value(id).key("results").object();
    jsonObject.key("table").value(table).key(key).value(value).endObject().endObject();
    webSocket.send(jsonObject.toString());
}
Also used : JSONStringer(org.json.JSONStringer)

Example 14 with JSONStringer

use of org.json.JSONStringer in project quorrabot by GloriousEggroll.

the class SoundBoard method doDBUpdate.

private void doDBUpdate(WebSocket webSocket, String id, String table, String key, String value) {
    JSONStringer jsonObject = new JSONStringer();
    try {
        Quorrabot.instance().getDataStore().set(table, key, value);
    } catch (NullPointerException ex) {
        if (!dbCallNull) {
            debugMsg("NULL returned from DB. DB Object not created yet.");
        }
        return;
    }
    jsonObject.object().key("query_id").value(id).endObject();
    webSocket.send(jsonObject.toString());
}
Also used : JSONStringer(org.json.JSONStringer)

Example 15 with JSONStringer

use of org.json.JSONStringer in project quorrabot by GloriousEggroll.

the class SoundBoard method triggerAudioPanel.

public void triggerAudioPanel(String soundHook) {
    JSONStringer jsonObject = new JSONStringer();
    jsonObject.object().key("soundboard_hook").value(soundHook).endObject();
    debugMsg("triggerAudioPanel(" + soundHook + ")");
    sendToAll(jsonObject.toString());
}
Also used : JSONStringer(org.json.JSONStringer)

Aggregations

JSONStringer (org.json.JSONStringer)34 Test (org.junit.Test)16 JSONObject (org.json.JSONObject)12 JSONException (org.json.JSONException)7 ArrayList (java.util.ArrayList)4 NonNull (android.support.annotation.NonNull)2 IOException (java.io.IOException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2 InSequence (org.jboss.arquillian.junit.InSequence)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Log (com.microsoft.appcenter.ingestion.models.Log)1 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 Log (com.microsoft.azure.mobile.ingestion.models.Log)1 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1