use of org.apache.geode.management.internal.cli.json.GfJsonObject 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.GfJsonObject in project geode by apache.
the class CompositeResultData method retrieveSection.
public SectionResultData retrieveSection(String keyToRetrieve) {
SectionResultData sectionToRetrieve = null;
if (contentObject.has(SectionResultData.generateSectionKey(keyToRetrieve))) {
GfJsonObject sectionData = contentObject.getJSONObject(SectionResultData.generateSectionKey(keyToRetrieve));
sectionToRetrieve = new SectionResultData(sectionData);
}
return sectionToRetrieve;
}
use of org.apache.geode.management.internal.cli.json.GfJsonObject 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.GfJsonObject 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;
}
use of org.apache.geode.management.internal.cli.json.GfJsonObject in project geode by apache.
the class AbstractResultData method readFileDataAndDump.
/**
* @param byteDataArray
* @throws GfJsonException
* @throws DataFormatException
* @throws IOException
*/
public static void readFileDataAndDump(GfJsonArray byteDataArray, String directory) throws GfJsonException, DataFormatException, IOException {
boolean overwriteAllExisting = false;
int length = byteDataArray.size();
// TODO - Abhishek Make this consistent -
String options = length > 1 ? "(y/N/a)" : "(y/N)";
BYTEARRAY_LOOP: for (int i = 0; i < length; i++) {
GfJsonObject object = byteDataArray.getJSONObject(i);
int fileType = object.getInt(FILE_TYPE_FIELD);
if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
throw new IllegalArgumentException("Unsupported file type found.");
}
// build file name
byte[] fileNameBytes = null;
String fileName = null;
GfJsonArray fileNameJsonBytes = object.getJSONArray(FILE_NAME_FIELD);
if (fileNameJsonBytes != null) {
// if in gfsh
fileNameBytes = GfJsonArray.toByteArray(fileNameJsonBytes);
fileName = new String(fileNameBytes);
} else {
// if on member
fileName = (String) object.get(FILE_NAME_FIELD);
}
// build file message
byte[] fileMessageBytes = null;
String fileMessage = null;
GfJsonArray fileMessageJsonBytes = object.getJSONArray(FILE_MESSAGE);
if (fileMessageJsonBytes != null) {
// if in gfsh
fileMessageBytes = GfJsonArray.toByteArray(fileMessageJsonBytes);
fileMessage = new String(fileMessageBytes);
} else {
// if on member
fileMessage = (String) object.get(FILE_MESSAGE);
}
String fileDataString = (String) object.get(FILE_DATA_FIELD);
int fileDataLength = (int) object.get(DATA_LENGTH_FIELD);
byte[] byteArray = Base64.getDecoder().decode(fileDataString);
byte[] uncompressBytes = CliUtil.uncompressBytes(byteArray, fileDataLength).getData();
boolean isGfshVM = CliUtil.isGfshVM();
File fileToDumpData = new File(fileName);
if (!fileToDumpData.isAbsolute()) {
if (directory == null || directory.isEmpty()) {
directory = System.getProperty("user.dir", ".");
}
fileToDumpData = new File(directory, fileName);
}
File parentDirectory = fileToDumpData.getParentFile();
if (parentDirectory != null) {
parentDirectory.mkdirs();
}
if (fileToDumpData.exists()) {
String fileExistsMessage = CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__FILE_WITH_NAME_0_EXISTS_IN_1, new Object[] { fileName, fileToDumpData.getParent(), options });
if (isGfshVM) {
Gfsh gfsh = Gfsh.getCurrentInstance();
if (gfsh != null && !gfsh.isQuietMode() && !overwriteAllExisting) {
fileExistsMessage = fileExistsMessage + " Overwrite? " + options + " : ";
String interaction = gfsh.interact(fileExistsMessage);
if ("a".equalsIgnoreCase(interaction.trim())) {
overwriteAllExisting = true;
} else if (!"y".equalsIgnoreCase(interaction.trim())) {
// do not save file & continue
continue BYTEARRAY_LOOP;
}
}
} else {
throw new IOException(fileExistsMessage);
}
} else if (!parentDirectory.exists()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_DIRECTORY_OF_0_DOES_NOT_EXIST, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
} else if (!parentDirectory.canWrite()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_DIRECTORY_OF_0_IS_NOT_WRITABLE, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
} else if (!parentDirectory.isDirectory()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_OF_0_IS_NOT_DIRECTORY, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
}
if (fileType == FILE_TYPE_TEXT) {
FileWriter fw = new FileWriter(fileToDumpData);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(new String(uncompressBytes));
bw.flush();
fw.flush();
fw.close();
} else if (fileType == FILE_TYPE_BINARY) {
FileOutputStream fos = new FileOutputStream(fileToDumpData);
fos.write(uncompressBytes);
fos.flush();
fos.close();
}
// System.out.println("fileMessage :: "+fileMessage);
if (fileMessage != null && !fileMessage.isEmpty()) {
if (isGfshVM) {
Gfsh.println(MessageFormat.format(fileMessage, new Object[] { fileToDumpData.getAbsolutePath() }));
}
}
// System.out.println(new String(uncompressed));
}
}
Aggregations