Search in sources :

Example 41 with JsonReader

use of javax.json.JsonReader in project mysql_perf_analyzer by yahoo.

the class UserDefinedMetrics method createFromJson.

public static UserDefinedMetrics createFromJson(java.io.InputStream in) {
    JsonReader jsonReader = null;
    UserDefinedMetrics udm = null;
    try {
        jsonReader = javax.json.Json.createReader(in);
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();
        udm = new UserDefinedMetrics(jsonObject.getString("groupName"));
        udm.setAuto("y".equalsIgnoreCase(jsonObject.getString("auto", null)));
        udm.setStoreInCommonTable("y".equalsIgnoreCase(jsonObject.getString("storeInCommonTable", null)));
        udm.setSource(jsonObject.getString("source"));
        udm.setUdmType(jsonObject.getString("type"));
        udm.setNameCol(jsonObject.getString("nameColumn", null));
        udm.setValueCol(jsonObject.getString("valueColumn", null));
        udm.setKeyCol(jsonObject.getString("keyColumn", null));
        udm.setSql(jsonObject.getString("sql", null));
        JsonArray metrics = jsonObject.getJsonArray("metrics");
        if (metrics != null) {
            int mlen = metrics.size();
            for (int i = 0; i < mlen; i++) {
                JsonObject mobj = metrics.getJsonObject(i);
                udm.addmetric(mobj.getString("name"), mobj.getString("sourceName"), "y".equalsIgnoreCase(mobj.getString("inc")), Metric.strToMetricDataType(mobj.getString("dataType")));
            }
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error to parse UDM", ex);
    //TODO
    }
    return udm;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Example 42 with JsonReader

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

Example 43 with JsonReader

use of javax.json.JsonReader in project opentheso by miledrousset.

the class SelectedTerme method majNoticeBdd.

private void majNoticeBdd() {
    // ResourceBundle bundlePref = getBundlePref();
    // st.getTaskResultSet().getFragmentCount();
    nbNotices = 0;
    urlNotice = user.getNodePreference().getUrlBdd();
    if (user.getNodePreference().isBddUseId()) {
        urlNotice = urlNotice.replace("##value##", idC);
    } else {
        urlNotice = urlNotice.replace("##value##", nom);
    }
    // récupération du total des notices
    String urlCounterBdd = user.getNodePreference().getUrlCounterBdd();
    urlCounterBdd = urlCounterBdd.replace("##conceptId##", idC);
    // urlCounterBdd = "http://healthandco.test.o2sources.com/concept/40/total";
    // exemple des données récupérées
    // "{\"content\":[{\"nb_notices\":\"s7\"}],\"debug\":\"\",\"error\":0}\" ";
    // {"content":[{"nb_notices":"7"}],"debug":"","error":0}
    URL url;
    try {
        url = new URL(urlCounterBdd);
        InputStream is = url.openStream();
        JsonReader reader = Json.createReader(is);
        JsonObject totalNotices = reader.readObject();
        reader.close();
        JsonArray values = totalNotices.getJsonArray("content");
        String name;
        for (int i = 0; i < values.size(); i++) {
            JsonObject item = values.getJsonObject(i);
            try {
                name = item.getString("nb_notices");
                if (name != null) {
                    if (!name.isEmpty())
                        nbNotices = Integer.parseInt(name);
                }
            } catch (JsonException e) {
                System.out.println(e.toString());
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
    }
// try {
// urlNotice = URLEncoder.encode(urlNotice);
// } catch (UnsupportedEncodingException ex) {
// Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
// }
}
Also used : JsonArray(javax.json.JsonArray) JsonException(javax.json.JsonException) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) PrefixString(com.k_int.IR.QueryModels.PrefixString) IOException(java.io.IOException) URL(java.net.URL) JsonException(javax.json.JsonException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SearchException(com.k_int.IR.SearchException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) TimeoutExceededException(com.k_int.IR.TimeoutExceededException) IOException(java.io.IOException)

Example 44 with JsonReader

use of javax.json.JsonReader in project opentheso by miledrousset.

the class ReadJson method getJsonObject.

/**
 * permet de lire un fichier Json d'après une inputStream
 * @param is
 * @return
 */
public JsonObject getJsonObject(InputStream is) {
    JsonReader reader = Json.createReader(is);
    JsonObject jsonObject = reader.readObject();
    reader.close();
    return jsonObject;
}
Also used : JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Example 45 with JsonReader

use of javax.json.JsonReader in project opentheso by miledrousset.

the class ReadJson method getJsonObject.

/**
 * Permet de lire un texte en Json
 * @param jsonText
 * @return
 */
public JsonObject getJsonObject(String jsonText) {
    // String total = " {\"content\":[{\"nb_notices\":\"7\"}],\"debug\":\"\",\"error\":0}\" ";
    JsonReader reader = Json.createReader(new StringReader(jsonText));
    JsonObject jsonObject = reader.readObject();
    reader.close();
    return jsonObject;
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Aggregations

JsonReader (javax.json.JsonReader)61 JsonObject (javax.json.JsonObject)49 StringReader (java.io.StringReader)31 JsonArray (javax.json.JsonArray)31 JsonString (javax.json.JsonString)25 Test (org.junit.Test)20 HashMap (java.util.HashMap)12 LinkedHashMap (java.util.LinkedHashMap)10 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)3 Response (com.ning.http.client.Response)3 File (java.io.File)3 JsonException (javax.json.JsonException)3 JsonValue (javax.json.JsonValue)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2