in src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java [71:144]
public void render(SlingHttpServletRequest req,
SlingHttpServletResponse resp) throws IOException {
// Access and check our data
final Resource r = req.getResource();
if (ResourceUtil.isNonExistingResource(r)) {
throw new ResourceNotFoundException("No data to render.");
}
int maxRecursionLevels = 0;
try {
maxRecursionLevels = getMaxRecursionLevel(req);
} catch(IllegalArgumentException iae) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.getMessage());
return;
}
resp.setContentType(req.getResponseContentType());
resp.setCharacterEncoding("UTF-8");
// We check the tree to see if the nr of nodes isn't bigger than the allowed nr.
boolean allowDump = true;
int allowedLevel = 0;
final boolean tidy = isTidy(req);
final boolean harray = hasSelector(req, HARRAY);
ResourceTraversor traversor = null;
try {
traversor = new ResourceTraversor(maxRecursionLevels, maximumResults, r, ecmaSupport);
allowedLevel = traversor.collectResources();
if ( allowedLevel != -1 ) {
allowDump = false;
}
} catch (final Exception e) {
reportException(e);
}
try {
// Dump the resource if we can
if (allowDump) {
if (tidy || harray) {
final JsonToText.Options opt = renderer.options()
.withIndent(tidy ? INDENT_SPACES : 0)
.withArraysForChildren(harray);
resp.getWriter().write(renderer.prettyPrint(traversor.getJSONObject(), opt));
} else {
// If no rendering options, use the plain toString() method, for
// backwards compatibility. Output might be slightly different
// with prettyPrint and no options
StringWriter writer = new StringWriter();
try (JsonGenerator json = Json.createGenerator(writer)){
json.write(traversor.getJSONObject());
}
resp.getWriter().write(writer.toString());
}
} else {
// We are not allowed to do the dump.
// Send a 300
String tidyUrl = (tidy) ? "tidy." : "";
resp.setStatus(HttpServletResponse.SC_MULTIPLE_CHOICES);
StringWriter writer = new StringWriter();
try (JsonGenerator json = Json.createGenerator(writer)) {
json.writeStartArray();
while (allowedLevel >= 0) {
json.write(
r.getResourceMetadata().getResolutionPath() + "." + tidyUrl + allowedLevel + ".json");
allowedLevel--;
}
json.writeEnd();
}
resp.getWriter().write(writer.toString());
}
} catch (Exception je) {
reportException(je);
}
}