ConfigUtil.java
4.91 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* 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;
}
}