use of org.apache.geode.management.internal.cli.json.GfJsonArray 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.GfJsonArray 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.GfJsonArray 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));
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.
the class JsonUtil method getByteArray.
public static byte[] getByteArray(GfJsonObject jsonObject, String byName) {
byte[] byteArray = null;
try {
GfJsonArray jsonArray = jsonObject.getJSONArray(byName);
byteArray = GfJsonArray.toByteArray(jsonArray);
} catch (GfJsonException e) {
throw new ResultDataException(e.getMessage());
}
return byteArray;
}
use of org.apache.geode.management.internal.cli.json.GfJsonArray in project geode by apache.
the class DataCommandFunctionWithPDXJUnitTest method assertThatRowIsBuiltCorrectly.
private void assertThatRowIsBuiltCorrectly(GfJsonObject table, int rowNum, Customer customer) throws Exception {
SoftAssertions softly = new SoftAssertions();
String id = (String) table.getJSONArray("id").get(rowNum);
String firstName = (String) table.getJSONArray("firstName").get(rowNum);
String lastName = (String) table.getJSONArray("lastName").get(rowNum);
softly.assertThat(id).describedAs("Customer ID").isEqualTo(customer.id);
softly.assertThat(firstName).describedAs("First name").isEqualTo(customer.firstName);
softly.assertThat(lastName).describedAs("Last name").isEqualTo(customer.lastName);
GfJsonArray phoneArray = table.getJSONArray("phone");
if (phoneArray == null) {
softly.assertThat(customer).describedAs("No phone data").isNotInstanceOf(CustomerWithPhone.class);
} else {
String phone = (String) phoneArray.get(rowNum);
if (customer instanceof CustomerWithPhone) {
softly.assertThat(phone).describedAs("Phone").isEqualTo(((CustomerWithPhone) customer).phone);
} else {
softly.assertThat(phone).describedAs("Phone (missing)").isEqualTo(MISSING_VALUE);
}
}
softly.assertAll();
}
Aggregations