ManifestXMLGenerator.java
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* 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;
}
}