CacheReporter.java 2.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.cq.mobile.appcache.impl;

import java.util.HashSet;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class CacheReporter {
    public static final String ZIP_FULL_LISTING_NAME = "pge-package.json";
    public static final String ZIP_UPDATE_LISTING_NAME = "pge-package-update.json";
    public static final String KEY_FILES = "files";
    private Node cacheRootNode = null;

    public CacheReporter(Node cacheRootNode) {
        this.cacheRootNode = cacheRootNode;
    }

    public JSONObject getFileListingJSON(Long lastUpdated) throws RepositoryException, JSONException {
        JSONObject fileListingJSON = new JSONObject();
        JSONArray fileListingJSONArray = new JSONArray();
        fileListingJSON.put("files", (Object)fileListingJSONArray);
        this.addFileListing(fileListingJSONArray, this.cacheRootNode, this.cacheRootNode, lastUpdated);
        fileListingJSON.put("count", fileListingJSONArray.length());
        return fileListingJSON;
    }

    private void addFileListing(JSONArray fileListingJSON, Node rootNode, Node currentDirNode, Long lastUpdated) throws RepositoryException {
        NodeIterator nodeIter = currentDirNode.getNodes();
        while (nodeIter.hasNext()) {
            Node node = nodeIter.nextNode();
            if (node.isNodeType("nt:file") && node.hasNode("jcr:content")) {
                if (CacheReporter.getIgnoreSet().contains(node.getName())) continue;
                Property lastModified = node.getProperty("jcr:content/jcr:lastModified");
                if (lastUpdated != null && lastModified.getLong() <= lastUpdated) continue;
                fileListingJSON.put((Object)CacheReporter.trimBasePath(rootNode.getPath(), node.getPath()));
                continue;
            }
            if (!node.isNodeType("nt:folder")) continue;
            this.addFileListing(fileListingJSON, rootNode, node, lastUpdated);
        }
    }

    private static Set<String> getIgnoreSet() {
        HashSet<String> ignoreSet = new HashSet<String>();
        ignoreSet.add("pge-package.json");
        ignoreSet.add("pge-package-update.json");
        return ignoreSet;
    }

    private static String trimBasePath(String basePath, String fullPath) {
        if (fullPath.startsWith(basePath)) {
            return fullPath.substring(basePath.length() + 1);
        }
        return fullPath;
    }
}