use of org.apache.sling.servlets.get.impl.util.ResourceTraversor in project sling by apache.
the class JsonRendererServlet method doGet.
@Override
protected void doGet(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);
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 JsonRenderer.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
Json.createGenerator(resp.getWriter()).write(traversor.getJSONObject()).close();
}
} else {
// We are not allowed to do the dump.
// Send a 300
String tidyUrl = (tidy) ? "tidy." : "";
resp.setStatus(HttpServletResponse.SC_MULTIPLE_CHOICES);
JsonGenerator writer = Json.createGenerator(resp.getWriter());
writer.writeStartArray();
while (allowedLevel >= 0) {
writer.write(r.getResourceMetadata().getResolutionPath() + "." + tidyUrl + allowedLevel + ".json");
allowedLevel--;
}
writer.writeEnd();
writer.close();
}
} catch (Exception je) {
reportException(je);
}
}
Aggregations