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