Skip to content

Commit

Permalink
SLING-6898: Remove commons.json from Resource Inventory.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1797029 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
karlpauls committed May 31, 2017
1 parent bc7d8ac commit 3e29a44
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 102 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.json</artifactId>
<version>2.0.6</version>
<artifactId>org.apache.sling.commons.johnzon</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
Expand All @@ -41,16 +44,15 @@
public abstract class JsonObjectCreator {

/** Dump given resource in JSON, optionally recursing into its objects */
public static JSONObject create(final Resource resource)
throws JSONException {
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 JSONObject obj = new JSONObject();
final JsonObjectBuilder obj = Json.createObjectBuilder();

if (propertyMap == null) {

Expand All @@ -59,22 +61,26 @@ public static JSONObject create(final Resource resource)
if (value != null) {

// single value property or just plain String resource or...
obj.put(resource.getName(), value);
obj.add(resource.getName(), value);

} else {

// Try multi-value "property"
final String[] values = resource.adaptTo(String[].class);
if (values != null) {
obj.put(resource.getName(), new JSONArray(Arrays.asList(values)));
JsonArrayBuilder array = Json.createArrayBuilder();
for (String v : values) {
array.add(v);
}
obj.add(resource.getName(), array);
}

}
if ( resource.getResourceType() != null ) {
obj.put("sling:resourceType", resource.getResourceType());
obj.add("sling:resourceType", resource.getResourceType());
}
if ( resource.getResourceSuperType() != null ) {
obj.put("sling:resourceSuperType", resource.getResourceSuperType());
obj.add("sling:resourceSuperType", resource.getResourceSuperType());
}

} else {
Expand Down Expand Up @@ -117,35 +123,36 @@ private static synchronized String format(final Calendar date) {
}

/** Dump only a value in the correct format */
private static Object getValue(final Object value) {
private static JsonValue getValue(final Object value) {
JsonObjectBuilder builder = Json.createObjectBuilder();
if ( value instanceof InputStream ) {
// input stream is already handled
return 0;
builder.add("key", 0);
} else if ( value instanceof Calendar ) {
return format((Calendar)value);
builder.add("key",format((Calendar)value));
} else if ( value instanceof Boolean ) {
return value;
builder.add("key", (Boolean) value);
} else if ( value instanceof Long ) {
return value;
builder.add("key", (Long) value);
} else if ( value instanceof Integer ) {
return value;
builder.add("key", (Integer) value);
} else if ( value != null ) {
return value.toString();
builder.add("key", value.toString());
} else {
return ""; // assume empty string
builder.add("key", ""); // assume empty string
}
return builder.build().get("key");
}

/** Dump a single node */
private static void createSingleResource(final Resource n, final JSONObject parent)
throws JSONException {
parent.put(n.getName(), create(n));
private static void createSingleResource(final Resource n, final JsonObjectBuilder parent) {
parent.add(n.getName(), create(n));
}

/**
* Write a single property
*/
private static void createProperty(final JSONObject obj,
private static void createProperty(final JsonObjectBuilder obj,
final ValueMap valueMap,
final String key,
final Object value) {
Expand Down Expand Up @@ -174,11 +181,7 @@ private static void createProperty(final JSONObject obj,
}
// write out empty array
if ( values.length == 0 ) {
try {
obj.put(key, new JSONArray());
} catch ( final JSONException ignore ) {
// we ignore this
}
obj.add(key, Json.createArrayBuilder());
return;
}
}
Expand All @@ -192,15 +195,15 @@ private static void createProperty(final JSONObject obj,
// in the name, and the value should be the size of the binary data
try {
if (values == null) {
obj.put(":" + key, getLength(valueMap, -1, key, (InputStream)value));
obj.add(":" + key, getLength(valueMap, -1, key, (InputStream)value));
} else {
final JSONArray result = new JSONArray();
final JsonArrayBuilder result = Json.createArrayBuilder();
for (int i = 0; i < values.length; i++) {
result.put(getLength(valueMap, i, key, (InputStream)values[i]));
result.add(getLength(valueMap, i, key, (InputStream)values[i]));
}
obj.put(":" + key, result);
obj.add(":" + key, result);
}
} catch ( final JSONException ignore ) {
} catch ( final JsonException ignore ) {
// we ignore this
LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
}
Expand All @@ -209,15 +212,15 @@ private static void createProperty(final JSONObject obj,

try {
if (!value.getClass().isArray()) {
obj.put(key, getValue(value));
obj.add(key, getValue(value));
} else {
final JSONArray result = new JSONArray();
final JsonArrayBuilder result = Json.createArrayBuilder();
for (Object v : values) {
result.put(getValue(v));
result.add(getValue(v));
}
obj.put(key, result);
obj.add(key, result);
}
} catch ( final JSONException ignore ) {
} catch ( final JsonException ignore ) {
// we ignore this
LoggerFactory.getLogger(JsonObjectCreator.class).warn("Unable to create JSON value", ignore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
package org.apache.sling.resource.inventory.impl;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.json.Json;
import javax.json.JsonException;
import javax.json.stream.JsonGenerator;

import org.apache.felix.inventory.Format;
import org.apache.felix.inventory.InventoryPrinter;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.json.JSONException;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
Expand Down Expand Up @@ -84,12 +88,13 @@ public void print(PrintWriter printWriter, Format format, boolean isZip) {
if ( rootResource != null ) {
final ResourceTraversor rt = new ResourceTraversor(rootResource);
rt.collectResources();
printWriter.write(rt.getJSONObject().toString(2));

StringWriter writer = new StringWriter();
Json.createGenerator(writer).write(rt.getJsonObject()).close();
printWriter.write(writer.toString());
}
} catch (final LoginException e) {
// ignore
} catch (final JSONException ignore) {
} catch (final JsonException ignore) {
LoggerFactory.getLogger(this.getClass()).warn("Unable to create resource json", ignore);
} finally {
if ( resolver != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@

import java.util.Iterator;

import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class ResourceTraversor {

private final JSONObject startObject;
private final JsonObjectBuilder startObject;

private final Resource startResource;

public ResourceTraversor(final Resource resource)
throws JSONException {
public ResourceTraversor(final Resource resource) {
this.startResource = resource;
this.startObject = this.adapt(resource);
}
Expand All @@ -40,7 +41,7 @@ public ResourceTraversor(final Resource resource)
* startObject.
* @throws JSONException
*/
public void collectResources() throws JSONException {
public void collectResources() throws JsonException {
collectChildren(startResource, this.startObject);
}

Expand All @@ -50,13 +51,13 @@ public void collectResources() throws JSONException {
* @throws JSONException
*/
private void collectChildren(final Resource resource,
final JSONObject jsonObj)
throws JSONException {
final JsonObjectBuilder jsonObj)
throws JsonException {

final Iterator<Resource> children = resource.listChildren();
while (children.hasNext()) {
final Resource res = children.next();
final JSONObject json = collectResource(res, jsonObj);
final JsonObjectBuilder json = collectResource(res, jsonObj);
collectChildren(res, json);
}
}
Expand All @@ -68,10 +69,10 @@ private void collectChildren(final Resource resource,
* @param level The level where this resource is located.
* @throws JSONException
*/
private JSONObject collectResource(final Resource resource, final JSONObject parent)
throws JSONException {
final JSONObject o = adapt(resource);
parent.put(resource.getName(), o);
private JsonObjectBuilder collectResource(final Resource resource, final JsonObjectBuilder parent)
throws JsonException {
final JsonObjectBuilder o = adapt(resource);
parent.add(resource.getName(), o);
return o;
}

Expand All @@ -82,11 +83,11 @@ private JSONObject collectResource(final Resource resource, final JSONObject par
* @return The JSON representation of the Resource
* @throws JSONException
*/
private JSONObject adapt(final Resource resource) throws JSONException {
private JsonObjectBuilder adapt(final Resource resource) throws JsonException {
return JsonObjectCreator.create(resource);
}

public JSONObject getJSONObject() {
return startObject;
public JsonObject getJsonObject() {
return startObject.build();
}
}
Loading

0 comments on commit 3e29a44

Please sign in to comment.