Search in sources :

Example 46 with JsonObjectBuilder

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

the class ServletJsonUtils method writeJson.

public static void writeJson(SlingHttpServletResponse response, DistributionResponse distributionResponse) throws IOException {
    JsonObjectBuilder json = Json.createObjectBuilder();
    try {
        json.add("success", distributionResponse.isSuccessful());
        json.add("state", distributionResponse.getState().name());
        json.add("message", distributionResponse.getMessage());
    } catch (JsonException e) {
        log.error("Cannot write json", e);
    }
    switch(distributionResponse.getState()) {
        case DISTRIBUTED:
            response.setStatus(200);
            break;
        case DROPPED:
            response.setStatus(400);
            break;
        case ACCEPTED:
            response.setStatus(202);
            break;
        default:
            // TODO
            break;
    }
    append(json.build(), response.getWriter());
}
Also used : JsonException(javax.json.JsonException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 47 with JsonObjectBuilder

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

the class ServletJsonUtils method writeJson.

public static void writeJson(SlingHttpServletResponse response, int status, String message, @Nullable Map<String, String> kv) throws IOException {
    JsonObjectBuilder json = Json.createObjectBuilder();
    try {
        json.add("message", message);
        if (kv != null && kv.size() > 0) {
            for (Map.Entry<String, String> entry : kv.entrySet()) {
                json.add(entry.getKey(), entry.getValue());
            }
        }
    } catch (JsonException e) {
        log.error("Cannot write json", e);
    }
    response.setStatus(status);
    append(json.build(), response.getWriter());
}
Also used : JsonException(javax.json.JsonException) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 48 with JsonObjectBuilder

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

the class JsonObjectCreator method create.

/** Dump given resource in JSON, optionally recursing into its objects */
public static JsonObjectBuilder create(final Resource resource) {
    final ValueMap valueMap = resource.adaptTo(ValueMap.class);
    @SuppressWarnings("unchecked") final Map propertyMap = (valueMap != null) ? valueMap : resource.adaptTo(Map.class);
    final JsonObjectBuilder obj = Json.createObjectBuilder();
    if (propertyMap == null) {
        // no map available, try string
        final String value = resource.adaptTo(String.class);
        if (value != null) {
            // single value property or just plain String resource or...
            obj.add(resource.getName(), value);
        } else {
            // Try multi-value "property"
            final String[] values = resource.adaptTo(String[].class);
            if (values != null) {
                JsonArrayBuilder array = Json.createArrayBuilder();
                for (String v : values) {
                    array.add(v);
                }
                obj.add(resource.getName(), array);
            }
        }
        if (resource.getResourceType() != null) {
            obj.add("sling:resourceType", resource.getResourceType());
        }
        if (resource.getResourceSuperType() != null) {
            obj.add("sling:resourceSuperType", resource.getResourceSuperType());
        }
    } else {
        @SuppressWarnings("unchecked") final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
        // the node's actual properties
        while (props.hasNext()) {
            @SuppressWarnings("unchecked") final Map.Entry prop = props.next();
            if (prop.getValue() != null) {
                createProperty(obj, valueMap, prop.getKey().toString(), prop.getValue());
            }
        }
    }
    // the child nodes
    final Iterator<Resource> children = resource.listChildren();
    while (children.hasNext()) {
        final Resource n = children.next();
        createSingleResource(n, obj);
    }
    return obj;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map)

Example 49 with JsonObjectBuilder

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

the class ResourceTraversor method collectResource.

/**
     * Adds a resource in the JSON tree.
     *
     * @param resource The resource to add
     * @param level The level where this resource is located.
     * @throws JSONException
     */
private JsonObjectBuilder collectResource(final Resource resource, final JsonObjectBuilder parent) throws JsonException {
    final JsonObjectBuilder o = adapt(resource);
    parent.add(resource.getName(), o);
    return o;
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 50 with JsonObjectBuilder

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

the class VersionInfoServlet method getJsonObject.

private JsonObject getJsonObject(Resource resource) throws RepositoryException {
    final JsonObjectBuilder result = Json.createObjectBuilder();
    final Node node = resource.adaptTo(Node.class);
    if (node == null || !node.isNodeType(JcrConstants.MIX_VERSIONABLE)) {
        return result.build();
    }
    final VersionHistory history = node.getVersionHistory();
    final Version baseVersion = node.getBaseVersion();
    for (final VersionIterator it = history.getAllVersions(); it.hasNext(); ) {
        final Version v = it.nextVersion();
        final JsonObjectBuilder obj = Json.createObjectBuilder();
        obj.add("created", createdDate(v));
        obj.add("successors", getArrayBuilder(getNames(v.getSuccessors())));
        obj.add("predecessors", getArrayBuilder(getNames(v.getPredecessors())));
        obj.add("labels", getArrayBuilder(history.getVersionLabels(v)));
        obj.add("baseVersion", baseVersion.isSame(v));
        result.add(v.getName(), obj);
    }
    return Json.createObjectBuilder().add("versions", result).build();
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) JsonObjectBuilder(javax.json.JsonObjectBuilder) VersionHistory(javax.jcr.version.VersionHistory)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)76 JsonArrayBuilder (javax.json.JsonArrayBuilder)31 Map (java.util.Map)19 JsonObject (javax.json.JsonObject)19 JsonException (javax.json.JsonException)14 HashMap (java.util.HashMap)9 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)6 JsonValue (javax.json.JsonValue)6 List (java.util.List)5 JsonString (javax.json.JsonString)5 PrintWriter (java.io.PrintWriter)3 Entry (java.util.Map.Entry)3 JsonArray (javax.json.JsonArray)3 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)2 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 JsonWriter (javax.json.JsonWriter)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2