FolderSyncHandler.java
20.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.mac.sync.api.DAMSyncService
* com.day.cq.dam.api.Asset
* com.day.cq.dam.commons.util.DamUtil
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.security.AccessControlManager
* javax.jcr.security.Privilege
* org.apache.commons.collections.CollectionUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* 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.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.servlets.post.PostOperation
* org.apache.sling.servlets.post.PostResponse
* org.apache.sling.servlets.post.SlingPostProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.mac.sync.helper.impl.servlets;
import com.adobe.cq.dam.mac.sync.api.DAMSyncService;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClient;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import org.apache.commons.collections.CollectionUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
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.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
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.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.servlets.post.PostOperation;
import org.apache.sling.servlets.post.PostResponse;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="DAM Folder Sync Handler")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dam.mac.sync"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class FolderSyncHandler
implements PostOperation {
private static final Logger log = LoggerFactory.getLogger(FolderSyncHandler.class);
private static final Map<String, Object> REPLICATION_SERVICE_AUTH_INFO = Collections.singletonMap("sling.service.subservice", "dam-replication-helper");
@Reference
private DAMSyncService damSyncService = null;
@Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
private volatile MACSyncClient syncClient = null;
@Reference
private ResourceResolverFactory rrf = null;
@Reference
private MACTenantConfigurationResolver configResolver = null;
private static final String REQUEST_PARAM_DISABLE_SYNC = "disable";
private static final String REQUEST_PARAM_SYNC_TYPE = "type";
private static final String PARAM_OPERATION = ":operation";
private static final String MAC_SYNC_OPERATION = "dam.mac.sync";
private static final String PARAM_PATH = "path";
private static final String REQUEST_PARAM_TEAM_MEMBER = "teamMemberPrincipalName";
private static final String REQUEST_PARAM_TEAM_MEMBER_ROLE = "teamMemberRole";
private static final String CC_SYNC = "cc";
private static final String BRANDPORTAL_SYNC = "mediaportal";
private static final String TEAM_MEMBER_ATTR = "dam:ccTeamMembers";
public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] slingPostProcessors) {
String[] arrstring;
Resource res = request.getResource();
String disableSync = request.getParameter("disable");
String type = request.getParameter("type");
String operation = request.getRequestParameter(":operation").getString();
String[] paths = request.getParameterValues("path");
if (paths == null) {
String[] arrstring2 = new String[1];
arrstring = arrstring2;
arrstring2[0] = res.getPath();
} else {
arrstring = paths;
}
paths = arrstring;
ResourceResolver userResolver = request.getResourceResolver();
Session userSession = (Session)userResolver.adaptTo(Session.class);
String parentLocation = request.getContextPath() + res.getPath();
String redirectUrl = request.getContextPath() + res.getPath();
ResourceResolver serviceResolver = null;
try {
serviceResolver = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
}
catch (LoginException loginException) {
String status = "Not authorized to perform the operation";
log.error(status, (Throwable)loginException);
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
return;
}
for (String path : paths) {
String status;
String status2;
MACTenantConfiguration mtc;
res = userResolver.getResource(path);
if (disableSync == null || disableSync.trim().length() <= 0) {
if ("mediaportal".equalsIgnoreCase(type)) {
log.debug("Replicating {} to brand portal", (Object)res.getPath());
} else {
log.debug("Enabling mac sync for {}", (Object)res.getPath());
}
try {
if (!userSession.itemExists(res.getPath())) {
status2 = "Path does not exists";
log.error(status2);
this.setResponseObject(response, 500, status2, status2, parentLocation, res.getPath(), redirectUrl);
return;
}
parentLocation = request.getContextPath() + res.getParent().getPath();
if (this.canSync((Session)userResolver.adaptTo(Session.class), res.getPath())) {
mtc = null;
mtc = "mediaportal".equalsIgnoreCase(type) ? this.configResolver.getDefaultMPConfig(serviceResolver) : this.configResolver.getDefaultConfig(serviceResolver);
if (mtc != null) {
if (type != null && type.equals("cc")) {
RequestParameter[] memberNamesRP = request.getRequestParameters("teamMemberPrincipalName");
RequestParameter[] rolesRP = request.getRequestParameters("teamMemberRole");
ValueMap vm = (ValueMap)res.adaptTo(ValueMap.class);
String[] existingMembers = (String[])vm.get("dam:ccTeamMembers", (Object)new String[0]);
List<String> currentList = Arrays.asList(existingMembers);
ArrayList<String> members = new ArrayList<String>();
if (null != memberNamesRP) {
if (memberNamesRP.length != rolesRP.length) {
String status3 = "Invalid Request Parameters, member names and roles should match";
log.warn(status3);
this.setResponseObject(response, 500, status3, status3, parentLocation, res.getPath(), redirectUrl);
return;
}
for (int i = 0; i < memberNamesRP.length; ++i) {
String id = memberNamesRP[i].getString();
String role = rolesRP[i].getString();
members.add(new TeamMember(id, role).toString());
}
}
Collection removedMembers = CollectionUtils.subtract(currentList, members);
Collection addedMembers = CollectionUtils.subtract(members, currentList);
if (!this.syncClient.setupMACSync(res.getPath(), false, mtc, addedMembers, removedMembers)) {
String status4 = "not able to setup mac sync";
this.setResponseObject(response, 500, status4, status4, parentLocation, res.getPath(), redirectUrl);
return;
}
this.setCCAttributes(userResolver, members, res);
}
if ("dam.mac.sync".equalsIgnoreCase(operation) && this.findShared(res) == null && !"mediaportal".equals(type)) {
if (!(type != null && type.equals("cc") || this.syncClient.setupMACSync(res.getPath(), false, mtc, null, null))) {
status = "not able to setup mac sync";
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
return;
}
this.setMacConfig(userResolver, mtc.getPath(), res);
this.damSyncService.registerDAMFolder(res.getPath(), serviceResolver);
continue;
}
if (!"mediaportal".equalsIgnoreCase(type)) continue;
this.setMPConfig(userResolver, mtc.getPath(), res);
Iterator assets = DamUtil.getAssets((Resource)res);
while (assets.hasNext()) {
String assetPath = ((Asset)assets.next()).getPath();
Resource childRes = userResolver.getResource(assetPath);
if (null == childRes.adaptTo(ResourceCollection.class)) continue;
this.setMPConfig(userResolver, mtc.getPath(), childRes);
}
this.damSyncService.publishResourcesToMP(Arrays.asList(res.getPath()), serviceResolver);
continue;
}
log.error("There is no applicable cloud configuration");
status = "There is no applicable cloud configuration";
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
continue;
}
status2 = "Not authorized to perform the operation";
log.error(status2);
this.setResponseObject(response, 500, status2, status2, parentLocation, res.getPath(), redirectUrl);
}
catch (RepositoryException e) {
status = "Error in enabling folder for mac sync";
log.error(status, (Throwable)e);
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
}
continue;
}
if ("mediaportal".equalsIgnoreCase(type)) {
log.debug("Deactivating {} from brand portal", (Object)res.getPath());
} else {
log.debug("disabling dam sync for {}", (Object)res.getPath());
}
try {
if (!userSession.itemExists(res.getPath())) {
return;
}
parentLocation = request.getContextPath() + res.getParent().getPath();
if (this.canSync((Session)userResolver.adaptTo(Session.class), res.getPath())) {
mtc = null;
if (!"mediaportal".equalsIgnoreCase(type)) {
mtc = this.configResolver.getConfigurationForPath(serviceResolver, res.getPath());
if (!this.syncClient.setupMACSync(res.getPath(), true, mtc, null, null)) {
log.error("not able to setup mac sync");
status = "not able to setup mac sync";
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
}
this.removeMacConfig(userResolver, res);
this.damSyncService.unregisterDAMFolder(res.getPath(), serviceResolver);
continue;
}
mtc = this.configResolver.getMPConfigurationForPath(serviceResolver, res.getPath());
this.damSyncService.unpublishResourcesFromMP(Arrays.asList(res.getPath()), serviceResolver);
this.removeMPConfig(userResolver, res);
continue;
}
status2 = "Not authorized to perform the operation";
log.warn(status2);
this.setResponseObject(response, 500, status2, status2, parentLocation, res.getPath(), redirectUrl);
continue;
}
catch (RepositoryException e) {
status = "Error in disabling folder for dam sync";
log.error(status, (Throwable)e);
this.setResponseObject(response, 500, status, status, parentLocation, res.getPath(), redirectUrl);
}
}
if (serviceResolver != null) {
serviceResolver.close();
}
}
private void removeMPConfig(ResourceResolver rr, Resource resource) {
ModifiableValueMap resourceProperties = null;
resourceProperties = null != resource.adaptTo(Asset.class) ? (ModifiableValueMap)resource.getChild("jcr:content").adaptTo(ModifiableValueMap.class) : (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
resourceProperties.remove((Object)"mpConfig");
try {
rr.commit();
}
catch (PersistenceException e) {
log.error("Unable to clean reference to cloud config for target folder " + resource.getPath(), (Throwable)e);
}
}
private void setResponseObject(PostResponse response, int statusCode, String status, String title, String parentLocation, String path, String location) {
response.setStatus(statusCode, status);
response.setTitle(title);
response.setParentLocation(parentLocation);
response.setLocation(location);
response.setPath(path);
}
private boolean canSync(Session session, String path) {
try {
AccessControlManager accessControlManager = session.getAccessControlManager();
Privilege[] privs = new Privilege[]{accessControlManager.privilegeFromName("crx:replicate"), accessControlManager.privilegeFromName("jcr:read"), accessControlManager.privilegeFromName("jcr:write")};
return accessControlManager.hasPrivileges(path, privs);
}
catch (RepositoryException e) {
log.error("unable to determine if user can apply team.", (Throwable)e);
new RuntimeException((Throwable)e);
return false;
}
}
private void setMacConfig(ResourceResolver rr, String macConfigPath, Resource tenantDAMFolder) {
ModifiableValueMap tenantDAMFolderProperties = (ModifiableValueMap)tenantDAMFolder.adaptTo(ModifiableValueMap.class);
tenantDAMFolderProperties.put((Object)"macConfig", (Object)macConfigPath);
try {
rr.commit();
}
catch (PersistenceException e) {
log.error("Unable to create tenant's DAM folder.", (Throwable)e);
}
}
private void setMPConfig(ResourceResolver rr, String mpConfig, Resource resource) {
ModifiableValueMap resourceProperties = null;
resourceProperties = null != resource.adaptTo(Asset.class) ? (ModifiableValueMap)resource.getChild("jcr:content").adaptTo(ModifiableValueMap.class) : (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
resourceProperties.put((Object)"mpConfig", (Object)mpConfig);
try {
rr.commit();
}
catch (PersistenceException e) {
log.error("Unable to set mp config on resource.", (Throwable)e);
}
}
private void setCCAttributes(ResourceResolver rr, List<String> members, Resource tenantDAMFolder) {
ModifiableValueMap tenantDAMFolderProperties = (ModifiableValueMap)tenantDAMFolder.adaptTo(ModifiableValueMap.class);
if (members.size() > 0) {
tenantDAMFolderProperties.put((Object)"dam:ccTeamMembers", (Object)members.toArray(new String[0]));
} else {
tenantDAMFolderProperties.remove((Object)"dam:ccTeamMembers");
}
try {
rr.commit();
}
catch (PersistenceException e) {
log.error("Unable to create tenant's DAM folder.", (Throwable)e);
}
}
private void removeMacConfig(ResourceResolver rr, Resource tenantDAMFolder) {
ModifiableValueMap targetFolderProps = (ModifiableValueMap)tenantDAMFolder.adaptTo(ModifiableValueMap.class);
targetFolderProps.remove((Object)"macConfig");
try {
rr.commit();
}
catch (PersistenceException e) {
log.error("Unable to clean reference to cloud config for target folder " + tenantDAMFolder.getPath(), (Throwable)e);
}
}
Resource findShared(Resource currentNode) throws RepositoryException {
if (currentNode == null || "/content/dam".equals(currentNode.getPath())) {
return null;
}
ValueMap vm = (ValueMap)currentNode.adaptTo(ValueMap.class);
if (vm.containsKey((Object)"macConfig")) {
return currentNode;
}
return this.findShared(currentNode.getParent());
}
protected void bindDamSyncService(DAMSyncService dAMSyncService) {
this.damSyncService = dAMSyncService;
}
protected void unbindDamSyncService(DAMSyncService dAMSyncService) {
if (this.damSyncService == dAMSyncService) {
this.damSyncService = null;
}
}
protected void bindSyncClient(MACSyncClient mACSyncClient) {
this.syncClient = mACSyncClient;
}
protected void unbindSyncClient(MACSyncClient mACSyncClient) {
if (this.syncClient == mACSyncClient) {
this.syncClient = null;
}
}
protected void bindRrf(ResourceResolverFactory resourceResolverFactory) {
this.rrf = resourceResolverFactory;
}
protected void unbindRrf(ResourceResolverFactory resourceResolverFactory) {
if (this.rrf == resourceResolverFactory) {
this.rrf = null;
}
}
protected void bindConfigResolver(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
this.configResolver = mACTenantConfigurationResolver;
}
protected void unbindConfigResolver(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
if (this.configResolver == mACTenantConfigurationResolver) {
this.configResolver = null;
}
}
private class TeamMember {
private String memberId;
private String memberRole;
TeamMember(String id, String role) {
this.memberId = id;
this.memberRole = role;
}
String getId() {
return this.memberId;
}
String getRole() {
return this.memberRole;
}
public String toString() {
return this.getId() + ":" + this.getRole();
}
}
}