CreateLiveCopyCommand.java
10.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.servlets.HtmlStatusResponseHelper
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.commands.WCMCommandContext
* com.day.cq.wcm.msm.api.LiveCopy
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.text.Text
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletRequest
* 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.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* 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.apache.sling.api.servlets.HtmlResponse
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.commands;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.cq.wcm.msm.impl.commands.LiveCommand;
import com.day.text.Text;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
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.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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.apache.sling.api.servlets.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
public class CreateLiveCopyCommand
extends LiveCommand {
static final String MISSING_PAGE_PARAM = "missingPage";
static final String EXCLUDE_SUB_PAGES_PARAM = "excludeSubPages";
private static final Logger log = LoggerFactory.getLogger(CreateLiveCopyCommand.class);
@Reference
private LiveCopyServiceImpl liveCopyService = null;
public String getCommandName() {
return "createLiveCopy";
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
HtmlResponse missingPages;
try {
HtmlResponse e;
Page master;
Page targetPage;
boolean isCreateLiveCopy;
String dstPath = request.getParameter("destPath");
if (StringUtils.isEmpty((String)dstPath)) {
HtmlResponse htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Either destination or parent needs to be specified."));
return htmlResponse;
}
missingPages = request.getParameterValues("missingPage");
boolean isMissingPages = !this.isEmpty((String[])missingPages);
String srcPath = request.getParameter("srcPath");
boolean bl = isCreateLiveCopy = !StringUtils.isEmpty((String)srcPath);
if (!isMissingPages && !isCreateLiveCopy) {
HtmlResponse htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Incomplete request: No valid source path or missing page provided."));
return htmlResponse;
}
ResourceResolver resolver = request.getResourceResolver();
if (isMissingPages) {
targetPage = pageManager.getPage(dstPath);
if (targetPage == null) {
HtmlResponse e2 = HtmlStatusResponseHelper.createStatusResponse((int)500, (String)I18n.get((HttpServletRequest)request, (String)"No Page at destination"), (String)dstPath);
return e2;
}
LiveRelationship lr = this.relationshipManager.getLiveRelationship(targetPage.getContentResource(), false);
if (lr == null) {
HtmlResponse session = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"No live relationship, can not un-skipp Page"), (String)targetPage.getPath());
return session;
}
Page source = pageManager.getPage(Text.getRelativeParent((String)lr.getSourcePath(), (int)1));
if (source == null) {
HtmlResponse session = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)(I18n.get((HttpServletRequest)request, (String)"Source Page for un-skipp does not exist at ") + lr.getSourcePath()), (String)targetPage.getPath());
return session;
}
boolean includeSubPages = !"true".equals(request.getParameter("excludeSubPages"));
LiveCopyManagerImpl lcm = this.liveCopyService.createLiveCopyManager(resolver);
LiveCopyManagerImpl.LiveCopyImpl copy = lcm.get(lr.getLiveCopy().getPath());
for (HtmlResponse missingPage : missingPages) {
String absPath = Utils.appendPath(targetPage.getPath(), (String)missingPage);
Page master2 = pageManager.getPage(Utils.appendPath(source.getPath(), (String)missingPage));
if (master2 == null) {
log.warn("Request to un-skipp Page at that does not exist in Blueprint: ignore", (Object)absPath);
continue;
}
copy.removeExclusion(absPath.substring(copy.getPath().length() + 1));
if (copy.contains(absPath)) {
lr = this.relationshipManager.getLiveRelationship((Resource)new NonExistingResource(resolver, Utils.appendPath(absPath, "jcr:content")), true);
this.rolloutManager.rollout(resolver, lr, true, false);
if (includeSubPages) {
Page lcPage = pageManager.getPage(absPath);
if (lcPage != null) {
this.rollout(master2, lcPage, lr, true, resolver);
} else {
log.warn("Target Page not created by roll-out to un-skip at {}", (Object)absPath);
}
}
resolver.commit();
continue;
}
this.createLiveCopy(master2, dstPath, resolver, includeSubPages, null, new String[0]);
}
}
if (!isCreateLiveCopy) {
targetPage = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Un-skipped pages for {0}", (String)null, (Object[])missingPages));
return targetPage;
}
Resource resource = resolver.getResource(srcPath);
Page page = master = resource == null ? null : (Page)resource.adaptTo(Page.class);
if (master == null) {
e = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Error during operation. Source path does not point to a Page: {0}", (String)null, (Object[])new Object[]{srcPath}));
return e;
}
if (Text.isDescendantOrEqual((String)srcPath, (String)dstPath)) {
e = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Destination path cannot contain source path."));
return e;
}
boolean copyShallow = "true".equals(request.getParameter("shallow"));
Page lcPage = this.createLiveCopy(master, dstPath, resolver, !copyShallow, request.getRequestParameterMap(), request.getParameterValues("cq:rolloutConfigs"));
HtmlResponse e3 = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Live Copy created"), (String)lcPage.getPath());
return e3;
}
catch (IllegalStateException is) {
log.debug("Error during Live Copy creation Relation would result in a Circle {}", (Object)is.getMessage());
missingPages = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)is.getMessage());
return missingPages;
}
catch (Exception e) {
log.error("Error during Live Copy creation.", (Throwable)e);
missingPages = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)e.getMessage());
return missingPages;
}
finally {
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
try {
if (session != null && session.hasPendingChanges()) {
session.refresh(false);
log.debug("Reverted uncommitted changes, due to Exception");
}
}
catch (RepositoryException e) {
log.error("Failed to revert changes {}", (Object)e.getMessage());
}
}
}
protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
this.liveCopyService = liveCopyServiceImpl;
}
protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
if (this.liveCopyService == liveCopyServiceImpl) {
this.liveCopyService = null;
}
}
}