Search in sources :

Example 1 with ResourceTraversor

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);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ResourceTraversor(org.apache.sling.servlets.get.impl.util.ResourceTraversor) JsonGenerator(javax.json.stream.JsonGenerator) JsonRenderer(org.apache.sling.servlets.get.impl.util.JsonRenderer) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Aggregations

IOException (java.io.IOException)1 JsonGenerator (javax.json.stream.JsonGenerator)1 SlingException (org.apache.sling.api.SlingException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 JsonRenderer (org.apache.sling.servlets.get.impl.util.JsonRenderer)1 ResourceTraversor (org.apache.sling.servlets.get.impl.util.ResourceTraversor)1