Search in sources :

Example 91 with JsonArrayBuilder

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

the class Announcement method asJSONObject.

/** Convert this announcement into a json object **/
private JsonObject asJSONObject(boolean filterTimes) {
    JsonObjectBuilder announcement = Json.createObjectBuilder();
    announcement.add("ownerId", ownerId);
    announcement.add("protocolVersion", protocolVersion);
    // for backwards compatibility!
    if (!filterTimes) {
        announcement.add("created", System.currentTimeMillis());
    }
    announcement.add("inherited", inherited);
    if (loop) {
        announcement.add("loop", loop);
    }
    if (serverInfo != null) {
        announcement.add("serverInfo", serverInfo);
    }
    if (localCluster != null) {
        announcement.add("localClusterView", asJSON(localCluster));
    }
    if (!filterTimes && backoffInterval > 0) {
        announcement.add("backoffInterval", backoffInterval);
    }
    if (resetBackoff) {
        announcement.add("resetBackoff", resetBackoff);
    }
    JsonArrayBuilder incomingAnnouncements = Json.createArrayBuilder();
    for (Iterator<Announcement> it = incomings.iterator(); it.hasNext(); ) {
        Announcement incoming = it.next();
        incomingAnnouncements.add(incoming.asJSONObject(filterTimes));
    }
    announcement.add("topologyAnnouncements", incomingAnnouncements);
    return announcement.build();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 92 with JsonArrayBuilder

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

the class AuthorizablePipe method bindMembers.

/**
     * add current group's members to the bindings
     * @param auth group whose members should be bound in the pipe bindings
     */
protected void bindMembers(Authorizable auth) {
    try {
        if (auth.isGroup()) {
            Group group = (Group) auth;
            Iterator<Authorizable> memberIterator = group.getMembers();
            JsonArrayBuilder array = Json.createArrayBuilder();
            while (memberIterator.hasNext()) {
                array.add(memberIterator.next().getID());
            }
            outputBinding = JsonUtil.toString(array);
        } else {
            logger.error("{} is not a group, unable to bind members", auth.getID());
        }
    } catch (Exception e) {
        logger.error("unable to bind members");
    }
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 93 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder 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 94 with JsonArrayBuilder

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

the class JsonObjectCreator method createProperty.

/**
     * Write a single property
     */
public static void createProperty(final JsonObjectBuilder obj, final ValueMap valueMap, final String key, final Object value) {
    Object[] values = null;
    if (value.getClass().isArray()) {
        final int length = Array.getLength(value);
        // write out empty array
        if (length == 0) {
            obj.add(key, Json.createArrayBuilder());
            return;
        }
        values = new Object[Array.getLength(value)];
        for (int i = 0; i < length; i++) {
            values[i] = Array.get(value, i);
        }
    }
    // special handling for binaries: we dump the length and not the data!
    if (value instanceof InputStream || (values != null && values[0] instanceof InputStream)) {
        // in the name, and the value should be the size of the binary data
        if (values == null) {
            obj.add(":" + key, getLength(valueMap, -1, key, (InputStream) value));
        } else {
            final JsonArrayBuilder result = Json.createArrayBuilder();
            for (int i = 0; i < values.length; i++) {
                result.add(getLength(valueMap, i, key, (InputStream) values[i]));
            }
            obj.add(":" + key, result);
        }
        return;
    }
    if (!value.getClass().isArray()) {
        obj.add(key, getValue(value));
    } else {
        final JsonArrayBuilder result = Json.createArrayBuilder();
        for (Object v : values) {
            result.add(getValue(v));
        }
        obj.add(key, result);
    }
}
Also used : InputStream(java.io.InputStream) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 95 with JsonArrayBuilder

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

the class JsonObjectCreator method create.

/** Dump given resource in JSON, optionally recursing into its objects */
private static JsonObjectBuilder create(final Resource resource, final int currentRecursionLevel, final int maxRecursionLevels) {
    final ValueMap valueMap = resource.adaptTo(ValueMap.class);
    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(ResourceUtil.getName(resource), value.toString());
        } else {
            // Try multi-value "property"
            final String[] values = resource.adaptTo(String[].class);
            if (values != null) {
                JsonArrayBuilder builder = Json.createArrayBuilder();
                for (String v : values) {
                    builder.add(v);
                }
                obj.add(ResourceUtil.getName(resource), builder);
            }
        }
    } else {
        @SuppressWarnings("unchecked") final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
        // the node's actual properties
        while (props.hasNext()) {
            final Map.Entry prop = props.next();
            if (prop.getValue() != null) {
                createProperty(obj, valueMap, prop.getKey().toString(), prop.getValue());
            }
        }
    }
    // the child nodes
    if (recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
        final Iterator<Resource> children = ResourceUtil.listChildren(resource);
        while (children.hasNext()) {
            final Resource n = children.next();
            createSingleResource(n, obj, currentRecursionLevel, maxRecursionLevels);
        }
    }
    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)

Aggregations

JsonArrayBuilder (javax.json.JsonArrayBuilder)177 JsonObjectBuilder (javax.json.JsonObjectBuilder)103 JsonObject (javax.json.JsonObject)36 Map (java.util.Map)29 Path (javax.ws.rs.Path)26 GET (javax.ws.rs.GET)24 HashMap (java.util.HashMap)19 JsonArray (javax.json.JsonArray)18 ArrayList (java.util.ArrayList)15 List (java.util.List)15 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)12 IOException (java.io.IOException)12 Dataverse (edu.harvard.iq.dataverse.Dataverse)10 Dataset (edu.harvard.iq.dataverse.Dataset)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 JsonValue (javax.json.JsonValue)9 StringWriter (java.io.StringWriter)8 JsonString (javax.json.JsonString)7 Date (java.util.Date)6 JsonException (javax.json.JsonException)6