FragmentPreprocessor.java
6.45 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.AgentConfig
* com.day.cq.replication.Preprocessor
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationOptions
* com.day.cq.replication.Replicator
* javax.jcr.Credentials
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.SimpleCredentials
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl.replication;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.Preprocessor;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.Replicator;
import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.felix.scr.annotations.Activate;
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.jcr.api.SlingRepository;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service(value={Preprocessor.class})
@Component
public class FragmentPreprocessor
implements Preprocessor {
public static final String REPLICATION_SERVICE_USER = "replicationService";
private final Logger log;
private static final String PROP_CF = "jcr:content/contentFragment";
private static final String NN_SUB_ASSETS = "subassets";
@Reference
private SlingRepository repository;
private BundleContext bundleContext;
public FragmentPreprocessor() {
this.log = LoggerFactory.getLogger(this.getClass());
}
private Node getFragment(Session session, String path) {
Node fragment = null;
try {
Property type;
Node replicationNode = session.getNode(path);
if (replicationNode.isNodeType("dam:Asset") && replicationNode.hasProperty("jcr:content/contentFragment") && (type = replicationNode.getProperty("jcr:content/contentFragment")).getBoolean()) {
fragment = replicationNode;
}
}
catch (RepositoryException re) {
// empty catch block
}
return fragment;
}
private Node getSubAssets(Node fragment) {
Node subAssets = null;
try {
if (fragment.hasNode("subassets")) {
subAssets = fragment.getNode("subassets");
}
}
catch (RepositoryException re) {
// empty catch block
}
return subAssets;
}
@Activate
protected void activate(ComponentContext componentContext) {
this.bundleContext = componentContext.getBundleContext();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void preprocess(ReplicationAction action, ReplicationOptions options) throws ReplicationException {
block15 : {
ReplicationActionType type = action.getType();
if (type != ReplicationActionType.ACTIVATE) {
return;
}
Session systemSession = null;
Session session = null;
try {
String agentUserId;
Node subAssets;
Node fragment;
session = systemSession = this.repository.loginService("replicationService", null);
AgentConfig config = action.getConfig();
if (config != null && (agentUserId = config.getAgentUserId()) != null && agentUserId.length() > 0) {
session = this.repository.impersonateFromService("replicationService", (Credentials)new SimpleCredentials(agentUserId, new char[0]), null);
}
Node node = subAssets = (fragment = this.getFragment(session, action.getPath())) != null ? this.getSubAssets(fragment) : null;
if (subAssets == null) break block15;
Replicator replicator = null;
ServiceReference serviceRef = this.bundleContext.getServiceReference(Replicator.class.getName());
if (serviceRef != null) {
replicator = (Replicator)this.bundleContext.getService(serviceRef);
}
if (replicator != null) {
try {
NodeIterator childIterator = subAssets.getNodes();
while (childIterator.hasNext()) {
Node child = childIterator.nextNode();
replicator.replicate(session, type, child.getPath(), options);
}
break block15;
}
finally {
this.bundleContext.ungetService(serviceRef);
}
}
this.log.warn("Replicator could not be acquired; sub assets are not replicated.");
}
catch (RepositoryException e) {
throw new ReplicationException((Exception)e);
}
finally {
if (session != null && session.isLive() && session != systemSession) {
session.logout();
}
if (systemSession != null && systemSession.isLive()) {
systemSession.logout();
}
}
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
}