MoveLiveCopyResourceConflictHandler.java
9.94 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageEvent
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentManager
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.LiveStatus
* com.day.cq.wcm.msm.api.ResourceNameRolloutConflictHandler
* com.day.cq.wcm.msm.api.RolloutManager
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.nodetype.NodeType
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentManager;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.ResourceNameRolloutConflictHandler;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.RolloutManagerImpl;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MoveLiveCopyResourceConflictHandler
implements ResourceNameRolloutConflictHandler {
static final String MSM_MOVED_POSTFIX = "_msm_moved";
static final String NT_VANITY_PATH = "sling:VanityPath";
static final String PN_VANITY_PATH = "sling:vanityPath";
private final EventAdmin eventAdmin;
private final LiveRelationshipManager relationshipManager;
private final RolloutManager rolloutManager;
private static final Logger log = LoggerFactory.getLogger(MoveLiveCopyResourceConflictHandler.class);
MoveLiveCopyResourceConflictHandler(RolloutManagerImpl rolloutManager, LiveRelationshipManager relationshipManager, EventAdmin eventAdmin) {
this.rolloutManager = rolloutManager;
this.relationshipManager = relationshipManager;
this.eventAdmin = eventAdmin;
}
public boolean handleNameConflict(LiveRelationship relation, ResourceResolver resourceResolver, boolean isReset) throws WCMException {
Resource target = resourceResolver.getResource(relation.getTargetPath());
if (!Utils.resourceHasNode(target)) {
return false;
}
try {
boolean result;
if (relation.getStatus().isPage()) {
PageManager pm = (PageManager)resourceResolver.adaptTo(PageManager.class);
if (pm == null) {
throw new IllegalStateException("PageManager not available");
}
result = this.handleConflict(pm.getContainingPage(relation.getTargetPath()), relation, isReset);
} else {
result = this.handleConflict(target, relation, isReset);
}
return result;
}
catch (RepositoryException e) {
throw new WCMException((Throwable)e);
}
catch (PersistenceException e) {
throw new WCMException((Throwable)e);
}
}
private boolean handleConflict(Page target, LiveRelationship relation, boolean isReset) throws PersistenceException, RepositoryException, WCMException {
boolean result;
if (isReset) {
PageManager pm = target.getPageManager();
pm.delete(target, false, false);
log.debug("Removed manually created Page {} on reset", (Object)relation.getTargetPath());
result = true;
} else {
log.debug("Move a manually created Resource that blocks a roll-out at {}", (Object)relation.getTargetPath());
ResourceResolver resourceResolver = target.getContentResource().getResourceResolver();
Session session = (Session)resourceResolver.adaptTo(Session.class);
if (session == null) {
throw new IllegalStateException("Can't move, need a JCR Repository Resource");
}
String oldPagePath = target.getPath();
String targetPath = MoveLiveCopyResourceConflictHandler.getAvailablePath(session, target.getParent().getPath() + "/" + target.getName());
session.move(oldPagePath, targetPath);
log.debug("Moved manually created Page to {}", (Object)targetPath);
LiveRelationship newRelation = this.relationshipManager.getLiveRelationship((Resource)new NonExistingResource(resourceResolver, Utils.appendPath(oldPagePath, "jcr:content")), true);
this.rolloutManager.rollout(resourceResolver, newRelation, false, false);
Resource targetAfter = resourceResolver.getResource(oldPagePath);
if (targetAfter == null) {
session.refresh(false);
result = false;
} else {
Page rolledOut = (Page)targetAfter.adaptTo(Page.class);
this.cleanup(session, rolledOut, rolledOut.getPageManager().getPage(targetPath));
resourceResolver.commit();
PageModification mod = PageModification.moved((String)oldPagePath, (String)targetPath, (String)null, (String)resourceResolver.getUserID());
PageEvent pe = new PageEvent(mod);
this.eventAdmin.postEvent(pe.toEvent());
result = true;
}
}
return result;
}
private boolean handleConflict(Resource target, LiveRelationship relation, boolean isReset) throws PersistenceException, RepositoryException {
ResourceResolver resourceResolver = target.getResourceResolver();
if (isReset) {
resourceResolver.delete(target);
log.debug("Removed manually created Resource of Component at {}", (Object)relation.getTargetPath());
} else {
ComponentManager cmpManager = (ComponentManager)resourceResolver.adaptTo(ComponentManager.class);
if (cmpManager != null && cmpManager.getComponentOfResource(target) == null) {
resourceResolver.delete(target);
log.debug("Removed manually created Resource at {}", (Object)relation.getTargetPath());
} else {
Session session = (Session)resourceResolver.adaptTo(Session.class);
String destPath = MoveLiveCopyResourceConflictHandler.getAvailablePath(session, target.getPath());
session.move(target.getPath(), destPath);
log.debug("Moved manually created Component to {}", (Object)destPath);
}
}
return true;
}
private static String getAvailablePath(Session session, String pathHint) throws RepositoryException {
int i = 0;
String destPath = pathHint + "_msm_moved";
while (session.nodeExists(destPath)) {
Object[] arrobject = new Object[]{pathHint, "_msm_moved", i++};
destPath = String.format("%s%s_%s", arrobject);
}
return destPath;
}
private void cleanup(Session session, Page oldTarget, Page copy) throws RepositoryException {
String parent = oldTarget.getPath();
Iterator children = copy.listChildren();
while (children.hasNext()) {
Page child = (Page)children.next();
session.move(child.getPath(), parent + "/" + child.getName());
}
if (oldTarget.hasContent() && copy.hasContent()) {
Node targetProp = (Node)oldTarget.getContentResource().adaptTo(Node.class);
Node copyProp = (Node)copy.getContentResource().adaptTo(Node.class);
for (String propName : new String[]{"cq:lastReplicated", "cq:lastReplicatedBy", "cq:lastReplicationAction"}) {
if (!copyProp.hasProperty(propName)) continue;
Property prop = copyProp.getProperty(propName);
targetProp.setProperty(propName, prop.getValue());
prop.remove();
}
if (copyProp.isNodeType("sling:VanityPath")) {
boolean copyVanityPath = false;
if (targetProp.isNodeType("sling:VanityPath") || targetProp.canAddMixin("sling:VanityPath")) {
if (!targetProp.isNodeType("sling:VanityPath")) {
targetProp.addMixin("sling:VanityPath");
}
copyVanityPath = true;
}
for (String propName2 : new String[]{"sling:vanityPath", "sling:vanityOrder"}) {
if (!copyProp.hasProperty(propName2)) continue;
Property prop = copyProp.getProperty(propName2);
if (copyVanityPath) {
targetProp.setProperty(propName2, prop.getValue());
}
prop.remove();
}
for (String type : copyProp.getMixinNodeTypes()) {
if (!type.getName().equals("sling:VanityPath")) continue;
copyProp.removeMixin("sling:VanityPath");
break;
}
}
}
}
}