PublisherImpl.java
7.54 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
/*
* 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
* javax.jcr.Node
* 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.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mcm.campaign.impl;
import com.adobe.cq.mcm.campaign.NewsletterException;
import com.adobe.cq.mcm.campaign.NewsletterReplicationException;
import com.adobe.cq.mcm.campaign.Publisher;
import com.adobe.cq.mcm.campaign.impl.ReferenceCollector;
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 java.util.Calendar;
import java.util.List;
import javax.jcr.Node;
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.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Component(metatype=0)
public class PublisherImpl
implements Publisher {
private final Logger log;
@org.apache.felix.scr.annotations.Reference
private ReferenceCollector referenceAggregator;
@org.apache.felix.scr.annotations.Reference
protected Replicator replicator;
public PublisherImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
}
private void replicateRecursively(Resource toPublish, Session session, ReplicationOptions options) throws NewsletterException {
this.log.debug("Replicating '{}'", (Object)toPublish.getPath());
if ("cq:Page".equals(toPublish.getResourceType())) {
this.replicatePage(toPublish, session, options, -1);
Iterable children = toPublish.getChildren();
for (Resource child : children) {
this.replicateRecursively(child, session, options);
}
}
}
private void replicatePage(Resource resource, Session session, ReplicationOptions options, long lastModified) throws NewsletterException {
try {
AccessControlManager acMgr = session.getAccessControlManager();
Privilege[] privileges = new Privilege[]{acMgr.privilegeFromName("{http://www.day.com/crx/1.0}replicate")};
if (!acMgr.hasPrivileges(resource.getPath(), privileges)) {
throw new NewsletterReplicationException("Current session is not authorized to replicate " + resource.getPath(), null, NewsletterReplicationException.Type.NO_ACCESS, session.getUserID(), resource.getPath());
}
boolean requiresReplication = true;
String reason = "unknown";
ReplicationStatus replStatus = (ReplicationStatus)resource.adaptTo(ReplicationStatus.class);
if (replStatus != null && (replStatus.isDelivered() || replStatus.isActivated())) {
Calendar lastModifiedCal;
if (lastModified < 0 && (lastModifiedCal = this.getLastModified(resource)) != null) {
lastModified = lastModifiedCal.getTimeInMillis();
}
if (lastModified >= 0) {
long lastPublished = replStatus.getLastPublished().getTimeInMillis();
boolean bl = requiresReplication = lastPublished < lastModified;
if (!requiresReplication) {
reason = "already replicated; timecodes: " + lastPublished + " (publish) " + lastModified + " (modified)";
}
}
}
if (requiresReplication) {
this.log.debug("Replicating '{}'", (Object)resource.getPath());
this.replicator.replicate(session, ReplicationActionType.ACTIVATE, resource.getPath(), options);
} else {
this.log.debug("Replication of '{}' skipped; reason: {}", (Object[])new String[]{resource.getPath(), reason});
}
}
catch (RepositoryException e) {
throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
}
catch (ReplicationException e) {
throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
}
}
private Calendar getLastModified(Resource page) {
Resource pageContent = page.getChild("jcr:content");
ValueMap contentProps = (ValueMap)pageContent.adaptTo(ValueMap.class);
Calendar lastModified = null;
if (contentProps != null && (lastModified = (Calendar)contentProps.get("cq:lastModified", Calendar.class)) == null) {
lastModified = (Calendar)contentProps.get("jcr:created", Calendar.class);
}
return lastModified;
}
@Override
public void publish(Resource resource) throws NewsletterException {
try {
Session session = ((Node)resource.adaptTo(Node.class)).getSession();
ReplicationOptions options = new ReplicationOptions();
options.setSynchronous(true);
List<Reference> references = this.referenceAggregator.getReferences(resource);
Calendar lastPageModifCal = this.getLastModified(resource);
long lastPageModif = lastPageModifCal != null ? lastPageModifCal.getTimeInMillis() : -1;
references.add(new Reference(null, null, resource, lastPageModif));
for (Reference reference : references) {
String type = reference.getType();
boolean isCampaignRef = "campaign".equals(type);
resource = reference.getResource();
if (resource == null) continue;
if (!isCampaignRef) {
long lastModified = reference.getLastModified();
this.replicatePage(resource, session, options, lastModified);
continue;
}
this.replicateRecursively(resource, session, options);
}
}
catch (RepositoryException e) {
throw new NewsletterReplicationException("Could not replicate newsletter", (Throwable)e, NewsletterReplicationException.Type.GENERIC);
}
}
protected void bindReferenceAggregator(ReferenceCollector referenceCollector) {
this.referenceAggregator = referenceCollector;
}
protected void unbindReferenceAggregator(ReferenceCollector referenceCollector) {
if (this.referenceAggregator == referenceCollector) {
this.referenceAggregator = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
}