LiveCommand.java
10.6 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.api.commands.WCMCommand
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutConfigManager
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.commands;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.commands.WCMCommand;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.RolloutConfigManagerFactory;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
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.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1)
@Service
public abstract class LiveCommand
implements WCMCommand {
private static final String[] EMPTY_ROLLOUT_CONFIG = new String[0];
@Reference
protected RolloutManager rolloutManager = null;
@Reference
protected LiveRelationshipManager relationshipManager = null;
@Reference
protected RolloutConfigManagerFactory rolloutConfigMgrFactory = null;
public static final String BP_PATH_PARAM = "bpPath";
private static final Logger log = LoggerFactory.getLogger(LiveCommand.class);
private static final String CHILD_PATTERN = "%s/%s";
protected Page createCopyTarget(Page master, String dstPath, RequestParameterMap map) throws WCMException, RepositoryException {
String pageLabel;
Page lcPage;
Node contentNode;
PageManager pageManager;
String pageName = pageLabel = LiveCommand.getParameter(map, "label");
if (StringUtils.isEmpty((String)pageLabel)) {
pageName = master.getName();
}
String template = null;
if (master.getTemplate() != null) {
template = master.getTemplate().getPath();
}
if (!(contentNode = (Node)(lcPage = (pageManager = master.getPageManager()).create(dstPath, pageName, template, null, false)).getContentResource().adaptTo(Node.class)).canAddMixin("cq:LiveRelationship")) {
contentNode.getSession().refresh(false);
throw new WCMException(String.format("Can't establish LiveRelationship from %s to %s: Node does not allow to set mixin of %s", master.getPath(), lcPage.getPath(), "cq:LiveRelationship"));
}
Resource pageResource = (Resource)lcPage.adaptTo(Resource.class);
ResourceResolver resolver = pageResource.getResourceResolver();
LiveRelationship ship = this.establishRelationship(master, lcPage, false, resolver, LiveCommand.getRolloutConfigParam(map));
contentNode.getParent().remove();
this.rollout(master, lcPage, ship, false, resolver);
String pageTitle = LiveCommand.getParameter(map, "title");
if (resolver.getResource(lcPage.getPath()) == null) {
if (!StringUtils.isEmpty((String)pageTitle)) {
pageTitle = master.getTitle();
}
lcPage = pageManager.create(dstPath, pageName, template, pageTitle, false);
} else if (!StringUtils.isEmpty((String)pageTitle)) {
((Node)lcPage.getContentResource().adaptTo(Node.class)).setProperty("jcr:title", pageTitle);
}
String before = LiveCommand.getParameter(map, "before");
if (!StringUtils.isEmpty((String)before)) {
pageManager.move(lcPage, lcPage.getPath(), Text.getName((String)before), false, false, null);
}
log.debug("Created Live Copy Resource at {}", (Object)lcPage.getPath());
return lcPage;
}
protected /* varargs */ LiveRelationship establishRelationship(Page master, Page liveCopy, boolean deep, ResourceResolver resolver, String ... rolloutConfigs) throws WCMException {
RolloutConfig[] configs;
if (this.isEmpty(rolloutConfigs)) {
rolloutConfigs = (String[])new HierarchyNodeInheritanceValueMap(liveCopy.getContentResource()).getInherited("cq:LiveSyncConfig/cq:rolloutConfigs", String[].class);
}
if (rolloutConfigs != null) {
RolloutConfigManager rolloutConfigManager = this.rolloutConfigMgrFactory.create(resolver);
ArrayList<RolloutConfig> tmp = new ArrayList<RolloutConfig>();
for (String path : rolloutConfigs) {
RolloutConfig rc = rolloutConfigManager.getRolloutConfig(path);
if (rc == null) continue;
tmp.add(rc);
}
configs = tmp.toArray((T[])new RolloutConfig[tmp.size()]);
} else {
configs = null;
}
return this.relationshipManager.establishRelationship(master, liveCopy, deep, false, configs);
}
protected void rollout(Page master, Page liveCopy, LiveRelationship ship, boolean deep, ResourceResolver resolver) throws WCMException {
this.rolloutManager.rollout(resolver, ship, true, false);
if (deep) {
this.rolloutChildren(master.listChildren(), resolver, liveCopy.getPath());
} else {
log.debug("Live Copy at {} set to be shallow", (Object)liveCopy.getPath());
}
}
protected /* varargs */ Page createLiveCopy(Page master, String dstPath, ResourceResolver resolver, boolean deep, RequestParameterMap parameterMap, String ... rolloutConfigs) throws RepositoryException, WCMException {
Session session = (Session)resolver.adaptTo(Session.class);
Page lcPage = this.createCopyTarget(master, dstPath, parameterMap);
this.establishRelationship(master, lcPage, deep, resolver, rolloutConfigs);
if (deep) {
this.rolloutChildren(master.listChildren(), resolver, lcPage.getPath());
}
session.save();
this.relationshipManager.getLiveRelationship(lcPage.getContentResource(), false);
return lcPage;
}
protected boolean isEmpty(String[] values) {
if (values != null) {
for (String value : values) {
if (StringUtils.isEmpty((String)value)) continue;
return false;
}
}
return true;
}
protected static String getParameter(RequestParameterMap map, String name) {
return map != null && map.containsKey((Object)name) ? map.getValue(name).getString() : null;
}
protected static String appendChild(String parentPath, String childName) {
return String.format("%s/%s", parentPath, childName);
}
private void rolloutChildren(Iterator<Page> children, ResourceResolver resolver, String target) throws WCMException {
while (children.hasNext()) {
Page child = children.next();
String targetChild = LiveCommand.appendChild(target, child.getName());
if (child.hasContent()) {
String contentPath = LiveCommand.appendChild(targetChild, child.getContentResource().getName());
LiveRelationship childShip = this.relationshipManager.getLiveRelationship((Resource)new NonExistingResource(resolver, contentPath), true);
if (childShip != null) {
this.rolloutManager.rollout(resolver, childShip, true, false);
} else {
throw new WCMException("Failed to access relation for child at " + child.getPath());
}
}
this.rolloutChildren(child.listChildren(), resolver, targetChild);
}
}
private static String[] getRolloutConfigParam(RequestParameterMap map) {
String[] configs;
if (map != null && map.containsKey((Object)"cq:rolloutConfigs")) {
RequestParameter[] paths = map.getValues("cq:rolloutConfigs");
configs = new String[paths.length];
for (int i = 0; i < paths.length; ++i) {
configs[i] = paths[i].getString();
}
} else {
configs = EMPTY_ROLLOUT_CONFIG;
}
return configs;
}
protected void bindRolloutManager(RolloutManager rolloutManager) {
this.rolloutManager = rolloutManager;
}
protected void unbindRolloutManager(RolloutManager rolloutManager) {
if (this.rolloutManager == rolloutManager) {
this.rolloutManager = null;
}
}
protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.relationshipManager = liveRelationshipManager;
}
protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.relationshipManager == liveRelationshipManager) {
this.relationshipManager = null;
}
}
protected void bindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
this.rolloutConfigMgrFactory = rolloutConfigManagerFactory;
}
protected void unbindRolloutConfigMgrFactory(RolloutConfigManagerFactory rolloutConfigManagerFactory) {
if (this.rolloutConfigMgrFactory == rolloutConfigManagerFactory) {
this.rolloutConfigMgrFactory = null;
}
}
}