MobileAssetUpdateHandler.java
7.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.contentsync.config.ConfigEntry
* com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.search.PredicateGroup
* com.day.cq.search.Query
* com.day.cq.search.QueryBuilder
* com.day.cq.search.result.SearchResult
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.contentsync.handler;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import com.day.text.Text;
import java.util.HashMap;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, factory="com.day.cq.contentsync.handler.ContentUpdateHandler/app-assets")
public class MobileAssetUpdateHandler
extends AbstractSlingResourceUpdateHandler {
private static final Logger log = LoggerFactory.getLogger(MobileAssetUpdateHandler.class);
public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
boolean changed = false;
try {
MobileResourceLocator mobileResMgr;
MobileResource appInstance;
ResourceResolver resourceResolver = this.resolverFactory.getResourceResolver(admin);
Resource resource = resourceResolver.getResource(this.getResolvedContentPath(configEntry));
configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
Resource appContentRoot = this.findAppRoot(resource);
if (appContentRoot != null) {
Iterator<Resource> assets = this.getAssets(appContentRoot, "nt:file", admin);
while (assets.hasNext()) {
String cachePath;
Resource asset = assets.next();
Resource assetContent = asset.getChild("jcr:content");
if (this.isModified(assetContent, lastUpdated, cachePath = this.getCachePath(configCacheRoot, asset, appContentRoot), admin)) {
Node parent = JcrUtil.createPath((String)Text.getRelativeParent((String)cachePath, (int)1), (String)"sling:Folder", (Session)admin);
JcrUtil.copy((Node)((Node)asset.adaptTo(Node.class)), (Node)parent, (String)asset.getName());
changed = true;
}
admin.save();
}
}
if ((appInstance = (mobileResMgr = (MobileResourceLocator)resourceResolver.adaptTo(MobileResourceLocator.class)).findClosestResourceByType(resource, MobileResourceType.INSTANCE.getType(), new String[0])) != null) {
String appAssetsPath = (String)appInstance.getProperties().get("appAssetPath", String.class);
Resource assetSourceRes = null;
if (StringUtils.isNotEmpty((String)appAssetsPath)) {
assetSourceRes = resourceResolver.getResource(appAssetsPath);
}
if (assetSourceRes != null) {
Iterator<Resource> assets = this.getAssets(assetSourceRes, "dam:Asset", admin);
while (assets.hasNext()) {
Resource assetRes = assets.next();
String cachePath = this.getCachePath(configCacheRoot, assetRes, assetSourceRes);
Asset asset = (Asset)assetRes.adaptTo(Asset.class);
if (this.isModified(asset.getOriginal(), lastUpdated, cachePath, admin)) {
Node parent = JcrUtil.createPath((String)Text.getRelativeParent((String)cachePath, (int)1), (String)"sling:Folder", (Session)admin);
JcrUtil.copy((Node)((Node)asset.getOriginal().adaptTo(Node.class)), (Node)parent, (String)asset.getName());
changed = true;
}
admin.save();
}
}
}
}
catch (Exception e) {
log.error("Update of cache entry failed: ", (Throwable)e);
}
return changed;
}
protected Iterator<Resource> getAssets(Resource assetFolder, String type, Session session) throws RepositoryException {
ResourceResolver resolver = this.resolverFactory.getResourceResolver(session);
HashMap<String, String> map = new HashMap<String, String>();
map.put("path", assetFolder.getPath());
map.put("type", type);
QueryBuilder queryBuilder = (QueryBuilder)resolver.adaptTo(QueryBuilder.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
query.setStart(0);
query.setHitsPerPage(0);
SearchResult searchResult = query.getResult();
return searchResult.getResources();
}
private String getCachePath(String configCacheRoot, Resource asset, Resource rootResource) {
String rootPath = rootResource.getPath();
String assetPath = asset.getPath();
if (assetPath.startsWith(rootPath)) {
assetPath = assetPath.substring(rootPath.length());
}
return configCacheRoot + assetPath;
}
protected boolean isModified(Resource resource, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
return this.isModified(resource.getValueMap(), lastUpdated, cachePath, session);
}
protected boolean isModified(Rendition rendition, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
return this.isModified(rendition.getProperties(), lastUpdated, cachePath, session);
}
private boolean isModified(ValueMap properties, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
Long lastModified = (Long)properties.get("jcr:lastModified", Long.class);
return !session.nodeExists(cachePath) || lastUpdated < lastModified;
}
private Resource findAppRoot(Resource baseRes) {
Resource r;
if (baseRes != null && baseRes.getChild("www") != null) {
return baseRes;
}
Resource childRes = null;
Iterable allChildren = baseRes.getChildren();
Iterator i$ = allChildren.iterator();
while (i$.hasNext() && ("jcr:content".equals((r = (Resource)i$.next()).getName()) || (childRes = this.findAppRoot(r)) == null || ResourceUtil.isNonExistingResource((Resource)childRes))) {
}
return childRes;
}
}