SitecatalystPostProcessor.java
6.28 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* 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.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.apache.sling.servlets.post.SlingPostProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.impl;
import com.day.cq.analytics.sitecatalyst.impl.util.ReportConfigUtils;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
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.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={SlingPostProcessor.class})
public class SitecatalystPostProcessor
implements SlingPostProcessor {
protected final Logger logger;
private final String NT_PAGE = "cq:Page";
private final String PAGE_MARKER = "/jcr:content/";
public static final String REPORT_NAME_SUFFIX = "ReportConfig";
public static final String PAGE_REPORTS_RESULTS_ROOT_NODE_NAME = "cq:meta";
public static final String PAGE_REPORTS_RESULTS_CURRENT = "last30Days";
public static final String PAGE_REPORTS_RESULTS_LAST_60_DAYS = "last30DaysPrev";
public static final String PAGE_REPORTS_RESULTS_LAST_90_DAYS = "last90Days";
public static final String PAGE_REPORTS_RESULTS_LAST_90_DAYS_PREVIOUS = "last90DaysPrev";
public static final String PAGE_REPORTS_RESULTS_THIS_YEAR = "thisYear";
public static final String PAGE_REPORTS_RESULTS_LAST_YEAR = "lastYear";
@Reference
private ConfigurationManagerFactory configManagerFactory;
public SitecatalystPostProcessor() {
this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
this.NT_PAGE = "cq:Page";
this.PAGE_MARKER = "/jcr:content/";
}
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
HashSet<String> fixPaths = new HashSet<String>();
for (Modification mod : changes) {
switch (mod.getType()) {
case ORDER:
case MOVE:
case COPY: {
break;
}
case MODIFY: {
int pageEndPos = mod.getSource().indexOf("/jcr:content/");
if (pageEndPos == -1) break;
String pagePath = mod.getSource().substring(0, pageEndPos);
fixPaths.add(pagePath);
break;
}
}
}
ResourceResolver resolver = request.getResourceResolver();
for (String pagePath : fixPaths) {
String contentPath = pagePath + '/' + "jcr:content";
this.logger.debug("Checking page {} for analytics configuration", (Object)pagePath);
Resource contentResource = resolver.getResource(contentPath);
if (contentResource == null) continue;
this.fixStructure(contentResource);
}
}
private void fixStructure(Resource resource) {
if (ResourceUtil.isA((Resource)resource.getParent(), (String)"cq:Page")) {
ValueMap props = (ValueMap)resource.adaptTo(ValueMap.class);
String[] configurations = (String[])props.get("cq:cloudserviceconfigs", (Object)new String[0]);
ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resource.getResourceResolver());
Configuration config = configManager.getConfiguration("sitecatalyst", configurations);
if (config != null) {
ReportConfigUtils.addAnalytics(resource, config.getResource(), configManager);
this.logger.debug("Adding analytics configuration to page {}", (Object)resource.getPath());
} else {
this.removeAnalytics(resource);
this.logger.debug("Removing analytics configuration from page {}", (Object)resource.getPath());
}
}
}
private void removeAnalytics(Resource resource) {
Resource analyticsRes = resource.getChild("analytics");
if (analyticsRes != null) {
try {
Node analyticsNode = (Node)analyticsRes.adaptTo(Node.class);
Node pageContentNode = analyticsNode.getParent();
ReportConfigUtils.recursivelyRemoveNodes(analyticsNode.getParent().getParent(), Arrays.asList("cq:meta"));
analyticsNode.remove();
pageContentNode.removeMixin("cq:metaMixin");
}
catch (RepositoryException e) {
this.logger.error(e.getMessage(), (Throwable)e);
}
}
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
}