Search in sources :

Example 26 with JsonException

use of javax.json.JsonException 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 27 with JsonException

use of javax.json.JsonException in project sling by apache.

the class FsMountMojo method getBundleInstalledVersion.

/**
     * Get version of fsresource bundle that is installed in the instance.
     * @param targetUrl Target URL
     * @return Version number or null if non installed
     * @throws MojoExecutionException
     */
private String getBundleInstalledVersion(final String bundleSymbolicName, final String targetUrl) throws MojoExecutionException {
    final String getUrl = targetUrl + "/bundles/" + bundleSymbolicName + ".json";
    final GetMethod get = new GetMethod(getUrl);
    try {
        final int status = getHttpClient().executeMethod(get);
        if (status == 200) {
            final String jsonText;
            try (InputStream jsonResponse = get.getResponseBodyAsStream()) {
                jsonText = IOUtils.toString(jsonResponse, CharEncoding.UTF_8);
            }
            try {
                JsonObject response = JsonSupport.parseObject(jsonText);
                JsonArray data = response.getJsonArray("data");
                if (data.size() > 0) {
                    JsonObject bundleData = data.getJsonObject(0);
                    return bundleData.getString("version");
                }
            } catch (JsonException ex) {
                throw new MojoExecutionException("Reading bundle data from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
            }
        }
    } catch (HttpException ex) {
        throw new MojoExecutionException("Reading bundle data from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MojoExecutionException("Reading bundle data from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
    } finally {
        get.releaseConnection();
    }
    // no version detected, bundle is not installed
    return null;
}
Also used : JsonArray(javax.json.JsonArray) JsonException(javax.json.JsonException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InputStream(java.io.InputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonObject(javax.json.JsonObject) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Aggregations

JsonException (javax.json.JsonException)27 IOException (java.io.IOException)11 JsonObject (javax.json.JsonObject)11 HashMap (java.util.HashMap)9 JsonString (javax.json.JsonString)7 Map (java.util.Map)6 JsonArray (javax.json.JsonArray)6 JsonObjectBuilder (javax.json.JsonObjectBuilder)6 StringReader (java.io.StringReader)5 Resource (org.apache.sling.api.resource.Resource)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 JsonArrayBuilder (javax.json.JsonArrayBuilder)4 LoginException (org.apache.sling.api.resource.LoginException)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 File (java.io.File)3 StringWriter (java.io.StringWriter)3 JsonValue (javax.json.JsonValue)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 FileInputStream (java.io.FileInputStream)2