use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class DataCommandFunction method getJSONForNonPrimitiveObject.
@SuppressWarnings({ "rawtypes" })
public static Object[] getJSONForNonPrimitiveObject(Object obj) {
Object[] array = new Object[2];
if (obj == null) {
array[0] = null;
array[1] = "<NULL>";
return array;
} else {
array[0] = obj.getClass().getCanonicalName();
Class klass = obj.getClass();
if (JsonUtil.isPrimitiveOrWrapper(klass)) {
array[1] = obj;
} else if (obj instanceof PdxInstance) {
String str = pdxToJson((PdxInstance) obj);
array[1] = str;
} else {
GfJsonObject object = new GfJsonObject(obj, true);
Iterator keysIterator = object.keys();
while (keysIterator.hasNext()) {
String key = (String) keysIterator.next();
Object value = object.get(key);
if (GfJsonObject.isJSONKind(value)) {
GfJsonObject jsonVal = new GfJsonObject(value);
// System.out.println("Re-wrote inner object");
try {
if (jsonVal.has("type-class")) {
object.put(key, jsonVal.get("type-class"));
} else {
// Its Map Value
object.put(key, "a Map");
}
} catch (GfJsonException e) {
throw new RuntimeException(e);
}
} else if (value instanceof JSONArray) {
// Its a collection either a set or list
try {
object.put(key, "a Collection");
} catch (GfJsonException e) {
throw new RuntimeException(e);
}
}
}
String str = object.toString();
array[1] = str;
}
return array;
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class CommandResult method buildObjectSection.
private void buildObjectSection(Table table, RowGroup parentRowGroup, GfJsonObject object, int depth) throws GfJsonException {
Iterator<String> keys = object.keys();
RowGroup rowGroup = null;
if (parentRowGroup != null) {
rowGroup = parentRowGroup;
} else {
rowGroup = table.newRowGroup();
}
GfJsonArray nestedCollection = null;
GfJsonObject nestedObject = null;
GfJsonObject fieldDisplayNames = object.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
List<String> fieldsToSkipOnUI = null;
if (object.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
GfJsonArray jsonArray = object.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
;
fieldsToSkipOnUI = new ArrayList<String>();
for (int i = 0; i < jsonArray.size(); i++) {
fieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
}
}
while (keys.hasNext()) {
String key = keys.next();
if (CliJsonSerializable.FIELDS_TO_SKIP.contains(key) || (fieldsToSkipOnUI != null && fieldsToSkipOnUI.contains(key))) {
continue;
}
try {
nestedCollection = object.getJSONArray(key);
} catch (GfJsonException e) {
/* next check if it's a nested object */
}
Object field = null;
if (nestedCollection == null) {
field = object.get(key);
if (!isPrimitiveOrStringOrWrapper(field)) {
nestedObject = object.getJSONObject(key);
}
}
if (nestedCollection != null && isPrimitiveOrStringOrWrapperArray(nestedCollection)) {
String str = nestedCollection.toString();
field = str.substring(1, str.length() - 1);
nestedCollection = null;
}
Row newRow = rowGroup.newRow();
String prefix = "";
/* if (nestedCollection != null) */
{
for (int i = 0; i < depth; i++) {
prefix += " . ";
}
}
String fieldNameToDisplay = fieldDisplayNames.getString(key);
if (nestedCollection == null) {
newRow.newLeftCol(prefix + fieldNameToDisplay);
}
if (nestedCollection != null) {
Map<String, Integer> columnsMap = new HashMap<String, Integer>();
GfJsonArray rowsArray = nestedCollection;
RowGroup newRowGroup = table.newRowGroup();
newRowGroup.setColumnSeparator(" | ");
newRowGroup.newBlankRow();
newRowGroup.newRow().newLeftCol(fieldNameToDisplay);
Row headerRow = newRowGroup.newRow();
int numOfRows = rowsArray.size();
List<String> tableFieldsToSkipOnUI = null;
for (int j = 0; j < numOfRows; j++) {
GfJsonObject content = rowsArray.getJSONObject(j);
if (content.has(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI)) {
GfJsonArray jsonArray = content.getJSONArray(CliJsonSerializable.FIELDS_TO_SKIP_ON_UI);
tableFieldsToSkipOnUI = new ArrayList<String>();
for (int i = 0; i < jsonArray.size(); i++) {
tableFieldsToSkipOnUI.add(String.valueOf(jsonArray.get(i)));
}
}
GfJsonArray columnNames = content.names();
int numOfColumns = columnNames.size();
if (headerRow.isEmpty()) {
GfJsonObject innerFieldDisplayNames = content.getJSONObject(CliJsonSerializable.FIELDS_TO_DISPLAYNAME_MAPPING);
for (int i = 0; i < numOfColumns; i++) {
Object columnName = columnNames.get(i);
if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
// skip file data if any //TODO - make response format better
continue;
}
headerRow.newCenterCol(innerFieldDisplayNames.getString((String) columnName));
columnsMap.put((String) columnName, i);
}
newRowGroup.newRowSeparator('-', false);
}
newRow = newRowGroup.newRow();
for (int i = 0; i < numOfColumns; i++) {
Object columnName = columnNames.get(i);
if (CliJsonSerializable.FIELDS_TO_SKIP.contains((String) columnName) || (tableFieldsToSkipOnUI != null && tableFieldsToSkipOnUI.contains(columnName))) {
// skip file data if any //TODO - make response format better
continue;
}
newRow.newLeftCol(String.valueOf(content.get((String) columnName)));
}
}
} else if (nestedObject != null) {
buildObjectSection(table, rowGroup, nestedObject, depth + 1);
} else {
// Row newRow = rowGroup.newRow();
// String prefix = "";
// for (int i = 0; i < depth; i++) {
// prefix += " . ";
// }
// newRow.newLeftCol(prefix+fieldDisplayNames.getString(key)).newLeftCol(field);
Object value = field;
/*
* if (isPrimitiveOrStringOrWrapperArray(value)) { value = Arrays.toString((String[])value);
* }
*/
newRow.newLeftCol(value);
}
nestedCollection = null;
nestedObject = null;
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class CommandResult method saveIncomingFiles.
public void saveIncomingFiles(String directory) throws IOException {
// dump file data if any
try {
GfJsonObject content = getContent();
if (content != null) {
GfJsonArray bytesArray = content.getJSONArray(CompositeResultData.BYTE_DATA_ACCESSOR);
AbstractResultData.readFileDataAndDump(bytesArray, directory);
} else {
// TODO Abhishek - add i18n
throw new RuntimeException("No associated files to save .. ");
// string
}
numTimesSaved = numTimesSaved + 1;
} catch (DataFormatException e) {
throw new RuntimeException(e);
} catch (GfJsonException e) {
throw new RuntimeException(e);
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class ResultBuilder method fromJson.
/**
* Prepare a Result object from a JSON String. This is useful on gfsh/client to read the response
* & prepare a Result object from the JSON response
*
* @param json JSON string for Result
* @return Result object prepare from the JSON string. If it fails, creates an error Result for
* Bad Response.
*/
public static Result fromJson(String json) {
Result result;
try {
GfJsonObject jsonObject = new GfJsonObject(json);
String contentType = jsonObject.getString("contentType");
GfJsonObject data = jsonObject.getJSONObject("data");
AbstractResultData resultData;
if (ResultData.TYPE_TABULAR.equals(contentType)) {
resultData = new TabularResultData(data);
} else /*
* else if (ResultData.TYPE_CATALOGED.equals(contentType)) { resultData = new
* CatalogedResultData(new GfJsonObject(String.valueOf(content))); }
*/
if (ResultData.TYPE_INFO.equals(contentType)) {
resultData = new InfoResultData(data);
} else if (ResultData.TYPE_ERROR.equals(contentType)) {
resultData = new ErrorResultData(data);
} else if (ResultData.TYPE_COMPOSITE.equals(contentType)) {
resultData = new CompositeResultData(data);
} else if (ResultData.TYPE_OBJECT.equals(contentType)) {
resultData = new ObjectResultData<>(data);
} else {
ErrorResultData errorResultData = new ErrorResultData();
errorResultData.addLine("Can not detect result type, unknown response format: " + json);
resultData = errorResultData;
}
result = buildResult(resultData);
} catch (GfJsonException e) {
result = createBadResponseErrorResult(json);
}
return result;
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class AbstractResultData method addAsFile.
private ResultData addAsFile(String fileName, byte[] data, int fileType, String message) {
if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
throw new IllegalArgumentException("Unsupported file type is specified.");
}
GfJsonObject sectionData = new GfJsonObject();
try {
GfJsonArray fileDataArray = contentObject.getJSONArray(BYTE_DATA_ACCESSOR);
if (fileDataArray == null) {
fileDataArray = new GfJsonArray();
contentObject.put(BYTE_DATA_ACCESSOR, fileDataArray);
}
fileDataArray.put(sectionData);
sectionData.put(FILE_NAME_FIELD, fileName);
sectionData.put(FILE_TYPE_FIELD, fileType);
sectionData.put(FILE_MESSAGE, message);
DeflaterInflaterData deflaterInflaterData = CliUtil.compressBytes(data);
sectionData.put(FILE_DATA_FIELD, Base64.getEncoder().encodeToString(deflaterInflaterData.getData()));
sectionData.put(DATA_LENGTH_FIELD, deflaterInflaterData.getDataLength());
} catch (GfJsonException e) {
throw new ResultDataException(e.getMessage());
}
return this;
}
Aggregations