Search in sources :

Example 1 with ClientException

use of org.apache.sling.testing.clients.ClientException in project sling by apache.

the class OsgiConsoleClient method getBundleData.

/**
     * Returns a data structure like:
     *
     * {
     *   "status" : "Bundle information: 173 bundles in total - all 173 bundles active.",
     *   "s" : [173,171,2,0,0],
     *   "data": [{
     *     "id":0,
     *     "name":"System Bundle",
     *     "fragment":false,
     *     "stateRaw":32,
     *     "state":"Active",
     *     "version":"3.0.7",
     *     "symbolicName":"org.apache.felix.framework",
     *     "category":""
     *   }]
     * }
     */
private JsonNode getBundleData(String symbolicName) throws ClientException {
    final String path = getBundlePath(symbolicName, ".json");
    final String content = this.doGet(path, SC_OK).getContent();
    final JsonNode root = JsonUtils.getJsonNodeFromString(content);
    if (root.get(JSON_KEY_DATA) == null) {
        throw new ClientException(path + " does not provide '" + JSON_KEY_DATA + "' element, JSON content=" + content);
    }
    Iterator<JsonNode> data = root.get(JSON_KEY_DATA).getElements();
    if (!data.hasNext()) {
        throw new ClientException(path + "." + JSON_KEY_DATA + " is empty, JSON content=" + content);
    }
    final JsonNode bundle = data.next();
    if (bundle.get(JSON_KEY_STATE) == null) {
        throw new ClientException(path + ".data[0].state missing, JSON content=" + content);
    }
    return bundle;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) ClientException(org.apache.sling.testing.clients.ClientException)

Example 2 with ClientException

use of org.apache.sling.testing.clients.ClientException in project sling by apache.

the class OsgiConsoleClient method getBundleId.

/**
     * Get the id of the bundle
     * @param symbolicName bundle symbolic name
     * @return the id
     * @throws ClientException if the id cannot be retrieved
     */
public long getBundleId(String symbolicName) throws ClientException {
    final JsonNode bundle = getBundleData(symbolicName);
    final JsonNode idNode = bundle.get(JSON_KEY_ID);
    if (idNode == null) {
        throw new ClientException("Cannot get id from bundle json");
    }
    return idNode.getLongValue();
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) ClientException(org.apache.sling.testing.clients.ClientException)

Example 3 with ClientException

use of org.apache.sling.testing.clients.ClientException in project sling by apache.

the class OsgiConsoleClient method getBundleVersion.

/**
     * Get the version of the bundle
     * @param symbolicName bundle symbolic name
     * @return bundle version
     * @throws ClientException
     */
public String getBundleVersion(String symbolicName) throws ClientException {
    final JsonNode bundle = getBundleData(symbolicName);
    final JsonNode versionNode = bundle.get(JSON_KEY_VERSION);
    if (versionNode == null) {
        throw new ClientException("Cannot get version from bundle json");
    }
    return versionNode.getTextValue();
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) ClientException(org.apache.sling.testing.clients.ClientException)

Example 4 with ClientException

use of org.apache.sling.testing.clients.ClientException in project sling by apache.

the class OsgiInstanceConfig method restore.

/**
     * Restore the current OSGi configuration for the PID defined in the constructor
     *
     * @throws InstanceConfigException if the config cannot be restored
     */
public InstanceConfig restore() throws InstanceConfigException, InterruptedException {
    try {
        osgiClient.waitEditConfiguration(WAIT_TIMEOUT, this.configPID, null, config);
        LOG.info("restored OSGi config for {}. It is now this: {}", this.configPID, this.config);
    } catch (ClientException e) {
        throw new InstanceConfigException("Could not edit OSGi configuration", e);
    } catch (TimeoutException e) {
        throw new InstanceConfigException("Timeout of " + WAIT_TIMEOUT + " ms was reached while waiting for the configuration", e);
    }
    return this;
}
Also used : InstanceConfigException(org.apache.sling.testing.clients.util.config.InstanceConfigException) ClientException(org.apache.sling.testing.clients.ClientException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with ClientException

use of org.apache.sling.testing.clients.ClientException in project sling by apache.

the class InputStreamBodyWithLength method getResourceStreamLength.

/**
     * Returns the length of a resource (which is needed for the InputStreamBody
     * to work. Can't currently think of a better solution than going through
     * the resource stream and count.
     *
     * @param resourcePath path to the file
     * @return the size of the resource
     */
private static long getResourceStreamLength(String resourcePath) throws ClientException {
    int streamLength = 0;
    InputStream stream = ResourceUtil.getResourceAsStream(resourcePath);
    try {
        for (int avail = stream.available(); avail > 0; avail = stream.available()) {
            streamLength += avail;
            stream.skip(avail);
        }
    } catch (IOException e) {
        throw new ClientException("Could not read " + resourcePath + "!", e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new ClientException("Could not close Inputstream for " + resourcePath + "!", e);
        }
    }
    return streamLength;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) ClientException(org.apache.sling.testing.clients.ClientException)

Aggregations

ClientException (org.apache.sling.testing.clients.ClientException)9 JsonNode (org.codehaus.jackson.JsonNode)4 IOException (java.io.IOException)2 TimeoutException (java.util.concurrent.TimeoutException)2 InstanceConfigException (org.apache.sling.testing.clients.util.config.InstanceConfigException)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Header (org.apache.http.Header)1