TargetedContentManagerImpl.java
13.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Filter
* com.day.cq.personalization.TargetedContentManager
* com.day.cq.personalization.TeaserUtils
* com.day.cq.tagging.Tag
* com.day.cq.wcm.api.Page
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.Event
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.personalization.impl;
import com.day.cq.commons.Filter;
import com.day.cq.personalization.TargetedContentManager;
import com.day.cq.personalization.TeaserUtils;
import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class TargetedContentManagerImpl
implements TargetedContentManager,
EventListener {
private final Logger log = LoggerFactory.getLogger(TargetedContentManagerImpl.class);
static final String TEASER_TYPE = "cq/personalization/components/teaserpage";
static final String EXPERIENCE_TYPE = "cq/personalization/components/experiencepage";
static final String CAMPAIGN_TYPE = "cq/personalization/components/campaignpage";
static final String DEFAULT_CAMPAIGN_ROOT = "/content/campaigns";
private static final String CAMPAIGN_ROOT = "cq.mcm.campaign.root";
private ResourceResolverFactory resolverFactory = null;
private String promotionRoot;
private ResourceResolver adminResolver;
private ObservationManager observationManager;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private List<String> campaignPathsCache = new ArrayList<String>();
private boolean cacheDirty = true;
protected void activate(ComponentContext context) throws Exception {
this.promotionRoot = OsgiUtil.toString(context.getProperties().get("cq.mcm.campaign.root"), (String)"/content/campaigns");
this.adminResolver = this.resolverFactory.getAdministrativeResourceResolver(Collections.emptyMap());
Session adminSession = (Session)this.adminResolver.adaptTo(Session.class);
this.observationManager = adminSession.getWorkspace().getObservationManager();
this.observationManager.addEventListener((EventListener)this, 63, "/", true, null, null, true);
}
protected void deactivate(ComponentContext context) throws RepositoryException {
if (this.adminResolver != null && this.adminResolver.isLive()) {
if (this.observationManager != null) {
this.observationManager.removeEventListener((EventListener)this);
}
this.adminResolver.close();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public void onEvent(EventIterator events) {
this.lock.writeLock().lock();
try {
while (events.hasNext()) {
Event e = events.nextEvent();
try {
String path = e.getPath();
if (!path.startsWith(this.promotionRoot)) continue;
this.cacheDirty = true;
return;
}
catch (RepositoryException e1) {
this.log.error("Failed to invalidate cache {}", (Object)e);
continue;
}
}
return;
}
finally {
this.lock.writeLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public JSONObject getTeaserInfo(ResourceResolver resolver, String campaignPath, String target) {
this.lock.readLock().lock();
try {
this.validateCache();
String defaultTeaser = null;
JSONObject json = new JSONObject();
JSONArray allTeasers = new JSONArray();
for (String cachedCampaignPath : this.campaignPathsCache) {
String title;
String[] segments;
Page campaign = this.getPage(resolver, cachedCampaignPath);
if (campaign == null || !campaign.isValid()) continue;
String campaignDefaultTeaser = null;
String campaignName = campaign.getName();
if (target != null) {
Iterator experiences = campaign.listChildren((Filter)new ResourceTypeFilter("cq/personalization/components/experiencepage"));
while (experiences.hasNext()) {
Page experience = (Page)experiences.next();
title = experience.getTitle();
segments = (String[])experience.getProperties().get("cq:segments", (Object)new String[0]);
Iterator teasers = experience.listChildren((Filter)new ResourceTypeFilter("cq/personalization/components/teaserpage"));
while (teasers.hasNext()) {
Page teaser = (Page)teasers.next();
if (!((String)teaser.getProperties().get("target", (Object)"")).equals(target)) continue;
if (segments.length == 0 && teaser.getTags().length == 0) {
campaignDefaultTeaser = teaser.getPath();
}
allTeasers.put((Object)this.getTeaserInfo(campaignName, title, segments, teaser));
}
}
}
if (campaign.getPath().equals(campaignPath)) {
Iterator teasers = campaign.listChildren((Filter)new ResourceTypeFilter("cq/personalization/components/teaserpage"));
while (teasers.hasNext()) {
Page teaser = (Page)teasers.next();
if (!teaser.isValid()) continue;
title = teaser.getTitle();
segments = (String[])teaser.getProperties().get("cq:segments", (Object)new String[0]);
if (segments.length == 0 && teaser.getTags().length == 0) {
campaignDefaultTeaser = teaser.getPath();
}
allTeasers.put((Object)this.getTeaserInfo(campaignName, title, segments, teaser));
}
}
if (defaultTeaser != null || campaignDefaultTeaser == null) continue;
defaultTeaser = campaignDefaultTeaser;
}
json.put("allTeasers", (Object)allTeasers);
json.put("defaultTeaserPath", (Object)defaultTeaser);
JSONObject i$ = json;
return i$;
}
catch (JSONException e) {
this.log.error("Failed to generate teaser info", (Throwable)e);
JSONObject json = new JSONObject();
return json;
}
finally {
this.lock.readLock().unlock();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public List<Page> getCampaigns(ResourceResolver resourceResolver) throws RepositoryException {
ArrayList<Page> campaigns;
campaigns = new ArrayList<Page>();
this.lock.readLock().lock();
try {
this.validateCache();
for (String campaignPath : this.campaignPathsCache) {
Page campaignPage = this.getPage(resourceResolver, campaignPath);
if (campaignPage == null) continue;
campaigns.add(campaignPage);
}
}
finally {
this.lock.readLock().unlock();
}
Collections.sort(campaigns, CampaignPrioritySorter.INSTANCE);
return campaigns;
}
private Page getPage(ResourceResolver resourceResolver, String campaignPath) {
Resource resource = resourceResolver.getResource(campaignPath);
if (resource == null) {
return null;
}
return (Page)resource.adaptTo(Page.class);
}
private JSONObject getTeaserInfo(String campaignName, String title, String[] segments, Page teaser) throws JSONException {
JSONObject json = new JSONObject();
json.put("path", (Object)teaser.getPath());
json.put("name", (Object)teaser.getName());
json.put("title", (Object)title);
json.put("campainName", (Object)campaignName);
json.put("thumbnail", (Object)TeaserUtils.getImage((Page)teaser));
json.put("id", (Object)(campaignName + "_" + teaser.getName()));
JSONArray segmentsArray = new JSONArray();
for (String s : segments) {
segmentsArray.put((Object)s);
}
json.put("segments", (Object)segmentsArray);
JSONArray tagsArray = new JSONArray();
for (Tag tag : teaser.getTags()) {
JSONObject tagJson = new JSONObject();
tagJson.put("name", (Object)tag.getName());
tagJson.put("title", (Object)tag.getTitle());
tagJson.put("titlePath", (Object)tag.getTitlePath());
tagJson.put("path", (Object)tag.getPath());
tagJson.put("tagID", (Object)tag.getTagID());
tagsArray.put((Object)tagJson);
}
json.put("tags", (Object)tagsArray);
return json;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void validateCache() {
if (this.cacheDirty) {
this.lock.readLock().unlock();
this.lock.writeLock().lock();
try {
if (this.cacheDirty) {
this.campaignPathsCache.clear();
this.collector(this.adminResolver.getResource(this.promotionRoot));
this.cacheDirty = false;
}
}
finally {
this.lock.readLock().lock();
this.lock.writeLock().unlock();
}
}
}
private void collector(Resource r) {
if (r.isResourceType("cq/personalization/components/campaignpage")) {
try {
Page c = (Page)r.getParent().adaptTo(Page.class);
this.campaignPathsCache.add(c.getPath());
}
catch (Exception e) {
this.log.error(e.getMessage());
}
} else {
Iterator i = r.listChildren();
while (i.hasNext()) {
this.collector((Resource)i.next());
}
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private class ResourceTypeFilter
implements Filter<Page> {
private final String type;
ResourceTypeFilter(String type) {
this.type = type;
}
public boolean includes(Page page) {
if (page == null) {
return false;
}
Resource r = page.getContentResource();
return r != null && ResourceUtil.isA((Resource)r, (String)this.type);
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
static class CampaignPrioritySorter
implements Comparator<Page> {
public static final CampaignPrioritySorter INSTANCE = new CampaignPrioritySorter();
private CampaignPrioritySorter() {
}
@Override
public int compare(Page o1, Page o2) {
int p2;
int p1 = (Integer)ResourceUtil.getValueMap((Resource)o1.getContentResource()).get("priority", (Object)100);
return p1 < (p2 = ((Integer)ResourceUtil.getValueMap((Resource)o2.getContentResource()).get("priority", (Object)100)).intValue()) ? 1 : (p1 != p2 ? -1 : 0);
}
}
}