DAMSyncServiceImpl.java
37.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.DamEvent
* com.day.cq.dam.api.DamEvent$Type
* com.day.cq.dam.commons.util.DamUtil
* com.day.cq.replication.Agent
* com.day.cq.replication.AgentFilter
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationListener
* com.day.cq.replication.ReplicationLog
* com.day.cq.replication.ReplicationLog$Level
* com.day.cq.replication.ReplicationOptions
* com.day.cq.replication.ReplicationResult
* com.day.cq.replication.Replicator
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.security.AccessControlManager
* javax.jcr.security.Privilege
* javax.jcr.version.VersionManager
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyOption
* org.apache.felix.scr.annotations.PropertyUnbounded
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* 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.commons.osgi.PropertiesUtil
* org.apache.sling.commons.threads.ThreadPool
* org.apache.sling.commons.threads.ThreadPoolManager
* org.apache.sling.resource.collection.ResourceCollection
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceRegistration
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.mac.sync.impl;
import com.adobe.cq.dam.mac.sync.api.DAMSyncService;
import com.adobe.cq.dam.mac.sync.api.SyncAgent;
import com.adobe.cq.dam.mac.sync.api.SyncAgentFactory;
import com.adobe.cq.dam.mac.sync.impl.Constants;
import com.adobe.cq.dam.mac.sync.impl.EventInfo;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamEvent;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentFilter;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationListener;
import com.day.cq.replication.ReplicationLog;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.ReplicationResult;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.Privilege;
import javax.jcr.version.VersionManager;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.PropertyOption;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
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.commons.osgi.PropertiesUtil;
import org.apache.sling.commons.threads.ThreadPool;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.apache.sling.resource.collection.ResourceCollection;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%com.adobe.cq.dam.mac.sync.damsyncservice.label", description="%com.adobe.cq.dam.mac.sync.damsyncservice.description", immediate=1)
@Service(value={DAMSyncService.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="com.adobe.cq.dam.mac.sync.damsyncservice.registered_paths", unbounded=PropertyUnbounded.ARRAY), @org.apache.felix.scr.annotations.Property(name="com.adobe.cq.dam.mac.sync.damsyncservice.sync.renditions", boolValue={0}), @org.apache.felix.scr.annotations.Property(name="com.adobe.cq.dam.mac.sync.damsyncservice.platform", options={@PropertyOption(name="aem", value="Adobe Experience Manager"), @PropertyOption(name="mac", value="Adobe Marketing Cloud")})})
public class DAMSyncServiceImpl
implements DAMSyncService {
private static final Logger LOG = LoggerFactory.getLogger(DAMSyncServiceImpl.class);
private static final Set<DamEvent.Type> ACCEPTED_DAM_EVENTS = new HashSet<DamEvent.Type>(){};
private static final Map<String, Object> REPLICATION_SERVICE_AUTH_INFO = Collections.singletonMap("sling.service.subservice", "dam-replication");
static final String SCR_PROP_NAME_REGISTERED_PATHS = "com.adobe.cq.dam.mac.sync.damsyncservice.registered_paths";
static final String SCR_PROP_NAME_PLATFORM = "com.adobe.cq.dam.mac.sync.damsyncservice.platform";
static final String SCR_PROP_NAME_SYNC_RENDITIONS = "com.adobe.cq.dam.mac.sync.damsyncservice.sync.renditions";
static final boolean SCR_PROP_DEFAULT_SYNC_RENDITIONS = false;
static final String PLATFORM_OPTION_VALUE_AEM = "Adobe Experience Manager";
static final String PLATFORM_OPTION_VALUE_MAC = "Adobe Marketing Cloud";
static final String PLATFORM_OPTION_NAME_AEM = "aem";
static final String PLATFORM_OPTION_NAME_MAC = "mac";
static final String JCR_PROP_MAC_CLOUDSERVICE_CONFIG = "macConfig";
static final String JCR_PROP_MP_CLOUDSERVICE_CONFIG = "mpConfig";
static final String JCR_PROP_TENANT = "tenant";
private static final String JCR_PROP_TENANT_URL = "tenantURL";
private static final String ORIGINAL_RENDITION_PATH = "jcr:content/renditions/original";
private ThreadPool threadPool;
private Set<String> listenPaths = Collections.newSetFromMap(new ConcurrentHashMap());
private ServiceRegistration damAssetEventHandlerSR;
private ServiceRegistration damEventHandlerSR;
private String platform;
private Boolean syncRenditions;
@Reference
private ResourceResolverFactory rrf = null;
@Reference
private Replicator replicator = null;
@Reference
private ThreadPoolManager threadPoolManager = null;
@Reference
private ConfigurationAdmin configurationAdmin = null;
@Reference
private ConfigurationManagerFactory configurationManagerFactory = null;
@Reference
private SyncAgentFactory agentFactory = null;
@Override
public void registerDAMFolder(String folderPath, ResourceResolver resolver) {
if (this.permissionCheck(resolver, folderPath)) {
if (!this.listenPaths.contains(folderPath)) {
LOG.debug("Registering " + folderPath);
this.listenPaths.add(folderPath);
this.updateConfiguration();
this.syncFolder(resolver, folderPath, false, ReplicationActionType.ACTIVATE);
}
} else {
LOG.warn("Not authorized to perform the operation");
}
}
@Override
public boolean isMACSharedResource(String resourcePath) {
for (String sharedPath : this.listenPaths) {
if (!resourcePath.startsWith(sharedPath)) continue;
return true;
}
return false;
}
@Override
public void publishResourcesToMP(List<String> resourcePaths, ResourceResolver resolver) {
for (String resourcePath : resourcePaths) {
if (this.permissionCheck(resolver, resourcePath)) {
this.syncFolder(resolver, resourcePath, true, ReplicationActionType.ACTIVATE);
continue;
}
LOG.warn("Not authorized to perform the operation");
}
}
@Override
public void unpublishResourcesFromMP(List<String> resourcePaths, ResourceResolver resolver) {
for (String resourcePath : resourcePaths) {
if (this.permissionCheck(resolver, resourcePath)) {
this.syncFolder(resolver, resourcePath, true, ReplicationActionType.DEACTIVATE);
continue;
}
LOG.warn("Not authorized to perform the operation");
}
}
private void syncFolder(ResourceResolver resolver, String folderPath, boolean isMPPublish, ReplicationActionType replicationType) {
Resource res = resolver.getResource(folderPath);
if (res == null) {
return;
}
Node n = (Node)res.adaptTo(Node.class);
try {
if (ReplicationActionType.ACTIVATE == replicationType) {
if (n.isNodeType("nt:folder")) {
FolderAssetIterator iterator = new FolderAssetIterator(res);
while (iterator.hasNext()) {
Asset asset = iterator.next();
if (asset == null) continue;
AgentFilter filter = this.getAgentFilter(asset.getPath(), isMPPublish);
this.replicateAsset(asset.getPath(), filter, isMPPublish, replicationType);
}
} else if (null != res.adaptTo(ResourceCollection.class)) {
Iterator assets = DamUtil.getAssets((Resource)res);
AgentFilter filter = this.getAgentFilter(res.getPath(), isMPPublish);
while (assets.hasNext()) {
String assetPath = ((Asset)assets.next()).getPath();
this.replicateAsset(assetPath, filter, isMPPublish, replicationType);
}
this.replicateAsset(res.getPath(), filter, isMPPublish, replicationType);
} else {
AgentFilter filter = this.getAgentFilter(res.getPath(), isMPPublish);
this.replicateAsset(res.getPath(), filter, isMPPublish, replicationType);
}
} else {
AgentFilter filter = this.getAgentFilter(res.getPath(), isMPPublish);
this.replicateAsset(res.getPath(), filter, isMPPublish, replicationType);
}
}
catch (RepositoryException e) {
LOG.error("error while syncing a the folder {}", (Object)folderPath, (Object)e);
}
}
@Override
public void unregisterDAMFolder(String folderPath, ResourceResolver resolver) {
if (this.permissionCheck(resolver, folderPath)) {
if (this.listenPaths.remove(folderPath)) {
LOG.debug("Un-registering " + folderPath);
this.updateConfiguration();
}
} else {
LOG.warn("Not authorized to perform the operation");
}
}
private boolean permissionCheck(ResourceResolver rr, String path) {
Session session = (Session)rr.adaptTo(Session.class);
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;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean setSender(String pathToReplicate) {
boolean result = true;
ResourceResolver rr = null;
try {
rr = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
Resource resource = rr.getResource(pathToReplicate);
if (resource == null) {
boolean bl = false;
return bl;
}
Node node = (Node)resource.adaptTo(Node.class);
Session session = node.getSession();
if (!node.isCheckedOut()) {
session.getWorkspace().getVersionManager().checkout(pathToReplicate);
}
node.addMixin("dam:SyncToMAC");
String sender = null;
if ("aem".equals(this.platform)) {
sender = "aem";
} else if ("mac".equals(this.platform)) {
sender = "mac";
}
if (!node.hasProperty("dam:replicationSender")) {
node.setProperty("dam:replicationSender", sender);
}
session.save();
}
catch (Exception e) {
LOG.error("Unable to mark sender for " + pathToReplicate, (Throwable)e);
result = false;
}
finally {
if (rr != null) {
rr.close();
}
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean replicate(AgentFilter agentFilter, String path, ReplicationActionType replicationType) {
boolean result = true;
ReplicationOptions options = new ReplicationOptions();
options.setSynchronous(true);
options.setFilter(agentFilter);
options.setListener(new ReplicationListener(){
public void onStart(Agent agent, ReplicationAction action) {
}
public void onMessage(ReplicationLog.Level level, String message) {
}
public void onEnd(Agent agent, ReplicationAction action, ReplicationResult result) {
}
public void onError(Agent agent, ReplicationAction action, Exception error) {
LOG.error("Unable to perform replication action " + action.toString() + " with agent " + agent.getId(), (Throwable)error);
}
});
ResourceResolver rr = null;
try {
rr = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
Session session = (Session)rr.adaptTo(Session.class);
this.replicator.replicate(session, replicationType, path, options);
}
catch (Exception e) {
LOG.error("Unable to replicate changed resource at " + path, (Throwable)e);
result = false;
}
finally {
if (rr != null) {
rr.close();
}
}
return result;
}
private AgentFilter getAgentFilter(String tenant, SyncAgent agent) {
final String expectedAgentName = agent.getAgentNameForTenant(tenant);
return new AgentFilter(){
public boolean isIncluded(Agent agent) {
return expectedAgentName.equals(agent.getId());
}
};
}
private String getTenantIdForPath(String path, boolean isMPPublish) {
String tenant = null;
if (path != null) {
String root = "/content/dam/mac/";
if (path.startsWith(root)) {
tenant = path.substring(root.length(), path.indexOf("/", root.length()));
} else if ("aem".equals(this.platform)) {
tenant = this.getTenantFromConfiguration(path, isMPPublish);
}
}
return tenant;
}
private void updateConfiguration() {
try {
org.osgi.service.cm.Configuration configuration = this.configurationAdmin.getConfiguration(this.getClass().getName());
Hashtable<String, String[]> properties = configuration.getProperties();
if (properties == null) {
properties = new Hashtable<String, String[]>();
}
String[] paths = this.listenPaths.toArray(new String[this.listenPaths.size()]);
properties.put("com.adobe.cq.dam.mac.sync.damsyncservice.registered_paths", paths);
configuration.update(properties);
}
catch (IOException e) {
LOG.error("Unable to persist configuration for " + this.getClass().getName(), (Throwable)e);
}
}
private void processDAMEvent(Event event) {
String pathToReplicate;
DamEvent damEvent = DamEvent.fromEvent((Event)event);
String assetPath = null;
if (damEvent != null && ACCEPTED_DAM_EVENTS.contains((Object)damEvent.getType())) {
pathToReplicate = damEvent.getAssetPath();
LOG.debug("Processing dam event for path {}", (Object)pathToReplicate);
assetPath = pathToReplicate;
} else {
pathToReplicate = (String)event.getProperty("path");
LOG.debug("Processing sling event for path {}", (Object)pathToReplicate);
if (pathToReplicate != null) {
assetPath = this.getAssetPath(pathToReplicate);
}
}
if (assetPath == null) {
LOG.debug("Can not process dam event for {} no asset path" + pathToReplicate);
return;
}
AgentFilter filter = this.getAgentFilter(assetPath, false);
if (damEvent != null) {
DamEvent.Type type = damEvent.getType();
if (type == DamEvent.Type.ASSET_CREATED) {
LOG.debug("Detected DAM event {} for path {}", (Object)type.toString(), (Object)pathToReplicate);
this.replicateAsset(pathToReplicate, filter, false, ReplicationActionType.ACTIVATE);
} else if (type == DamEvent.Type.ASSET_REMOVED) {
LOG.debug("Detected DAM event {} for path {}", (Object)type.toString(), (Object)pathToReplicate);
this.replicate(filter, pathToReplicate, ReplicationActionType.DELETE);
}
} else {
EventInfo info = new EventInfo(event);
EventInfo.DAMEventType damEventType = info.getDamEventType();
EventInfo.ResourceEventType resourceEventType = info.getResourceEventType();
LOG.debug("Detected {} DAM event for a resource {} event for path {}", (Object[])new String[]{damEventType.toString(), resourceEventType.toString(), pathToReplicate});
if (resourceEventType == EventInfo.ResourceEventType.CHANGED) {
String[] resourceChangedAttributes = (String[])event.getProperty("resourceChangedAttributes");
String[] resourceAddedAttributes = (String[])event.getProperty("resourceAddedAttributes");
HashSet resourceAttributes = new HashSet();
if (resourceChangedAttributes != null) {
Collections.addAll(resourceAttributes, resourceChangedAttributes);
}
if (resourceAddedAttributes != null) {
Collections.addAll(resourceAttributes, resourceAddedAttributes);
}
if (Collections.disjoint(resourceAttributes, Constants.EXCLUDED_RESOURCE_ATTRIBUTES) && damEventType == EventInfo.DAMEventType.RENDITION) {
if (!this.syncRenditions.booleanValue() && !pathToReplicate.contains("jcr:content/renditions/original")) {
return;
}
if (this.shouldReplicateAsset(assetPath)) {
LOG.debug("Replication DAM event {} for path {}", (Object)resourceEventType.toString(), (Object)pathToReplicate);
if (this.setSender(assetPath)) {
this.replicate(filter, pathToReplicate, ReplicationActionType.ACTIVATE);
}
}
}
} else if (resourceEventType == EventInfo.ResourceEventType.ADDED && damEventType == EventInfo.DAMEventType.ACTIVITY_STREAM) {
LOG.debug("Replication DAM event {} for path {}", (Object)resourceEventType.toString(), (Object)pathToReplicate);
this.replicate(filter, pathToReplicate, ReplicationActionType.ACTIVATE);
}
}
}
private void replicateAsset(String pathToReplicate, AgentFilter filter, boolean isMPPublish, ReplicationActionType replicationType) {
if (!isMPPublish && this.shouldReplicateAsset(pathToReplicate)) {
LOG.debug("Replicating path {}", (Object)pathToReplicate);
if (this.setSender(pathToReplicate)) {
this.replicate(filter, pathToReplicate, replicationType);
}
} else if (isMPPublish) {
this.replicate(filter, pathToReplicate, replicationType);
}
}
private AgentFilter getAgentFilter(String assetPath, boolean isMPPublish) {
String tenant = this.getTenantIdForPath(assetPath, isMPPublish);
AgentFilter filter = null;
if ("aem".equals(this.platform)) {
filter = !isMPPublish ? this.getAgentFilter(tenant, this.agentFactory.getSyncAgent("Default")) : this.getAgentFilter(tenant, this.agentFactory.getSyncAgent("Default", "MP Replication Agent"));
} else if ("mac".equals(this.platform)) {
filter = this.getAgentFilter(tenant, this.agentFactory.getSyncAgent("Outbox"));
}
return filter;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean shouldReplicateAsset(String assetPath) {
ResourceResolver rr = null;
try {
rr = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
Map assetProperties = (Map)rr.getResource(assetPath).adaptTo(ValueMap.class);
String sender = null;
if ("aem".equals(this.platform)) {
sender = "aem";
} else if ("mac".equals(this.platform)) {
sender = "mac";
}
String currentSender = (String)assetProperties.get("dam:replicationSender");
if (currentSender == null || sender.equals(currentSender)) {
boolean bl = true;
return bl;
}
}
catch (LoginException e) {
LOG.error("Unable to get a resource resolver (with appropriate privileges) to determine if asset needs to be replicated.", (Throwable)e);
}
finally {
if (rr != null) {
rr.close();
}
}
return false;
}
@Activate
protected void activate(ComponentContext componentContext) {
this.threadPool = this.threadPoolManager.get("DAM Transporter");
this.modified(componentContext);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Modified
protected void modified(ComponentContext componentContext) {
Dictionary properties = componentContext.getProperties();
this.platform = PropertiesUtil.toString(properties.get("com.adobe.cq.dam.mac.sync.damsyncservice.platform"), (String)"aem");
this.syncRenditions = PropertiesUtil.toBoolean(properties.get("com.adobe.cq.dam.mac.sync.damsyncservice.sync.renditions"), (boolean)false);
String[] registeredPaths = PropertiesUtil.toStringArray(properties.get("com.adobe.cq.dam.mac.sync.damsyncservice.registered_paths"), (String[])new String[0]);
LOG.debug("Processing mac dam sync cloud config modification ");
if (registeredPaths != null && registeredPaths.length > 0) {
StringBuilder damEventFilterSB;
StringBuilder damAssetEventFilterSB;
this.listenPaths.clear();
damAssetEventFilterSB = new StringBuilder("(|");
damEventFilterSB = new StringBuilder("(|");
ResourceResolver rr = null;
try {
rr = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
int batch = 0;
int BATCH_SIZE = 500;
for (String path : registeredPaths) {
LOG.debug("Processing mac dam sync cloud config modification, Adding path " + path);
this.listenPaths.add(path);
damAssetEventFilterSB.append("(").append("assetPath").append("=").append(path).append("/*)");
damEventFilterSB.append("(").append("path").append("=").append(path).append("/*/").append("jcr:content").append("/").append("metadata").append(")");
damEventFilterSB.append("(").append("path").append("=").append(path).append("/*/").append("jcr:content").append("/").append("renditions").append("/*)");
damEventFilterSB.append("(").append("path").append("=").append(path).append("/*/").append("jcr:content").append("/").append("comments").append("/*)");
if (++batch <= BATCH_SIZE) continue;
batch = 0;
rr.commit();
}
if (rr.hasChanges()) {
rr.commit();
}
}
catch (LoginException e) {
new RuntimeException((Throwable)e);
}
catch (PersistenceException e) {
LOG.error("Error while syncing the folders", (Throwable)e);
new RuntimeException((Throwable)e);
}
finally {
if (rr != null) {
rr.close();
}
}
damAssetEventFilterSB.append(")");
damEventFilterSB.append(")");
Hashtable<String, String> damAssetEventHandlerProperties = new Hashtable<String, String>();
Hashtable<String, String> damEventHandlerProperties = new Hashtable<String, String>();
String damAssetEventFilter = damAssetEventFilterSB.toString();
String damMetadataEventFilter = damEventFilterSB.toString();
LOG.debug("damAssetEventFilter = " + damAssetEventFilter);
LOG.debug("damEventFilter = " + damMetadataEventFilter);
damAssetEventHandlerProperties.put("event.filter", damAssetEventFilter);
damAssetEventHandlerProperties.put("event.topics", "com/day/cq/dam");
damEventHandlerProperties.put("event.filter", damMetadataEventFilter);
damEventHandlerProperties.put("event.topics", (String)new String[]{"org/apache/sling/api/resource/Resource/CHANGED", "org/apache/sling/api/resource/Resource/ADDED"});
if (this.damAssetEventHandlerSR == null) {
this.damAssetEventHandlerSR = componentContext.getBundleContext().registerService(EventHandler.class.getName(), (Object)new EventHandler(){
public void handleEvent(final Event event) {
Runnable task = new Runnable(){
@Override
public void run() {
DAMSyncServiceImpl.this.processDAMEvent(event);
}
};
DAMSyncServiceImpl.this.threadPool.execute(task);
}
}, damAssetEventHandlerProperties);
} else {
this.damAssetEventHandlerSR.setProperties(damAssetEventHandlerProperties);
}
if (this.damEventHandlerSR == null) {
this.damEventHandlerSR = componentContext.getBundleContext().registerService(EventHandler.class.getName(), (Object)new EventHandler(){
public void handleEvent(final Event event) {
Runnable task = new Runnable(){
@Override
public void run() {
DAMSyncServiceImpl.this.processDAMEvent(event);
}
};
DAMSyncServiceImpl.this.threadPool.execute(task);
}
}, damEventHandlerProperties);
} else {
this.damEventHandlerSR.setProperties(damEventHandlerProperties);
}
} else {
this.listenPaths.clear();
if (this.damEventHandlerSR != null) {
this.damEventHandlerSR.unregister();
this.damEventHandlerSR = null;
}
if (this.damAssetEventHandlerSR != null) {
this.damAssetEventHandlerSR.unregister();
this.damAssetEventHandlerSR = null;
}
}
}
@Deactivate
protected void deactivate(ComponentContext componentContext) {
if (this.threadPool != null) {
this.threadPoolManager.release(this.threadPool);
this.threadPool = null;
}
if (this.damEventHandlerSR != null) {
this.damEventHandlerSR.unregister();
this.damEventHandlerSR = null;
}
if (this.damAssetEventHandlerSR != null) {
this.damAssetEventHandlerSR.unregister();
this.damAssetEventHandlerSR = null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private String getAssetPath(String replicatePath) {
ResourceResolver rr = null;
try {
rr = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
Resource replicateResource = rr.getResource(replicatePath);
Asset asset = DamUtil.resolveToAsset((Resource)replicateResource);
if (asset != null) {
String string = asset.getPath();
return string;
}
}
catch (LoginException e) {
LOG.error("Unable to get a resource resolver (with appropriate privileges) to resolve to asset.", (Throwable)e);
}
finally {
if (rr != null) {
rr.close();
}
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private String getTenantFromConfiguration(String path, boolean isMPPublish) {
String tenantId;
tenantId = null;
if (path != null) {
ResourceResolver serviceResolver = null;
try {
String properties;
String configPropertyName;
Configuration configuration = null;
serviceResolver = this.rrf.getServiceResourceResolver(REPLICATION_SERVICE_AUTH_INFO);
ConfigurationManager configurationManager = this.configurationManagerFactory.getConfigurationManager(serviceResolver);
while (!"/".equals(path) && path.length() > 0 && configuration == null) {
Resource resource = serviceResolver.getResource(path);
if (resource != null) {
String configPath;
if (null == resource.adaptTo(Asset.class)) {
properties = (ValueMap)resource.adaptTo(ValueMap.class);
configPropertyName = isMPPublish ? "mpConfig" : "macConfig";
configPath = (String)properties.get(configPropertyName, String.class);
configuration = configurationManager.getConfiguration(configPath);
} else {
properties = (ValueMap)resource.getChild("jcr:content").adaptTo(ValueMap.class);
configPropertyName = isMPPublish ? "mpConfig" : "macConfig";
configPath = (String)properties.get(configPropertyName, String.class);
configuration = configurationManager.getConfiguration(configPath);
}
}
path = path.substring(0, path.lastIndexOf("/"));
}
if (configuration != null) {
String[] split;
String tenantURL = (String)configuration.getProperties().get("tenant", String.class);
if (tenantURL != null) {
properties = tenantURL;
return properties;
}
tenantURL = (String)configuration.getProperties().get("tenantURL", String.class);
if (tenantURL != null) {
if (tenantURL.startsWith("https://")) {
tenantURL = tenantURL.replaceFirst("https://", "");
}
if ((split = tenantURL.split("\\.")).length >= 2) {
configPropertyName = split[0];
return configPropertyName;
}
}
split = null;
return split;
}
}
catch (LoginException e) {
LOG.error("Unable to obtain a resource resolver.", (Throwable)e);
}
finally {
if (serviceResolver != null) {
serviceResolver.close();
}
}
}
return tenantId;
}
protected void bindRrf(ResourceResolverFactory resourceResolverFactory) {
this.rrf = resourceResolverFactory;
}
protected void unbindRrf(ResourceResolverFactory resourceResolverFactory) {
if (this.rrf == resourceResolverFactory) {
this.rrf = null;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
protected void bindThreadPoolManager(ThreadPoolManager threadPoolManager) {
this.threadPoolManager = threadPoolManager;
}
protected void unbindThreadPoolManager(ThreadPoolManager threadPoolManager) {
if (this.threadPoolManager == threadPoolManager) {
this.threadPoolManager = null;
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
protected void bindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configurationManagerFactory = configurationManagerFactory;
}
protected void unbindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configurationManagerFactory == configurationManagerFactory) {
this.configurationManagerFactory = null;
}
}
protected void bindAgentFactory(SyncAgentFactory syncAgentFactory) {
this.agentFactory = syncAgentFactory;
}
protected void unbindAgentFactory(SyncAgentFactory syncAgentFactory) {
if (this.agentFactory == syncAgentFactory) {
this.agentFactory = null;
}
}
private static class FolderAssetIterator
implements Iterator<Asset> {
private Resource parentResource;
private Iterator<Resource> it = null;
private List<Resource> parentNodes = new ArrayList<Resource>();
private Asset next = null;
private Iterator<Resource> getChildren(Resource res) {
return res.listChildren();
}
public FolderAssetIterator(Resource resource) {
this.parentResource = resource;
this.it = this.getChildren(this.parentResource);
this.next = this.getNext();
}
private Asset getNext() {
while (!this.it.hasNext() && !this.parentNodes.isEmpty()) {
this.it = this.getChildren(this.parentNodes.remove(0));
}
while (this.it.hasNext()) {
Resource member = this.it.next();
Node n = (Node)member.adaptTo(Node.class);
try {
if (n.isNodeType("dam:Asset")) {
return (Asset)member.adaptTo(Asset.class);
}
if (!n.isNodeType("nt:folder")) continue;
this.parentNodes.add(member);
}
catch (RepositoryException e) {
LOG.error("unexpected exception ", (Throwable)e);
}
}
if (!this.parentNodes.isEmpty()) {
return this.getNext();
}
return null;
}
@Override
public boolean hasNext() {
return this.next != null;
}
@Override
public Asset next() {
Asset toReturn = this.next;
this.next = this.getNext();
return toReturn;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove not allowed for asset iterator on folder listing");
}
}
}