RolloutCommand.java
9.07 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
/*
* 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.WCMCommand
* com.day.cq.wcm.api.commands.WCMCommandContext
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$RolloutParams
* com.day.cq.wcm.msm.api.RolloutManager$RolloutProgress
* javax.servlet.ServletOutputStream
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang.ArrayUtils
* 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.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.bgservlets.RuntimeState
* 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.WCMCommand;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import com.day.cq.wcm.msm.api.RolloutManager;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.ArrayUtils;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.bgservlets.RuntimeState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class RolloutCommand
implements WCMCommand {
private final Logger log = LoggerFactory.getLogger(RolloutCommand.class);
public static final String TYPE_DEEP = "deep";
public static final String TYPE_PAGE = "page";
public static final String TYPE_DELETE = "delete";
public static final String TYPE_PARAM = "type";
public static final String PARAS_PARAM = "paras";
public static final String RESET_PARAM = "reset";
@Reference
private RolloutManager rolloutManager = null;
public String getCommandName() {
return "rollout";
}
public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
final RuntimeState runtimeState = (RuntimeState)request.getAttribute(RuntimeState.class.getName());
boolean backgroundExecution = runtimeState != null;
try {
Object[] paths = request.getParameterValues("path");
if (ArrayUtils.isEmpty((Object[])paths)) {
if (backgroundExecution) {
runtimeState.setProgressMessage(I18n.get((HttpServletRequest)request, (String)"Roll-out Command incomplete: source path missing"));
}
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"No path provided"));
}
String[] targets = request.getParameterValues("msm:targetPath");
RolloutManager.RolloutProgress rolloutProgress = null;
if (backgroundExecution) {
response.setContentType("text/plain; charset=UTF-8");
final ServletOutputStream out = response.getOutputStream();
rolloutProgress = new RolloutManager.RolloutProgress(){
public void reportProgress(String path, String operation) {
try {
String msg = String.format("%s : %s", operation, path);
runtimeState.setProgressMessage(msg);
out.println(msg);
}
catch (IOException e) {
RolloutCommand.this.log.error("Trying to commit status change: {}", (Throwable)e);
}
}
};
Object[] arrobject = new Object[2];
arrobject[0] = Arrays.toString(paths);
arrobject[1] = targets == null ? I18n.get((HttpServletRequest)request, (String)"all LiveCopies") : Arrays.asList(targets);
String description = I18n.get((HttpServletRequest)request, (String)"Rollout of {0} to {1}", (String)null, (Object[])arrobject);
rolloutProgress.reportProgress(description, I18n.get((HttpServletRequest)request, (String)"STARTING"));
}
long startTime = System.currentTimeMillis();
String type = request.getParameter("type");
if ("delete".equals(type) && !ArrayUtils.isEmpty((Object[])request.getParameterValues("paras"))) {
ResourceResolver resolver = request.getResourceResolver();
for (String parPath : request.getParameterValues("paras")) {
Resource resource = resolver.getResource(parPath);
if (resource == null) continue;
resolver.delete(resource);
this.log.debug("Deleted Component at {}", (Object)parPath);
if (rolloutProgress == null) continue;
rolloutProgress.reportProgress("Deleted Component at " + parPath, I18n.get((HttpServletRequest)request, (String)"ROLLOUT"));
}
resolver.commit();
}
Object lastPath = null;
RolloutManager.RolloutParams params = new RolloutManager.RolloutParams();
for (Object path : paths) {
Page masterPage = pageManager.getPage((String)path);
if (masterPage == null) {
this.log.debug("Received request to roll-out non-existing Blueprint at {}: ignore", path);
continue;
}
lastPath = path;
params.rolloutProgress = rolloutProgress;
params.master = masterPage;
params.reset = "true".equals(request.getParameter("reset"));
if ("page".equals(type) || "deep".equals(type)) {
params.isDeep = "deep".equals(type);
} else {
params.isDeep = request.getParameter("shallow") == null || Boolean.valueOf(request.getParameter("shallow")) == false;
params.paragraphs = request.getParameterValues("paras");
}
if (targets == null) {
this.rolloutManager.rollout(params);
continue;
}
for (String target : targets) {
params.targets = new String[]{target};
this.rolloutManager.rollout(params);
}
}
HtmlResponse statusResponse = null;
if (lastPath == null) {
String msg = "Roll-out Command incomplete: source path missing";
if (backgroundExecution) {
runtimeState.setProgressMessage(I18n.get((HttpServletRequest)request, (String)msg));
} else {
statusResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)msg));
}
} else {
String msg = I18n.get((HttpServletRequest)request, (String)"Page rolled-out: {0}. Execution time={1} ms", (String)null, (Object[])new Object[]{Arrays.toString(paths), System.currentTimeMillis() - startTime});
if (backgroundExecution) {
params.rolloutProgress.reportProgress(msg, I18n.get((HttpServletRequest)request, (String)"DONE"));
} else {
statusResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)msg, (String)lastPath);
}
}
return statusResponse;
}
catch (Exception e) {
this.log.error("Error during roll-out.", (Throwable)e);
if (backgroundExecution) {
runtimeState.setProgressMessage(I18n.get((HttpServletRequest)request, (String)"Failed to Roll-out: {}", (String)e.getMessage()));
return null;
}
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)(e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
}
}
protected void bindRolloutManager(RolloutManager rolloutManager) {
this.rolloutManager = rolloutManager;
}
protected void unbindRolloutManager(RolloutManager rolloutManager) {
if (this.rolloutManager == rolloutManager) {
this.rolloutManager = null;
}
}
}