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();
}
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");
}
}
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;
}
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);
}
}
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;
}
Aggregations