Search in sources :

Example 6 with JsonParser

use of javax.json.stream.JsonParser in project wildfly by wildfly.

the class JsonTestCase method testJsonServlet.

@Test
public void testJsonServlet() throws Exception {
    final String result = HttpRequest.get(url + "json", 10, TimeUnit.SECONDS);
    final JsonParser parser = Json.createParser(new StringReader(result));
    String key = null;
    String value = null;
    while (parser.hasNext()) {
        final Event event = parser.next();
        switch(event) {
            case KEY_NAME:
                key = parser.getString();
                break;
            case VALUE_STRING:
                value = parser.getString();
                break;
        }
    }
    parser.close();
    Assert.assertEquals("Key should be \"name\"", "name", key);
    Assert.assertEquals("Value should be \"value\"", "value", value);
}
Also used : StringReader(java.io.StringReader) Event(javax.json.stream.JsonParser.Event) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 7 with JsonParser

use of javax.json.stream.JsonParser 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 8 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class ActionReportJsonProprietaryReader method readFrom.

@Override
public ActionReport readFrom(final InputStream is, final String contentType) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileUtils.copy(is, baos, 0);
    String str = baos.toString("UTF-8");
    try {
        JsonParser parser = Json.createParser(new StringReader(str));
        parser.next();
        JsonObject json = parser.getObject();
        // JsonObject jsonObject = json.getJsonObject("action-report");
        CliActionReport result = new CliActionReport();
        fillActionReport(result, json);
        return result;
    } catch (JsonException ex) {
        LoggerRef.logger.log(Level.SEVERE, AdminLoggerInfo.mUnexpectedException, ex);
        throw new IOException(ex);
    }
}
Also used : JsonException(javax.json.JsonException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonParser(javax.json.stream.JsonParser)

Example 9 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class AdminCommandStateJsonProprietaryReader method readFrom.

@Override
public AdminCommandState readFrom(final InputStream is, final String contentType) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileUtils.copy(is, baos, 0);
    String str = baos.toString("UTF-8");
    try {
        JsonParser parser = Json.createParser(new StringReader(str));
        parser.next();
        JsonObject json = parser.getObject();
        return readAdminCommandState(json);
    } catch (JsonException ex) {
        LoggerRef.logger.log(Level.SEVERE, AdminLoggerInfo.mUnexpectedException, ex);
        throw new IOException(ex);
    }
}
Also used : JsonException(javax.json.JsonException) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonParser(javax.json.stream.JsonParser)

Example 10 with JsonParser

use of javax.json.stream.JsonParser 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)

Aggregations

JsonParser (javax.json.stream.JsonParser)24 StringReader (java.io.StringReader)12 JsonObject (javax.json.JsonObject)10 Test (org.junit.Test)9 JsonException (javax.json.JsonException)6 Locale (java.util.Locale)4 Event (javax.json.stream.JsonParser.Event)4 IOException (java.io.IOException)3 JsonArray (javax.json.JsonArray)3 JsonString (javax.json.JsonString)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 JsonValue (javax.json.JsonValue)2 ConstraintViolation (javax.validation.ConstraintViolation)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 BaseModel (org.glassfish.admin.rest.model.BaseModel)2 Test (org.testng.annotations.Test)2 CachedCommandModel (com.sun.enterprise.admin.util.CachedCommandModel)1