protected void doGet()

in src/main/java/org/apache/sling/fileoptim/impl/servlets/FileOptimizerData.java [48:75]


    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getParameter("path");

        Resource resource = request.getResourceResolver().getResource(path);

        if (resource == null) {
            response.sendError(404, "No Resource found at path " + path);
        } else if (fileOptimizer.canOptimize(resource)) {

            OptimizationResult res = fileOptimizer.getOptimizedContents(resource);
            response.setContentType("application/json");

            JsonGenerator json = Json.createGenerator(response.getWriter());
            json.writeStartObject();
            json.write("algorithm", res.getAlgorithm());
            json.write("originalSize", res.getOriginalSize());
            json.write("optimizedSize", res.getOptimizedSize());
            json.write("optimized", res.isOptimized());
            json.write("preview", "/system/fileoptim/preview?path=" + path);
            json.write("savings", res.getSavings());
            json.writeEnd();
            json.close();
            response.flushBuffer();
        } else {
            response.sendError(400, "Resource at path " + path + " is not a file or cannot be optimized");
        }
    }