use of javax.json.JsonValue in project Payara by payara.
the class RemoteRestAdminCommand method parseMetadata.
/**
* Parse the JSon metadata for the command.
*
* @param str the string
* @return the etag to compare the command cache model
*/
private CachedCommandModel parseMetadata(String str, String etag) {
if (logger.isLoggable(Level.FINER)) {
// XXX - assume "debug" == "FINER"
logger.finer("------- RAW METADATA RESPONSE ---------");
logger.log(Level.FINER, "ETag: {0}", etag);
logger.finer(str);
logger.finer("------- RAW METADATA RESPONSE ---------");
}
if (str == null) {
return null;
}
try {
boolean sawFile = false;
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject obj = parser.getObject();
obj = obj.getJsonObject("command");
CachedCommandModel cm = new CachedCommandModel(obj.getString("@name"), etag);
cm.dashOk = parseBoolean(obj, "@unknown-options-are-operands", false);
cm.managedJob = parseBoolean(obj, "@managed-job", false);
cm.setUsage(obj.getString("usage", null));
JsonValue optns = obj.get("option");
if (!JsonValue.NULL.equals(optns) && optns != null) {
JsonArray jsonOptions;
if (optns instanceof JsonArray) {
jsonOptions = (JsonArray) optns;
} else {
JsonArrayBuilder optBuilder = Json.createArrayBuilder();
optBuilder.add(optns);
jsonOptions = optBuilder.build();
}
for (int i = 0; i < jsonOptions.size(); i++) {
JsonObject jsOpt = jsonOptions.getJsonObject(i);
String type = jsOpt.getString("@type");
ParamModelData opt = new ParamModelData(jsOpt.getString("@name"), typeOf(type), parseBoolean(jsOpt, "@optional", false), jsOpt.getString("@default", null), jsOpt.getString("@short", null), parseBoolean(jsOpt, "@obsolete", false), jsOpt.getString("@alias", null));
opt.param._acceptableValues = jsOpt.getString("@acceptable-values", "");
if ("PASSWORD".equals(type)) {
opt.param._password = true;
opt.prompt = jsOpt.getString("@prompt", null);
opt.promptAgain = jsOpt.getString("@prompt-again", null);
} else if ("FILE".equals(type)) {
sawFile = true;
}
if (parseBoolean(jsOpt, "@primary", false)) {
opt.param._primary = true;
}
if (parseBoolean(jsOpt, "@multiple", false)) {
if (opt.type == File.class) {
opt.type = File[].class;
} else {
opt.type = List.class;
}
opt.param._multiple = true;
}
cm.add(opt);
}
}
if (sawFile) {
cm.add(new ParamModelData("upload", Boolean.class, true, null));
addedUploadOption = true;
cm.setAddedUploadOption(true);
}
if (notify) {
cm.add(new ParamModelData("notify", Boolean.class, false, "false"));
}
this.usage = cm.getUsage();
return cm;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Can not parse command metadata", ex);
return null;
}
}
use of javax.json.JsonValue in project Payara by payara.
the class ParamMetadata method toJson.
public JsonObject toJson() throws JsonException {
JsonObjectBuilder o = Json.createObjectBuilder();
// o.put("name", name);
o.add("type", getTypeString());
if (help != null) {
o.add("help", help);
}
JsonValue defVal = (defaultValue != null) ? defaultValue : JsonObject.NULL;
o.add("default", defVal);
o.add("readOnly", readOnly);
o.add("confidential", confidential);
o.add("immutable", immutable);
o.add("createOnly", createOnly);
return o.build();
}
use of javax.json.JsonValue in project Payara by payara.
the class JsonHashMapProvider method readFrom.
@Override
public HashMap<String, String> readFrom(Class<HashMap<String, String>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream in) throws IOException {
HashMap map = new HashMap();
try {
JsonParser parser = Json.createParser(in);
JsonObject obj;
if (parser.hasNext()) {
parser.next();
obj = parser.getObject();
} else {
obj = JsonValue.EMPTY_JSON_OBJECT;
}
for (String k : obj.keySet()) {
JsonValue value = obj.get(k);
if (value.getValueType() == ValueType.STRING) {
map.put(k, ((JsonString) value).getString());
} else {
map.put(k, value.toString());
}
}
return map;
} catch (JsonException ex) {
// map.put("error", "Entity Parsing Error: " + ex.getMessage());
return map;
}
}
use of javax.json.JsonValue in project torodb by torodb.
the class TableRefConverter method fromJsonArray.
public static TableRef fromJsonArray(TableRefFactory tableRefFactory, JsonArray tableRefJsonArray) {
TableRef tableRef = tableRefFactory.createRoot();
for (JsonValue tableRefNameValue : tableRefJsonArray) {
String tableRefName = ((JsonString) tableRefNameValue).getString();
tableRef = createChild(tableRefFactory, tableRef, tableRefName);
}
return tableRef;
}
use of javax.json.JsonValue in project sling by apache.
the class TeleporterHttpClient method verifyCorrectBundleState.
void verifyCorrectBundleState(String bundleSymbolicName, int timeoutInSeconds) throws IOException {
final String url = baseUrl + "/system/console/bundles/" + bundleSymbolicName + ".json";
final long end = System.currentTimeMillis() + timeoutInSeconds * 1000;
final ExponentialBackoffDelay d = new ExponentialBackoffDelay(50, 250);
while (System.currentTimeMillis() < end) {
String jsonBody = waitForStatus(url, 200, timeoutInSeconds * 1000);
// deserialize json (https://issues.apache.org/jira/browse/SLING-6536)
try (JsonReader jsonReader = Json.createReader(new StringReader(jsonBody))) {
// extract state
JsonArray jsonArray = jsonReader.readObject().getJsonArray("data");
if (jsonArray == null) {
throw new JsonException("Could not find 'data' array");
}
JsonObject bundleObject = jsonArray.getJsonObject(0);
String state = bundleObject.getString("state");
if ("Active".equals(state)) {
return;
}
// otherwise evaluate the import section
JsonArray propsArray = bundleObject.getJsonArray("props");
if (propsArray == null) {
throw new JsonException("Could not find 'props' object");
}
// iterate through all of them until key="Imported Packages" is found
for (JsonValue propValue : propsArray) {
if (propValue.getValueType().equals(ValueType.OBJECT)) {
JsonObject propObject = (JsonObject) propValue;
if ("Imported Packages".equals(propObject.getString("key"))) {
JsonArray importedPackagesArray = propObject.getJsonArray("value");
String reason = null;
for (JsonValue importedPackageValue : importedPackagesArray) {
if (importedPackageValue.getValueType().equals(ValueType.STRING)) {
String importedPackage = ((JsonString) importedPackageValue).getString();
if (importedPackage.startsWith("ERROR:")) {
reason = importedPackage;
}
}
}
// only if ERROR is found there is no more need to wait for the bundle to become active, otherwise it might just be started in the background
if (reason != null) {
throw new IllegalStateException("The test bundle '" + bundleSymbolicName + "' is in state '" + state + "'. This is due to unresolved import-packages: " + reason);
}
}
}
}
} catch (JsonException | IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Test bundle '" + bundleSymbolicName + "' not correctly installed. Could not parse JSON response though to expose further information: " + jsonBody, e);
}
d.waitNextDelay();
}
throw new IOException("Bundle '" + bundleSymbolicName + "' was not started after " + timeoutInSeconds + " seconds. The check at " + url + " was not successfull. Probably some dependent bundle was not started.");
}
Aggregations