Search in sources :

Example 31 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 32 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 33 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 34 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)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)34 JsonArrayBuilder (javax.json.JsonArrayBuilder)21 Map (java.util.Map)11 JsonObject (javax.json.JsonObject)10 JsonException (javax.json.JsonException)7 HashMap (java.util.HashMap)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)5 List (java.util.List)3 Entry (java.util.Map.Entry)3 JsonValue (javax.json.JsonValue)3 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 Privilege (javax.jcr.security.Privilege)2 JsonArray (javax.json.JsonArray)2 JsonString (javax.json.JsonString)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1