Search in sources :

Example 31 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class JsonUtil method toMap.

private static Object toMap(Object value, Class<?> parameterType) throws GfJsonException {
    try {
        if (value instanceof JSONObject) {
            JSONObject obj = (JSONObject) value;
            Iterator iterator = obj.keys();
            Map map = new HashMap();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                Object elem;
                elem = obj.get(key);
                if (isPrimitiveOrWrapper(elem.getClass())) {
                    map.put(key, elem);
                } else
                    throw new GfJsonException("Only primitive types are supported in map type for input commands");
            }
            return map;
        } else
            throw new GfJsonException("Expected JSONObject for Map. Retrieved type is " + value.getClass());
    } catch (JSONException e) {
        throw new GfJsonException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Iterator(java.util.Iterator) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 32 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class DataCommandResult method buildTable.

private int buildTable(TabularResultData table, int startCount, int endCount) {
    // Three steps:
    // 1a. Convert each row object to a Json object.
    // 1b. Build a list of keys that are used for each object
    // 2. Pad MISSING_VALUE into Json objects for those data that are missing any particular key
    // 3. Build the table from these Json objects.
    // 1.
    int lastRowExclusive = Math.min(selectResult.size(), endCount + 1);
    List<SelectResultRow> paginatedRows = selectResult.subList(startCount, lastRowExclusive);
    List<GfJsonObject> tableRows = new ArrayList<>();
    List<GfJsonObject> rowsWithRealJsonObjects = new ArrayList<>();
    Set<String> columns = new HashSet<>();
    for (SelectResultRow row : paginatedRows) {
        GfJsonObject object = new GfJsonObject();
        try {
            if (row.value == null || MISSING_VALUE.equals(row.value)) {
                object.put("Value", MISSING_VALUE);
            } else if (row.type == ROW_TYPE_PRIMITIVE) {
                object.put(RESULT_FLAG, row.value);
            } else {
                object = buildGfJsonFromRawObject(row.value);
                rowsWithRealJsonObjects.add(object);
                object.keys().forEachRemaining(columns::add);
            }
            tableRows.add(object);
        } catch (GfJsonException e) {
            JSONObject errJson = new JSONObject().put("Value", "Error getting bean properties " + e.getMessage());
            tableRows.add(new GfJsonObject(errJson, false));
        }
    }
    // 2.
    for (GfJsonObject tableRow : rowsWithRealJsonObjects) {
        for (String key : columns) {
            if (!tableRow.has(key)) {
                try {
                    tableRow.put(key, MISSING_VALUE);
                } catch (GfJsonException e) {
                    // TODO: Address this unlikely possibility.
                    logger.warn("Ignored GfJsonException:", e);
                }
            }
        }
    }
    // 3.
    for (GfJsonObject jsonObject : tableRows) {
        addJSONObjectToTable(table, jsonObject);
    }
    return paginatedRows.size();
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) HashSet(java.util.HashSet)

Example 33 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class GemfireDataCommandsDUnitTest method validateJSONGetResult.

private void validateJSONGetResult(CommandResult cmdResult, String[] expectedCols) {
    CompositeResultData rd = (CompositeResultData) cmdResult.getResultData();
    SectionResultData section = rd.retrieveSectionByIndex(0);
    TabularResultData table = section.retrieveTableByIndex(0);
    GfJsonArray array = table.getHeaders();
    assertEquals(expectedCols.length, array.size());
    try {
        for (String col : expectedCols) {
            boolean found = false;
            getLogWriter().info("Validating column " + col);
            for (int i = 0; i < array.size(); i++) {
                String header = (String) array.get(i);
                if (col.equals(header))
                    found = true;
            }
            assertEquals(true, found);
        }
    } catch (GfJsonException e) {
        fail("Error accessing table data", e);
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)

Example 34 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class GemfireDataCommandsDUnitTest method validateJSONGetResultValues.

private void validateJSONGetResultValues(CommandResult cmdResult, String[] expectedCols) {
    CompositeResultData rd = (CompositeResultData) cmdResult.getResultData();
    SectionResultData section = rd.retrieveSectionByIndex(0);
    TabularResultData table = section.retrieveTableByIndex(0);
    GfJsonArray array = table.getHeaders();
    assertEquals(expectedCols.length, array.size());
    try {
        for (String col : expectedCols) {
            boolean found = false;
            getLogWriter().info("Validating column " + col);
            for (int i = 0; i < array.size(); i++) {
                String header = (String) array.get(i);
                if (col.equals(header))
                    found = true;
            }
            assertEquals(true, found);
            List<String> values = table.retrieveAllValues(col);
            for (String value : values) {
                assertNotSame("null", value);
            }
        }
    } catch (GfJsonException e) {
        fail("Error accessing table data", e);
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)

Aggregations

GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)34 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)27 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)14 JSONObject (org.json.JSONObject)9 ArrayList (java.util.ArrayList)6 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)4 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 HashMap (java.util.HashMap)3 SelectResultRow (org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow)3 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)3 SectionResultData (org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)2