AbstractPagesUpdateHandler.java
21.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.DownloadResource
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.contentsync.config.ConfigEntry
* com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler
* com.day.cq.contentsync.handler.util.RequestResponseFactory
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.RenditionPicker
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMMode
* com.day.cq.wcm.contentsync.PathRewriterOptions
* com.day.cq.wcm.contentsync.PathRewriterOptions$RewriteMode
* com.day.cq.wcm.foundation.Download
* com.day.cq.wcm.foundation.Image
* com.day.cq.wcm.foundation.WCMRenditionPicker
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* javax.servlet.ServletRequest
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* 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.commons.osgi.OsgiUtil
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.osgi.service.component.ComponentContext
* 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.adobe.cq.mobile.platform.impl.contentsync.handler.ResourceVisitor;
import com.adobe.cq.mobile.platform.impl.rewriter.HybridAppPathRewriterOptions;
import com.adobe.cq.mobile.platform.impl.rewriter.RenderMode;
import com.day.cq.commons.DownloadResource;
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.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.RenditionPicker;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.contentsync.PathRewriterOptions;
import com.day.cq.wcm.foundation.Download;
import com.day.cq.wcm.foundation.Image;
import com.day.cq.wcm.foundation.WCMRenditionPicker;
import com.day.text.Text;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
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.commons.osgi.OsgiUtil;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, inherit=1)
public abstract class AbstractPagesUpdateHandler
extends AbstractSlingResourceUpdateHandler {
protected final Logger log;
@Property(value={"foundation/components/image"}, cardinality=Integer.MAX_VALUE)
private static final String IMAGE_RESOURCE_TYPES = "cq.pagesupdatehandler.imageresourcetypes";
@Property(value={"foundation/components/video"}, cardinality=Integer.MAX_VALUE)
private static final String VIDEO_RESOURCE_TYPES = "cq.pagesupdatehandler.videoresourcetypes";
public static final String PN_EXTENSION_CONFIG_PROPERTY = "extension";
public static final String PN_SELECTOR_CONFIG_PROPERTY = "selector";
public static final String PN_DEEP_CONFIG_PROPERTY = "deep";
public static final String PN_IMAGES_CONFIG_PROPERTY = "includeImages";
public static final String PN_VIDEOS_CONFIG_PROPERTY = "includeVideos";
protected static final String PN_INC_MODIFIED_PAGES_ONLY = "includeModifiedPagesOnly";
public static final String PN_INCLUDED_PAGE_TYPES = "includedPageTypes";
public static final String PN_EXCLUDED_PAGE_TYPES = "excludedPageTypes";
protected static final String REWRITE_RELATIVE_PARENT = "relativeParentPath";
private static final String PARAMETER_APP_EXPORT = "appExport";
private static final String DEFAULT_REWRITE_MODE = PathRewriterOptions.RewriteMode.REWRITE_RELATIVE.name();
private PathRewriterOptions defaultOptions;
protected Map<String, Object> params;
private String[] imageResourceTypes;
private String[] videoResourceTypes;
protected String[] includedPageTypes;
protected String[] excludedPageTypes;
private boolean includeImages = false;
private boolean includeVideos = false;
private boolean isPublishMode = false;
protected AbstractPagesUpdateHandler() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Activate
protected void activate(ComponentContext context) {
this.imageResourceTypes = OsgiUtil.toStringArray(context.getProperties().get("cq.pagesupdatehandler.imageresourcetypes"));
this.videoResourceTypes = OsgiUtil.toStringArray(context.getProperties().get("cq.pagesupdatehandler.videoresourcetypes"));
}
protected HttpServletRequest createRequest(String uri) {
HttpServletRequest request = this.requestResponseFactory.createRequest("GET", uri, this.params);
WCMMode.DISABLED.toRequest((ServletRequest)request);
this.setRewriteOptions(request);
return request;
}
protected void setRewriteOptions(HttpServletRequest request) {
request.setAttribute("pathRewritingOptions", (Object)this.defaultOptions);
HybridAppPathRewriterOptions options = this.isPublishMode ? new HybridAppPathRewriterOptions(RenderMode.PUBLISH) : new HybridAppPathRewriterOptions(RenderMode.DEV);
request.setAttribute("hybridAppPathRewritingOptions", (Object)options);
}
public boolean isPublishModeEnabled() {
return this.isPublishMode;
}
public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
ResourceResolver resolver = this.resolverFactory.getResourceResolver(session);
Page rootPage = ((PageManager)resolver.adaptTo(PageManager.class)).getPage(this.getResolvedContentPath(configEntry));
boolean cacheUpdated = false;
ValueMap configOptions = ResourceUtil.getValueMap((Resource)resolver.getResource(configEntry.getPath()));
boolean deep = (Boolean)configOptions.get("deep", (Object)true);
this.includeImages = (Boolean)configOptions.get("includeImages", (Object)true);
this.includeVideos = (Boolean)configOptions.get("includeVideos", (Object)true);
this.initialize(configEntry, admin, session);
this.setupRunMode(configEntry, resolver);
this.createPathRewritingOptions(configEntry, resolver);
this.createParameterMap(configEntry, resolver);
this.setupPageTypes(configEntry);
try {
if (rootPage != null) {
String uri = this.buildURI(configEntry, rootPage);
if (StringUtils.isNotBlank((CharSequence)uri) && this.includePage(configEntry, rootPage) && this.isModified(rootPage, uri, lastUpdated, configEntry, configCacheRoot, admin)) {
cacheUpdated = this.renderResource(uri, configCacheRoot, admin, session) || cacheUpdated;
this.collectAssets(rootPage, configCacheRoot, admin, session);
admin.save();
}
if (deep) {
cacheUpdated = this.renderChildren(configEntry, rootPage.listChildren(), lastUpdated, configCacheRoot, admin, session) || cacheUpdated;
}
}
}
catch (Exception e) {
this.log.error("Rendering page failed: ", (Throwable)e);
}
return cacheUpdated;
}
protected void setupPageTypes(ConfigEntry configEntry) {
this.includedPageTypes = configEntry.getValues("includedPageTypes");
this.excludedPageTypes = configEntry.getValues("excludedPageTypes");
}
protected boolean includePage(ConfigEntry configEntry, Page pageToTest) {
boolean includePage;
boolean bl = includePage = this.includedPageTypes == null;
if (this.includedPageTypes != null) {
Resource pageAsResource = pageToTest.getContentResource();
if (this.excludedPageTypes != null && this.excludedPageTypes.length > 0) {
for (String excludeType : this.excludedPageTypes) {
if (!pageAsResource.isResourceType(excludeType)) continue;
this.log.debug("Excluding page: " + pageToTest.getPath());
includePage = false;
}
}
for (String type : this.includedPageTypes) {
if (!pageAsResource.isResourceType(type)) continue;
includePage = true;
break;
}
}
return includePage;
}
protected void createPathRewritingOptions(ConfigEntry configEntry, ResourceResolver resolver) {
ValueMap rewrite = ResourceUtil.getValueMap((Resource)resolver.getResource(configEntry.getPath() + "/rewrite"));
this.defaultOptions = new PathRewriterOptions(PathRewriterOptions.RewriteMode.valueOf((String)((String)rewrite.get("links", (Object)DEFAULT_REWRITE_MODE))), PathRewriterOptions.RewriteMode.valueOf((String)((String)rewrite.get("clientlibs", (Object)DEFAULT_REWRITE_MODE))), PathRewriterOptions.RewriteMode.valueOf((String)((String)rewrite.get("images", (Object)DEFAULT_REWRITE_MODE))));
if (rewrite.containsKey((Object)"relativeParentPath")) {
Resource topLevelResource;
String relativeParent = (String)rewrite.get("relativeParentPath", String.class);
if (StringUtils.isEmpty((CharSequence)relativeParent) && (topLevelResource = this.getTopLevelAppResource(resolver.getResource(this.getResolvedContentPath(configEntry)))) != null) {
relativeParent = topLevelResource.getPath();
}
this.defaultOptions.setRelativeParentPath(relativeParent);
}
}
protected void createParameterMap(ConfigEntry configEntry, ResourceResolver resolver) {
Resource topLevelResource;
ValueMap values = ResourceUtil.getValueMap((Resource)resolver.getResource(configEntry.getPath() + "/parameters"));
this.params = new HashMap<String, Object>();
for (String key : values.keySet()) {
if (key.startsWith("jcr:")) continue;
String value = (String)values.get(key, String.class);
if (value != null) {
this.params.put(key, value);
continue;
}
this.params.put(key, values.get(key, String[].class));
}
if (this.params.containsKey("appExport") && (topLevelResource = this.getTopLevelAppResource(resolver.getResource(this.getResolvedContentPath(configEntry)))) != null) {
this.defaultOptions.setRelativeParentPath(topLevelResource.getPath());
}
}
protected boolean renderChildren(ConfigEntry configEntry, Iterator<Page> children, Long lastUpdated, String configCacheRoot, Session admin, Session session) throws Exception {
boolean cacheUpdated = false;
while (children.hasNext()) {
Page childPage = children.next();
String uri = this.buildURI(configEntry, childPage);
if (StringUtils.isNotBlank((CharSequence)uri) && this.includePage(configEntry, childPage) && this.isModified(childPage, uri, lastUpdated, configEntry, configCacheRoot, admin)) {
cacheUpdated = this.renderResource(uri, configCacheRoot, admin, session) || cacheUpdated;
this.collectAssets(childPage, configCacheRoot, admin, session);
session.save();
}
cacheUpdated = this.renderChildren(configEntry, childPage.listChildren(), lastUpdated, configCacheRoot, admin, session) || cacheUpdated;
}
return cacheUpdated;
}
protected boolean isModified(Page page, String uri, Long lastUpdated, ConfigEntry configEntry, String configCacheRoot, Session session) throws RepositoryException {
if (!session.nodeExists(configCacheRoot + uri)) {
return true;
}
Calendar cal = page.getLastModified();
if (cal != null) {
long lastModified = cal.getTime().getTime();
return lastUpdated < lastModified || lastModified == -1;
}
return true;
}
protected boolean collectAssets(Page page, String configCacheRoot, Session admin, Session session) throws RepositoryException {
ComponentVisitor visitor = new ComponentVisitor(configCacheRoot, admin, session);
visitor.visit(page.getContentResource());
return visitor.cacheUpdated();
}
protected String buildURI(ConfigEntry entry, Page page) {
if (page == null) {
return null;
}
String uri = page.getPath();
if (entry.getValue("selector") != null) {
uri = uri + "." + entry.getValue("selector");
}
if (entry.getValue("extension") != null) {
uri = uri + "." + entry.getValue("extension");
}
return uri;
}
protected boolean includeOnlyModifiedPages(ConfigEntry configEntry) {
return Boolean.parseBoolean(configEntry.getValue("includeModifiedPagesOnly"));
}
protected Resource getTopLevelAppResource(Resource resource) {
MobileResourceLocator locator = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
MobileResource mobileResource = locator.findClosestResourceByType(resource, MobileResourceType.CONTENT.getType(), new String[0]);
if (mobileResource != null) {
for (Resource child : ((Resource)mobileResource.adaptTo(Resource.class)).getChildren()) {
if (!child.isResourceType("cq:Page") || !Text.isDescendantOrEqual((String)child.getPath(), (String)resource.getPath())) continue;
return child;
}
}
return null;
}
protected void setupRunMode(ConfigEntry configEntry, ResourceResolver resolver) {
Resource configResource = resolver.getResource(configEntry.getPath());
String renderMode = this.getInheritedProperty(configResource, "renderMode", String.class);
try {
if (StringUtils.isNotBlank((CharSequence)renderMode)) {
RenderMode mode = RenderMode.valueOf(renderMode.toUpperCase());
this.isPublishMode = mode == RenderMode.PUBLISH;
this.log.debug("Handler has been configured to run in PUBLISH mode");
}
}
catch (IllegalArgumentException e) {
this.log.warn("Unable to distinguish render mode from {} falling back to DEV mode", (Object)renderMode);
}
}
protected void initialize(ConfigEntry configEntry, Session adminSession, Session userSession) {
}
protected <T> T getInheritedProperty(Resource configEntry, String property, Class<T> propType) {
Object prodProp = configEntry.getValueMap().get(property, propType);
if (prodProp == null) {
prodProp = configEntry.getParent().getValueMap().get(property, propType);
}
return (T)prodProp;
}
protected String getTargetPath(String path) {
return StringUtils.replace((String)path, (String)"jcr:content", (String)"jcr_content");
}
protected class ComponentVisitor
extends ResourceVisitor {
private static final String DOWNLOAD_RESOURCE_TYPE = "foundation/components/download";
private String configCacheRoot;
private Session admin;
private Session session;
private ResourceResolver resolver;
private boolean cacheUpdated;
public ComponentVisitor(String configCacheRoot, Session admin, Session session) {
this.cacheUpdated = false;
this.configCacheRoot = configCacheRoot;
this.admin = admin;
this.session = session;
this.resolver = AbstractPagesUpdateHandler.this.resolverFactory.getResourceResolver(session);
}
@Override
protected void accept(Resource res) {
try {
if (AbstractPagesUpdateHandler.this.includeImages && this.isA(res, AbstractPagesUpdateHandler.this.imageResourceTypes)) {
Image image = new Image(res);
image.setSelector(".img");
this.cacheUpdated = this.updateImageResource(image) || this.cacheUpdated;
} else if (AbstractPagesUpdateHandler.this.includeVideos && this.isA(res, AbstractPagesUpdateHandler.this.videoResourceTypes)) {
Download download = new Download(res);
ValueMap properties = (ValueMap)res.adaptTo(ValueMap.class);
boolean copyToDevice = (Boolean)properties.get("copyToDevice", (Object)false);
this.cacheUpdated = this.updateVideoResource((DownloadResource)download, copyToDevice) || this.cacheUpdated;
} else if (ResourceUtil.isA((Resource)res, (String)"foundation/components/download")) {
Download download = new Download(res);
this.cacheUpdated = this.updateDownloadResource((DownloadResource)download) || this.cacheUpdated;
}
}
catch (RepositoryException e) {
AbstractPagesUpdateHandler.this.log.error("Updating page dependencies failed: ", (Throwable)e);
}
}
private boolean updateImageResource(Image image) {
boolean cacheModified = false;
try {
cacheModified = AbstractPagesUpdateHandler.this.renderResource(image.getHref(), this.configCacheRoot, this.admin, this.session);
}
catch (Exception e) {
AbstractPagesUpdateHandler.this.log.error("Rendering image resource failed: ", (Throwable)e);
}
return cacheModified;
}
private boolean updateVideoResource(DownloadResource download, boolean copyToDevice) throws RepositoryException {
boolean cacheModified = false;
try {
if (copyToDevice) {
String path = download.getResource().getPath() + ".original.mp4";
cacheModified = AbstractPagesUpdateHandler.this.renderResource(path, this.configCacheRoot, this.admin, this.session);
}
String poster = download.getResource().getPath() + ".thumb.png";
cacheModified |= AbstractPagesUpdateHandler.this.renderResource(poster, this.configCacheRoot, this.admin, this.session);
}
catch (Exception e) {
AbstractPagesUpdateHandler.this.log.error("Rendering resource failed: ", (Throwable)e);
}
return cacheModified;
}
private boolean updateDownloadResource(DownloadResource download) throws RepositoryException {
boolean cacheModified = false;
if (download.hasContent()) {
String srcPath = download.getFileReference();
if (!srcPath.equals("")) {
Rendition rendition;
Resource srcRes = this.resolver.getResource(srcPath);
if (srcRes != null && srcRes.adaptTo(Asset.class) != null && (rendition = ((Asset)srcRes.adaptTo(Asset.class)).getRendition((RenditionPicker)new WCMRenditionPicker())) != null && rendition.adaptTo(Node.class) != null) {
String cacheParentPath = this.configCacheRoot + Text.getRelativeParent((String)download.getHref(), (int)1);
Node parent = JcrUtil.createPath((String)cacheParentPath, (String)"sling:Folder", (Session)this.session);
JcrUtil.copy((Node)((Node)rendition.adaptTo(Node.class)), (Node)parent, (String)Text.getName((String)download.getHref()));
cacheModified = true;
}
} else {
srcPath = download.getFileNodePath();
Resource srcRes = this.resolver.getResource(srcPath);
if (srcRes != null) {
String cacheParentPath = this.configCacheRoot + Text.getRelativeParent((String)download.getHref(), (int)1);
Node parent = JcrUtil.createPath((String)cacheParentPath, (String)"sling:Folder", (Session)this.session);
JcrUtil.copy((Node)((Node)srcRes.adaptTo(Node.class)), (Node)parent, (String)Text.getName((String)download.getHref()));
cacheModified = true;
}
}
}
return cacheModified;
}
private boolean isA(Resource resource, String[] resourceTypes) {
for (String type : resourceTypes) {
if (!ResourceUtil.isA((Resource)resource, (String)type)) continue;
return true;
}
return false;
}
protected boolean cacheUpdated() {
return this.cacheUpdated;
}
}
}