ManifestXMLGenerator.java 5.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.XSSAPI
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.nodetype.NodeType
 *  org.apache.commons.codec.binary.Base64
 *  org.apache.commons.codec.digest.DigestUtils
 *  org.apache.commons.lang.StringUtils
 */
package com.adobe.cq.mobile.dps.impl.metadata;

import com.adobe.granite.xss.XSSAPI;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.activation.MimetypesFileTypeMap;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

public class ManifestXMLGenerator {
    private static final String KEY_TYPE = "type";
    private static final String KEY_LENGTH = "length";
    private static final String KEY_HASH = "hash";
    private XSSAPI xssAPI = null;

    public ManifestXMLGenerator(XSSAPI xssAPI) {
        this.xssAPI = xssAPI;
    }

    public void generatePKGPropertiesXMLContent(String rootPage, PrintWriter writer, Map<String, Map<String, String>> contentMetadata) throws Exception {
        try {
            String version = "3.0.0";
            String targetViewer = "33.0.0";
            String modified = "2014-10-23T20:42:23Z";
            String href = rootPage;
            writer.println("<manifest version='" + version + "' targetViewer='" + targetViewer + "' dateModified='" + modified + "'>");
            writer.println("<index type='text/html' href='" + href + "'/>");
            writer.println("<resources>");
            for (Map.Entry<String, Map<String, String>> entry : contentMetadata.entrySet()) {
                Map<String, String> info = entry.getValue();
                writer.println("<resource type='" + this.xssAPI.encodeForXMLAttr(info.get("type")) + "' href='" + this.xssAPI.encodeForXMLAttr(entry.getKey()) + "' length='" + this.xssAPI.encodeForXMLAttr(String.valueOf(info.get("length"))) + "' md5='" + this.xssAPI.encodeForXMLAttr(info.get("hash")) + "'/>");
            }
            writer.println("</resources>");
            writer.println("</manifest>");
        }
        catch (Exception ex) {
            throw new Exception("Failed to write PKGProperties XML for: " + contentMetadata, ex);
        }
    }

    public static Map<String, Map<String, String>> getPageMetadata(Node folioZipRootNode) throws RepositoryException, Exception {
        LinkedHashMap<String, Map<String, String>> metadata = new LinkedHashMap<String, Map<String, String>>();
        ManifestXMLGenerator.addPageMetadata(metadata, folioZipRootNode, folioZipRootNode);
        return metadata;
    }

    private static void addPageMetadata(Map<String, Map<String, String>> fileMetadata, Node folioZipRootNode, Node currentDirNode) throws RepositoryException, Exception {
        String date = "2012-06-03T23:42:32Z";
        NodeIterator folioContentIter = currentDirNode.getNodes();
        while (folioContentIter.hasNext()) {
            Node fileNode = folioContentIter.nextNode();
            if (fileNode.getPrimaryNodeType().getName().equalsIgnoreCase("nt:file")) {
                if (ManifestXMLGenerator.getIgnoreSet().contains(fileNode.getName())) continue;
                HashMap<String, String> info = new HashMap<String, String>();
                String filename = fileNode.getName();
                String type = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filename);
                String href = ManifestXMLGenerator.trimBasePath(folioZipRootNode.getPath(), fileNode.getPath());
                if (href.indexOf("jcr:content") > 0) {
                    href = StringUtils.replace((String)href, (String)"jcr:content", (String)"_jcr_content");
                }
                Property binaryProperty = fileNode.getProperty("jcr:content/jcr:data");
                long size = binaryProperty.getLength();
                String md5 = Base64.encodeBase64String((byte[])DigestUtils.md5((InputStream)binaryProperty.getBinary().getStream()));
                info.put("type", type);
                info.put("hash", md5);
                info.put("length", String.valueOf(size));
                fileMetadata.put(href, info);
                continue;
            }
            if (!fileNode.getPrimaryNodeType().getName().equalsIgnoreCase("sling:folder")) continue;
            ManifestXMLGenerator.addPageMetadata(fileMetadata, folioZipRootNode, fileNode);
        }
    }

    private static Set<String> getIgnoreSet() {
        HashSet<String> ignoreSet = new HashSet<String>();
        ignoreSet.add("mimetype");
        ignoreSet.add("pkgproperties.xml");
        return ignoreSet;
    }

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