CreateLaunchCommand.java
8.51 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchException
* com.adobe.cq.launches.api.LaunchManager
* com.adobe.cq.launches.api.LaunchManager$CreateOptions
* com.adobe.cq.launches.api.LaunchManagerFactory
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.LanguageUtil
* com.day.cq.commons.servlets.HtmlStatusResponseHelper
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.commands.WCMCommandContext
* 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.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.HtmlResponse
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.launches.impl.commands;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.launches.impl.LaunchSourceImpl;
import com.adobe.cq.wcm.launches.impl.commands.LaunchesCommand;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import java.util.ArrayList;
import java.util.Calendar;
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.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
@Service
public class CreateLaunchCommand
extends LaunchesCommand {
private final Logger log = LoggerFactory.getLogger(CreateLaunchCommand.class);
@Reference
private XSSAPI xssAPI;
public String getCommandName() {
return "createLaunch";
}
public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
try {
Calendar liveDate;
String[] shallowValues;
String title;
Resource[] resourceList;
String[] srcPathArray = request.getParameterValues("srcPathList");
ArrayList<String> srcPathList = new ArrayList<String>();
if (srcPathArray != null && srcPathArray.length > 0) {
for (int index = 0; index < srcPathArray.length; ++index) {
if (StringUtils.isEmpty((String)srcPathArray[index])) continue;
srcPathList.add(srcPathArray[index]);
}
} else {
String srcPath = request.getParameter("srcPath");
if (!StringUtils.isEmpty((String)srcPath)) {
srcPathList.add(srcPath);
}
}
if (srcPathList.size() == 0) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Page path needs to be specified."));
}
try {
title = this.getStringParam(request, "title", "title", true, null);
}
catch (LaunchException e) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
}
try {
liveDate = this.getCalendarParam(request, "liveDate", "live date", false, null);
}
catch (LaunchException e) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
}
ResourceResolver resolver = request.getResourceResolver();
LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resolver);
boolean shallow = "true".equals(request.getParameter("shallow"));
boolean isLiveCopy = "true".equals(request.getParameter("isLiveCopy"));
String template = request.getParameter("template");
if (template != null) {
shallow = true;
isLiveCopy = false;
}
if ((shallowValues = request.getParameterValues("shallowList")) == null) {
shallowValues = new String[]{request.getParameter("shallow")};
}
if ((resourceList = new Resource[srcPathList.size()]).length != shallowValues.length) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path list does not match with shallow param list", (String)null));
}
ArrayList<LaunchSourceImpl> launchSourceList = new ArrayList<LaunchSourceImpl>();
String strResourceSitePath = null;
for (int index = 0; index < srcPathList.size(); ++index) {
String srcPath = (String)srcPathList.get(index);
Resource resource = resolver.getResource(srcPath);
if (resource == null) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path does not point to a resource: {0}", (String)null, (Object[])new Object[]{srcPath}));
}
String strSitePath = this.calculateSitePath(resource);
if (strResourceSitePath == null) {
strResourceSitePath = strSitePath;
} else if (!strResourceSitePath.equals(strSitePath)) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"Source path does not point to same site: {0}", (String)null, (Object[])new Object[]{srcPath}));
}
boolean childDeep = !"true".equals(shallowValues[index]);
launchSourceList.add(new LaunchSourceImpl(resource, childDeep));
}
String[] sourceConfigs = request.getParameterValues("sourceRolloutConfigs");
String[] promoteConfigs = request.getParameterValues("promoteRolloutConfigs");
LaunchManager.CreateOptions options = new LaunchManager.CreateOptions();
options.launchSourceList = launchSourceList;
options.resource = null;
options.title = title;
options.liveDate = liveDate;
options.isDeep = !shallow;
options.isLiveCopy = isLiveCopy;
options.template = template;
options.sourceRolloutConfigs = sourceConfigs;
options.promoteRolloutConfigs = promoteConfigs;
Launch launch = launchManager.createLaunch(options);
return HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)I18n.get((HttpServletRequest)request, (String)"Launch created: {0}", (String)null, (Object[])new Object[]{this.xssAPI.filterHTML(launch.getTitle())}), (String)launch.getRootResource().getPath());
}
catch (Exception e) {
this.log.error("Error during creation of launch.", (Throwable)e);
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)I18n.get((HttpServletRequest)request, (String)"{0}", (String)null, (Object[])new Object[]{e.getMessage()}));
}
}
private String calculateSitePath(Resource resource) {
String strRetVal = LanguageUtil.getLanguageRoot((String)resource.getPath());
if (strRetVal == null) {
strRetVal = "";
}
return strRetVal;
}
protected void bindXssAPI(XSSAPI xSSAPI) {
this.xssAPI = xSSAPI;
}
protected void unbindXssAPI(XSSAPI xSSAPI) {
if (this.xssAPI == xSSAPI) {
this.xssAPI = null;
}
}
}