ActivateCampaignPage.java
10.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationOptions
* com.day.cq.replication.ReplicationStatus
* com.day.cq.replication.Replicator
* com.day.cq.wcm.api.reference.Reference
* com.day.cq.wcm.api.reference.ReferenceProvider
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.security.AccessControlManager
* javax.jcr.security.Privilege
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.impl;
import com.adobe.cq.mcm.campaign.NewsletterReplicationException;
import com.adobe.cq.mcm.campaign.Publisher;
import com.day.cq.mcm.campaign.impl.AbstractCampaignProcess;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Property(name="process.label", value={"Adobe Campaign: Activate Page"})
public class ActivateCampaignPage
extends AbstractCampaignProcess {
private final Logger log;
@org.apache.felix.scr.annotations.Reference
private ResourceResolverFactory resolverFactory;
@org.apache.felix.scr.annotations.Reference(referenceInterface=ReferenceProvider.class, cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private final List<ReferenceProvider> referenceProviders;
@org.apache.felix.scr.annotations.Reference
protected Replicator replicator;
@org.apache.felix.scr.annotations.Reference
protected EventAdmin eventAdmin;
@org.apache.felix.scr.annotations.Reference
protected Publisher publisher;
public ActivateCampaignPage() {
this.log = LoggerFactory.getLogger(this.getClass());
this.referenceProviders = new CopyOnWriteArrayList<ReferenceProvider>();
}
protected void bindReferenceProviders(ReferenceProvider referenceProvider) {
this.referenceProviders.add(referenceProvider);
}
protected void unbindReferenceProviders(ReferenceProvider referenceProvider) {
this.referenceProviders.remove((Object)referenceProvider);
}
private boolean canReplicate(String path, Session session) throws RepositoryException {
AccessControlManager acMgr = session.getAccessControlManager();
return session.getAccessControlManager().hasPrivileges(path, new Privilege[]{acMgr.privilegeFromName("{http://www.day.com/crx/1.0}replicate")});
}
private List<Reference> getReferences(Resource page) {
ArrayList<Reference> refs = new ArrayList<Reference>();
for (ReferenceProvider referenceProvider : this.referenceProviders) {
refs.addAll(referenceProvider.findReferences(page));
}
return refs;
}
private List<Reference> filterByReplicationState(List<Reference> refs, Session session) throws RepositoryException {
ArrayList<Reference> filteredRefs = new ArrayList<Reference>(refs.size());
for (Reference reference : refs) {
boolean published = false;
boolean outdated = false;
ReplicationStatus replStatus = null;
Resource resource = reference.getResource();
boolean canReplicate = resource != null && this.canReplicate(resource.getPath(), session);
long lastPublished = 0;
if (resource != null) {
replStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
if (replStatus != null) {
boolean bl = published = replStatus.isDelivered() || replStatus.isActivated();
if (published) {
lastPublished = replStatus.getLastPublished().getTimeInMillis();
outdated = lastPublished < reference.getLastModified();
}
}
this.log.debug("Considering reference at {} . Published: {}, outdated: {} ( lastPublished: {}, lastModified: {} )", new Object[]{reference.getResource().getPath(), published, outdated, new Date(lastPublished), new Date(reference.getLastModified())});
}
if (!canReplicate || published && !outdated) continue;
filteredRefs.add(reference);
}
return filteredRefs;
}
private void replicate(String path, Session session) throws RepositoryException, ReplicationException {
if (this.canReplicate(path, session)) {
ReplicationOptions options = new ReplicationOptions();
options.setSynchronous(true);
this.replicator.replicate(session, ReplicationActionType.ACTIVATE, path, options);
} else {
this.log.debug(session.getUserID() + " is not allowed to replicate this page/asset: " + path + ". Starting workflow to activate ");
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put("path", path);
properties.put("replicationType", (String)ReplicationActionType.ACTIVATE);
properties.put("userId", session.getUserID());
Event event = new Event("com/day/cq/wcm/workflow/req/for/activation", properties);
this.eventAdmin.sendEvent(event);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void execute(WorkItem item, WorkflowSession session, MetaDataMap data) throws WorkflowException {
String payloadPath = (String)item.getWorkflowData().getPayload();
Session jcrSession = session.getSession();
ResourceResolver resolver = this.getResourceResolver(this.resolverFactory, session);
if (resolver == null) {
throw new WorkflowException("Could not create ResourceResolver.");
}
try {
Resource page = resolver.getResource(payloadPath);
long startTc = System.currentTimeMillis();
this.publisher.publish(page);
long duration = System.currentTimeMillis() - startTc;
this.log.debug("Replicating '{}' took {}ms.", new Object[]{page.getPath(), duration});
}
catch (Exception e) {
NewsletterReplicationException nre;
this.log.error("Newsletter activation failed", (Throwable)e);
this.setStatus(resolver, item, "failed");
if (e instanceof NewsletterReplicationException && (nre = (NewsletterReplicationException)e).getType() == NewsletterReplicationException.Type.NO_ACCESS) {
this.log.error("Newsletter activation failed", (Throwable)e);
this.setStatus(resolver, item, "failed");
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put("path", nre.getPath());
properties.put("replicationType", (String)ReplicationActionType.ACTIVATE);
properties.put("userId", nre.getUserID());
Event event = new Event("com/day/cq/wcm/workflow/req/for/activation", properties);
this.eventAdmin.sendEvent(event);
}
}
finally {
resolver.close();
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
protected void bindEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
protected void unbindEventAdmin(EventAdmin eventAdmin) {
if (this.eventAdmin == eventAdmin) {
this.eventAdmin = null;
}
}
protected void bindPublisher(Publisher publisher) {
this.publisher = publisher;
}
protected void unbindPublisher(Publisher publisher) {
if (this.publisher == publisher) {
this.publisher = null;
}
}
}