Search in sources :

Example 71 with JsonNode

use of org.codehaus.jackson.JsonNode 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 72 with JsonNode

use of org.codehaus.jackson.JsonNode in project sling by apache.

the class OsgiConsoleClient method getConfiguration.

//
// OSGi configurations
//
/**
     * Returns a map of all properties set for the config referenced by the PID, where the map keys
     * are the property names.
     *
     * @param pid the pid of the configuration
     * @param expectedStatus list of accepted statuses of the response
     * @return the properties as a map
     * @throws ClientException if the response status does not match any of the expectedStatus
     */
public Map<String, Object> getConfiguration(String pid, int... expectedStatus) throws ClientException {
    // make the request
    SlingHttpResponse resp = this.doPost(URL_CONFIGURATION + "/" + pid, null);
    // check the returned status
    HttpUtils.verifyHttpStatus(resp, HttpUtils.getExpectedStatus(SC_OK, expectedStatus));
    // get the JSON node
    JsonNode rootNode = JsonUtils.getJsonNodeFromString(resp.getContent());
    // go through the params
    Map<String, Object> props = new HashMap<String, Object>();
    if (rootNode.get("properties") == null)
        return props;
    JsonNode properties = rootNode.get("properties");
    for (Iterator<String> it = properties.getFieldNames(); it.hasNext(); ) {
        String propName = it.next();
        JsonNode value = properties.get(propName).get("value");
        if (value != null) {
            props.put(propName, value.getValueAsText());
            continue;
        }
        value = properties.get(propName).get("values");
        if (value != null) {
            Iterator<JsonNode> iter = value.getElements();
            List<String> list = new ArrayList<String>();
            while (iter.hasNext()) {
                list.add(iter.next().getValueAsText());
            }
            props.put(propName, list.toArray(new String[list.size()]));
        }
    }
    return props;
}
Also used : SlingHttpResponse(org.apache.sling.testing.clients.SlingHttpResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode)

Example 73 with JsonNode

use of org.codehaus.jackson.JsonNode 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 74 with JsonNode

use of org.codehaus.jackson.JsonNode 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 75 with JsonNode

use of org.codehaus.jackson.JsonNode in project stanbol by apache.

the class AnalyzedTextParser method parseSpan.

private void parseSpan(AnalysedText at, JsonNode node) throws IOException {
    if (node.isObject()) {
        ObjectNode jSpan = (ObjectNode) node;
        int[] spanPos = new int[] { -1, -1 };
        Collection<Entry<String, JsonNode>> jAnnotations = new ArrayList<Entry<String, JsonNode>>(4);
        SpanTypeEnum spanType = parseSpanData(jSpan, spanPos, jAnnotations);
        if (spanType == null || spanPos[0] < 0 || spanPos[1] < 0) {
            log.warn("Illegal or missing span type, start and/or end position (ignored, json: " + jSpan);
            return;
        }
        //now create the Span
        Span span;
        switch(spanType) {
            case Text:
                log.warn("Encounterd 'Text' span that is not the first span in the " + "'spans' array (ignored, json: " + node + ")");
                return;
            case TextSection:
                log.warn("Encountered 'TextSection' span. This SpanTypeEnum entry " + "is currently unused. If this is no longer the case please " + "update this implementation (ignored, json: " + node + ")");
                return;
            case Sentence:
                span = at.addSentence(spanPos[0], spanPos[1]);
                break;
            case Chunk:
                span = at.addChunk(spanPos[0], spanPos[1]);
                break;
            case Token:
                span = at.addToken(spanPos[0], spanPos[1]);
                break;
            default:
                log.warn("Unsupported SpanTypeEnum  '" + spanType + "'!. Please " + "update this implementation (ignored, json: " + node + ")");
                return;
        }
        if (!jAnnotations.isEmpty()) {
            parseAnnotations(span, jAnnotations);
        }
    } else {
        log.warn("Unable to parse Span form JsonNode " + node + " (expected JSON object)!");
    }
}
Also used : Entry(java.util.Map.Entry) SpanTypeEnum(org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) SerializedString(org.codehaus.jackson.io.SerializedString) Span(org.apache.stanbol.enhancer.nlp.model.Span)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)200 Test (org.junit.Test)55 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)50 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)15 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6