ConfigUtil.java 4.91 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.contentsync.config.Config
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.cq.mobile.appcache.impl;

import com.day.cq.contentsync.config.Config;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;

public class ConfigUtil {
    public static final String CACHE_ROOT = "/var/contentsync";
    public static final String TIMESTAMPS_PROPERTY = "timestamps";
    public static final String ZIPS_SUFFIX = "-zips";
    public static final String UPDATE_METADATA_SUFFIX = "-updateMetadata";
    @Deprecated
    private static final String CACHE_PROPERTY = "cache";

    public static String configToCachePath(Config config, Session session) throws RepositoryException {
        return "/var/contentsync" + Text.getRelativeParent((String)config.getPath(), (int)1) + "/" + ConfigUtil.getConfigName(config, session);
    }

    public static String configToZipCachePath(Config config, Session session) throws RepositoryException {
        String cachePath = ConfigUtil.configToCachePath(config, session);
        String zipCachePath = cachePath + "-zips";
        return zipCachePath;
    }

    public static String configToMetadataCachePath(Config config, Session session) throws RepositoryException {
        String cachePath = ConfigUtil.configToCachePath(config, session);
        String updateMetadataPath = cachePath + "-updateMetadata";
        return updateMetadataPath;
    }

    public static String cachePathFromRequestURI(String requestURI) {
        int pos = requestURI.lastIndexOf("/");
        StringBuilder cachePath = new StringBuilder("/var/contentsync");
        cachePath.append(requestURI.substring(0, pos));
        cachePath.append(requestURI.substring(pos).replaceFirst("(\\.\\d*)*.zip", ""));
        cachePath.append("-zips");
        cachePath.append(requestURI.substring(pos));
        cachePath.append("/jcr:content/jcr:data");
        return cachePath.toString();
    }

    public static String cacheToConfigPath(String path) {
        return path.startsWith("/var/contentsync") ? path.replaceFirst("/var/contentsync", "") : null;
    }

    public static boolean isConfig(Resource resource) {
        try {
            return ((Node)resource.adaptTo(Node.class)).isNodeType("cq:ContentSyncConfig");
        }
        catch (RepositoryException e) {
            return false;
        }
    }

    public static boolean isConfigEntry(Resource resource) {
        try {
            Node node = (Node)resource.adaptTo(Node.class);
            return node.getParent().isNodeType("cq:ContentSyncConfig") && node.hasProperty("type") && node.hasProperty("path");
        }
        catch (RepositoryException e) {
            return false;
        }
    }

    public static boolean isCached(Config config, Session session) throws RepositoryException {
        String cachePath = ConfigUtil.configToCachePath(config, session);
        return session.itemExists(cachePath) && session.itemExists(cachePath + "/" + "timestamps");
    }

    public static boolean isPersonalized(String configPath, Session session) throws RepositoryException {
        String personalizedPath = configPath + "/" + "personalized";
        if (session.itemExists(personalizedPath)) {
            return session.getProperty(personalizedPath).getBoolean();
        }
        String cachePath = configPath + "/" + "cache";
        if (session.itemExists(cachePath)) {
            return !session.getProperty(cachePath).getBoolean();
        }
        return false;
    }

    public static String getConfigName(Config config, Session session) throws RepositoryException {
        if (ConfigUtil.isPersonalized(config.getPath(), session)) {
            return config.getName() + "." + session.getUserID();
        }
        return config.getName();
    }

    public static List<Long> getCacheUpdates(Config config, Session session) {
        ArrayList<Long> updates = null;
        try {
            ArrayList<Long> list = new ArrayList<Long>();
            String path = ConfigUtil.configToCachePath(config, session);
            if (session.nodeExists(path)) {
                Node cacheNode = session.getNode(path);
                Value[] timestampsArray = cacheNode.getProperty("timestamps").getValues();
                for (int i = 0; i < timestampsArray.length; ++i) {
                    Long ts = timestampsArray[i].getLong();
                    list.add(ts);
                }
            }
            updates = list;
        }
        catch (Exception ex) {
            // empty catch block
        }
        return updates;
    }
}