AppCacheUtil.java
9.06 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.contentsync.config.Config
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.security.Privilege
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.appcache.impl;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.CacheUpdate;
import com.adobe.cq.mobile.appcache.impl.ConfigUtil;
import com.day.cq.contentsync.config.Config;
import java.security.Principal;
import java.util.Calendar;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.Privilege;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AppCacheUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(AppCacheUtil.class);
private static final String PID_CONTENTSYNC_MANAGER = "com.day.cq.contentsync.impl.ContentSyncManagerImpl";
private static final String CONFIG_KEY_FALLBACK_AUTHORIZABLE = "contentsync.fallback.authorizable";
private static final String DEFAULT_FALLBACK_AUTHORIZABLE = "everyone";
private static final String NN_REP_POLICY = "/rep:policy";
/*
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public static String getZipPath(AppCacheManager appCacheManager, Config config, Date ifModifiedSince, Session session) {
String zipPath = null;
try {
CacheUpdate latestCacheUpdate = appCacheManager.getLastUpdate(config);
if (latestCacheUpdate != null) {
if (ifModifiedSince != null) {
long before = -1;
long latest = latestCacheUpdate.getCreated().getTimeInMillis();
long ifModifiedSinceTimestamp = ifModifiedSince.getTime();
LOGGER.debug("ifModifiedSinceTimestamp,before,latest: " + ifModifiedSinceTimestamp + ", " + before + ", " + latest);
if (ifModifiedSinceTimestamp >= latest) {
LOGGER.debug("client already has the latest content " + config.getPath());
return null;
}
List<CacheUpdate> cacheUpdateList = appCacheManager.getUpdates(config);
for (CacheUpdate cacheUpdate : cacheUpdateList) {
long cacheUpdateTs = cacheUpdate.getCreated().getTimeInMillis();
LOGGER.debug("ifModifiedSinceTimestamp,before,updatets: " + ifModifiedSinceTimestamp + ", " + before + ", " + "" + cacheUpdateTs);
if (cacheUpdateTs > ifModifiedSinceTimestamp) continue;
LOGGER.debug("updating before to updatets: " + before + ", " + cacheUpdateTs);
before = cacheUpdateTs;
break;
}
String cachePath = ConfigUtil.configToCachePath(config, session);
String zipsCachePath = cachePath + "-zips" + "/" + ConfigUtil.getConfigName(config, session);
if (before == -1) {
zipPath = zipsCachePath + "." + latest + ".zip";
} else if (before != -1 && latest != before) {
zipPath = zipsCachePath + "." + before + "." + latest + ".zip";
}
} else {
LOGGER.warn("No modifiedSince provided " + config.getPath());
}
} else {
LOGGER.debug("No cache updates available " + config.getPath());
}
}
catch (RepositoryException ex) {
LOGGER.error("Failed to find content update zip path " + config.getPath());
}
LOGGER.info("Zip path for config " + config.getPath() + " and ifModifiedSince " + ifModifiedSince + " was " + zipPath);
return zipPath;
}
public static void setupCacheACLs(ResourceResolverFactory resolverFactory, Config config, Session userSession, boolean onCache, boolean onZips, boolean onMetadata, String authorizableId) throws RepositoryException, LoginException {
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null);
Session aclSession = (Session)resolver.adaptTo(Session.class);
String cachePath = ConfigUtil.configToCachePath(config, aclSession);
String zipsCachePath = ConfigUtil.configToZipCachePath(config, aclSession);
String cacheMetadataPath = ConfigUtil.configToMetadataCachePath(config, aclSession);
boolean updated = false;
Privilege[] readPrivileges = AccessControlUtils.privilegesFromNames((Session)aclSession, (String[])new String[]{"{http://www.jcp.org/jcr/1.0}read"});
if (onCache && aclSession.nodeExists(cachePath) && !aclSession.nodeExists(cachePath + "/rep:policy")) {
updated |= AppCacheUtil.setACLs(aclSession, userSession, cachePath, authorizableId, readPrivileges);
}
if (onZips && aclSession.nodeExists(zipsCachePath) && !aclSession.nodeExists(zipsCachePath + "/rep:policy")) {
updated |= AppCacheUtil.setACLs(aclSession, userSession, zipsCachePath, authorizableId, readPrivileges);
}
if (onMetadata && aclSession.nodeExists(cacheMetadataPath) && !aclSession.nodeExists(cacheMetadataPath + "/rep:policy")) {
updated |= AppCacheUtil.setACLs(aclSession, userSession, cacheMetadataPath, authorizableId, readPrivileges);
}
if (updated) {
aclSession.save();
}
if (resolver != null) {
resolver.close();
}
}
private static boolean setACLs(Session aclSession, Session userSession, String path, String authorizableId, Privilege[] privileges) throws RepositoryException {
boolean aclsSet = false;
UserManager userManager = ((JackrabbitSession)userSession).getUserManager();
Authorizable authorizable = userManager.getAuthorizable(authorizableId);
if (authorizable != null) {
aclsSet = AccessControlUtils.addAccessControlEntry((Session)aclSession, (String)path, (Principal)authorizable.getPrincipal(), (Privilege[])privileges, (boolean)true);
}
return aclsSet;
}
private static Dictionary loadProperties(ConfigurationAdmin configurationAdmin) {
Dictionary properties2;
Dictionary properties2 = new Hashtable();
try {
String pidFilter = null;
Configuration[] configs = configurationAdmin.listConfigurations(pidFilter);
for (int i = 0; i < configs.length; ++i) {
Configuration config = configs[i];
String pid = config.getPid();
if (!pid.equals("com.day.cq.contentsync.impl.ContentSyncManagerImpl")) continue;
properties2 = config.getProperties();
break;
}
}
catch (Exception ex) {
LOGGER.warn("Failed to read properties for service com.day.cq.contentsync.impl.ContentSyncManagerImpl", (Throwable)ex);
}
return properties2;
}
public static String getAuthorizable(ConfigurationAdmin configurationAdmin, Config config) throws RepositoryException {
Dictionary contentSyncManagerProperties = AppCacheUtil.loadProperties(configurationAdmin);
String fallbackauthorizable = "everyone";
Object fallbackAuthorizableConfig = contentSyncManagerProperties.get("contentsync.fallback.authorizable");
if (fallbackAuthorizableConfig != null) {
fallbackauthorizable = fallbackAuthorizableConfig.toString();
}
String authorizable = fallbackauthorizable;
if (config.getAuthorizable() != null) {
authorizable = config.getAuthorizable();
} else {
LOGGER.warn("Configured fallback authorizable \"{}\" is used for cache's access permissions. Please make sure your content sync configuration contains a valid \"authorizable\" property!", (Object)fallbackauthorizable);
}
return authorizable;
}
}