Search in sources :

Example 11 with JsonValue

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;
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) ParamModelData(com.sun.enterprise.admin.util.CommandModelData.ParamModelData) CachedCommandModel(com.sun.enterprise.admin.util.CachedCommandModel) SSLException(javax.net.ssl.SSLException) JsonArray(javax.json.JsonArray) JsonArrayBuilder(javax.json.JsonArrayBuilder) SmartFile(com.sun.enterprise.universal.io.SmartFile) JsonParser(javax.json.stream.JsonParser)

Example 12 with JsonValue

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();
}
Also used : JsonValue(javax.json.JsonValue) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 13 with JsonValue

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;
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonParser(javax.json.stream.JsonParser)

Example 14 with JsonValue

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;
}
Also used : JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) TableRef(com.torodb.core.TableRef)

Example 15 with JsonValue

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.");
}
Also used : JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) IOException(java.io.IOException) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString)

Aggregations

JsonValue (javax.json.JsonValue)25 JsonObject (javax.json.JsonObject)14 JsonString (javax.json.JsonString)13 JsonArray (javax.json.JsonArray)11 Map (java.util.Map)7 JsonException (javax.json.JsonException)7 JsonObjectBuilder (javax.json.JsonObjectBuilder)6 HashMap (java.util.HashMap)5 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 JsonReader (javax.json.JsonReader)3 KvValue (com.torodb.kvdocument.values.KvValue)2 ListKvArray (com.torodb.kvdocument.values.heap.ListKvArray)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonNumber (javax.json.JsonNumber)2 JsonParser (javax.json.stream.JsonParser)2 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1