Search in sources :

Example 21 with JSONStringer

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

the class SoundBoard method doDBDecr.

private void doDBDecr(WebSocket webSocket, String id, String table, String key, String value) {
    JSONStringer jsonObject = new JSONStringer();
    try {
        Quorrabot.instance().getDataStore().decr(table, key, Integer.parseInt(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 22 with JSONStringer

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

the class SoundBoard method doDBKeysQuery.

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

Example 23 with JSONStringer

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

the class SoundBoard method doDBIncr.

private void doDBIncr(WebSocket webSocket, String id, String table, String key, String value) {
    JSONStringer jsonObject = new JSONStringer();
    try {
        Quorrabot.instance().getDataStore().incr(table, key, Integer.parseInt(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 24 with JSONStringer

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

the class SoundBoard method doDBDelKey.

private void doDBDelKey(WebSocket webSocket, String id, String table, String key) {
    JSONStringer jsonObject = new JSONStringer();
    try {
        Quorrabot.instance().getDataStore().del(table, key);
    } 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 25 with JSONStringer

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

the class HTTPSServer method handleDBQuery.

/* Query List:
     *
     * table=tableName&getKeys       - Get list of keys.
     * table=tableName&getData=key   - Get a specific row of data.
     * table=tableName&tableExists   - Return if the table exists or not.
     * table=tableName&keyExists=key - Return if a key exists in a table or not.
     */
private void handleDBQuery(String uriPath, String[] uriQueryList, HttpExchange exchange, Boolean hasPassword) {
    JSONStringer jsonObject = new JSONStringer();
    String[] keyValue;
    String dbTable = null;
    Boolean dbExists;
    if (!hasPassword) {
        jsonObject.object().key("error").value("access denied").endObject();
        sendHTMLError(403, jsonObject.toString(), exchange);
        return;
    }
    if (uriQueryList == null) {
        jsonObject.object().key("error").value("bad request").endObject();
        sendHTMLError(400, jsonObject.toString(), exchange);
        return;
    }
    for (String uriQuery : uriQueryList) {
        keyValue = uriQuery.split("=");
        if (keyValue[0].equals("table")) {
            if (keyValue[1] == null) {
                sendHTMLError(400, "Bad Request", exchange);
                return;
            }
            if (!Quorrabot.instance().getDataStore().FileExists(keyValue[1])) {
                jsonObject.object().key("error").value("table does not exist").endObject();
                sendHTMLError(400, jsonObject.toString(), exchange);
                return;
            }
            dbTable = keyValue[1];
        } else {
            // All other commands need the table.
            if (dbTable == null) {
                jsonObject.object().key("error").value("table not provided").endObject();
                sendHTMLError(400, jsonObject.toString(), exchange);
                return;
            }
        }
        // { "table" : { "table_name": "tableName", "exists" : true } }
        if (keyValue[0].equals("tableExists")) {
            dbExists = Quorrabot.instance().getDataStore().FileExists(dbTable);
            jsonObject.object().key("table");
            jsonObject.object();
            jsonObject.key("table_name").value(dbTable);
            jsonObject.key("exists").value(dbExists);
            jsonObject.endObject();
            jsonObject.endObject();
            sendData("text/text", jsonObject.toString(), exchange);
            return;
        }
        // { "table" : { "table_name": "tableName", "key" : "keyString", "keyExists": true } } 
        if (keyValue[0].equals("keyExists")) {
            if (keyValue.length > 1) {
                dbExists = Quorrabot.instance().getDataStore().exists(dbTable, keyValue[1]);
                jsonObject.object().key("table");
                jsonObject.object();
                jsonObject.key("table_name").value(dbTable);
                jsonObject.key("key").value(keyValue[1]);
                jsonObject.key("keyExists").value(dbExists);
                jsonObject.endObject();
                jsonObject.endObject();
                sendData("text/text", jsonObject.toString(), exchange);
                return;
            } else {
                jsonObject.object().key("error").value("key not provided").endObject();
                sendHTMLError(400, jsonObject.toString(), exchange);
                return;
            }
        }
        // { "table" : { "table_name": "tableName", "key" : "keyString", "value": "valueString" } }
        if (keyValue[0].equals("getData")) {
            if (keyValue.length > 1) {
                String dbString = Quorrabot.instance().getDataStore().GetString(dbTable, "", keyValue[1]);
                jsonObject.object().key("table");
                jsonObject.object();
                jsonObject.key("table_name").value(dbTable);
                jsonObject.key("key").value(keyValue[0]);
                jsonObject.key("value").value(dbString);
                jsonObject.endObject();
                jsonObject.endObject();
                sendData("text/text", jsonObject.toString(), exchange);
                return;
            } else {
                jsonObject.object().key("error").value("key not provided").endObject();
                sendHTMLError(400, jsonObject.toString(), exchange);
                return;
            }
        }
        // { "table" : { "table_name": "tableName", "keylist" : [ { "key" : "keyString" } ] } }
        if (keyValue[0].equals("getKeys")) {
            jsonObject.object();
            jsonObject.key("table");
            jsonObject.object();
            jsonObject.key("table_name").value(dbTable);
            jsonObject.key("keylist").array();
            String[] dbKeys = Quorrabot.instance().getDataStore().GetKeyList(dbTable, "");
            for (String dbKey : dbKeys) {
                jsonObject.object();
                jsonObject.key("key").value(dbKey);
                jsonObject.endObject();
            }
            jsonObject.endArray();
            jsonObject.endObject();
            jsonObject.endObject();
            sendData("text/text", jsonObject.toString(), exchange);
            return;
        }
    }
    jsonObject.object().key("error").value("malformed request").endObject();
    sendHTMLError(400, jsonObject.toString(), exchange);
    return;
}
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