FormResourceEdit.java
13.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.RequestDispatcher
* javax.servlet.ServletException
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.commons.collections.CollectionUtils
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestDispatcherOptions
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.foundation.forms;
import com.day.cq.wcm.foundation.forms.MergedMultiResource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class FormResourceEdit {
private static final Logger log = LoggerFactory.getLogger(FormResourceEdit.class);
public static final String RESOURCES_ATTRIBUTE = "cq.form.editresources";
public static final String RESOURCES_PARAM = ":resource";
public static final String REOPEN_PARAM = "reopen";
public static final String WRITE_SUFFIX = "@Write";
public static void setResources(ServletRequest req, List<Resource> resources) {
req.setAttribute("cq.form.editresources", resources);
}
public static List<Resource> getResources(ServletRequest req) {
return (List)req.getAttribute("cq.form.editresources");
}
public static Resource getMergedResource(List<Resource> resources) {
return new MergedMultiResource(resources);
}
public static CommonAndPartial getCommonAndPartialMultiValues(List<Resource> resources, String name) {
CommonAndPartial r = new CommonAndPartial();
boolean firstResource = true;
for (Resource resource : resources) {
ValueMap map = (ValueMap)resource.adaptTo(ValueMap.class);
if (map == null) continue;
String[] values = (String[])map.get(name, (Object)new String[0]);
if (firstResource) {
for (String v : values) {
r.common.add(v);
}
firstResource = false;
continue;
}
List<String> newValues = Arrays.asList(values);
r.partial.addAll(CollectionUtils.disjunction(r.common, newValues));
r.common = new HashSet<String>(CollectionUtils.intersection(r.common, newValues));
}
return r;
}
public static boolean isSingleResource(ServletRequest req) {
List<Resource> r = FormResourceEdit.getResources(req);
return r != null && r.size() == 1;
}
public static boolean isMultiResource(ServletRequest req) {
List<Resource> r = FormResourceEdit.getResources(req);
return r != null && r.size() > 1;
}
public static boolean isSingleResourcePost(SlingHttpServletRequest request) {
RequestParameter[] resourceParams = request.getRequestParameters(":resource");
return resourceParams == null || resourceParams.length == 1;
}
public static boolean isMultiResourcePost(SlingHttpServletRequest request) {
RequestParameter[] resourceParams = request.getRequestParameters(":resource");
return resourceParams != null && resourceParams.length > 1;
}
public static String getPostResourcePath(SlingHttpServletRequest request) {
RequestParameter[] resourceParams = request.getRequestParameters(":resource");
if (resourceParams != null && resourceParams.length == 1) {
return resourceParams[0].getString();
}
return null;
}
public static List<Resource> getPostResources(SlingHttpServletRequest request) {
ResourceResolver resolver = request.getResourceResolver();
RequestParameter[] resourceParams = request.getRequestParameters(":resource");
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
ArrayList<Resource> resources = new ArrayList<Resource>();
if (resourceParams != null) {
for (RequestParameter rp : resourceParams) {
Resource r = resolver.getResource(rp.getString());
try {
if (r == null || !session.hasPermission(r.getPath(), "set_property")) continue;
resources.add(r);
continue;
}
catch (RepositoryException e) {
log.error("Could not check write permission on node", (Throwable)e);
}
}
}
return resources;
}
public static void multiPost(List<Resource> resources, SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
RequestParameterMap originalParams = request.getRequestParameterMap();
TreeMap groupedParams = new TreeMap();
boolean requireItemPrefix = false;
HashSet<String> paramsToKeep = new HashSet<String>();
HashSet<String> paramsToRemove = new HashSet<String>();
for (Map.Entry param : originalParams.entrySet()) {
String op;
TreeMap map;
int pos;
String propName;
String name = (String)param.getKey();
if (":operation".equals(name) && !"modify".equals(op = originalParams.getValue(name).getString())) {
throw new ServletException("Only :operation=modify can be used when posting to multiple resources (was: '" + op + "')");
}
if (name.startsWith("./")) {
requireItemPrefix = true;
}
if ((map = (TreeMap)groupedParams.get(propName = (pos = name.indexOf("@")) >= 0 ? name.substring(0, pos) : name)) == null) {
map = new TreeMap();
groupedParams.put(propName, map);
}
map.put(name, param.getValue());
if (name.endsWith("@Write")) {
paramsToKeep.add(propName);
map.remove(name);
}
if (":formid".equals(name) || ":formstart".equals(name) || ":resource".equals(name)) continue;
if (name.startsWith(":")) {
paramsToKeep.add(name);
}
if (!name.endsWith("@MoveFrom")) continue;
paramsToRemove.add(propName);
}
Iterator iter = groupedParams.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
if (paramsToKeep.contains(name) && !paramsToRemove.contains(name)) continue;
iter.remove();
}
ParameterMap params = new ParameterMap();
log.debug("posting to multiple resources:");
boolean first = true;
for (Resource r : resources) {
String path = r.getPath();
log.debug("{}", (Object)path);
for (Map p : groupedParams.values()) {
for (Map.Entry param2 : p.entrySet()) {
String name = (String)param2.getKey();
if (name.startsWith(":") || name.startsWith("/")) {
if (!first) continue;
params.put(name, param2.getValue());
continue;
}
if (requireItemPrefix) {
if (name.startsWith("./")) {
params.put(path + "/" + name.substring("./".length()), param2.getValue());
continue;
}
if (!name.startsWith("../")) continue;
path = Text.getRelativeParent((String)path, (int)1);
params.put(path + "/" + name.substring("../".length()), param2.getValue());
continue;
}
params.put(path + "/" + name, param2.getValue());
}
}
first = false;
}
if (log.isDebugEnabled()) {
log.debug("rewritten parameters:");
FormResourceEdit.logParams(params);
}
RequestDispatcherOptions options = new RequestDispatcherOptions();
options.setReplaceSelectors("");
options.setReplaceSuffix("");
RequestDispatcher dispatcher = request.getRequestDispatcher(request.getResource(), options);
dispatcher.forward((ServletRequest)new CustomParameterRequest(request, params), (ServletResponse)response);
}
private static void logParams(Map<String, RequestParameter[]> parameters) {
for (Map.Entry<String, RequestParameter[]> ps : parameters.entrySet()) {
for (RequestParameter rp : ps.getValue()) {
log.debug("{} = {}", (Object)ps.getKey(), (Object)rp.getString());
}
}
}
private static class ParameterMap
extends TreeMap<String, RequestParameter[]>
implements RequestParameterMap {
private static final long serialVersionUID = 4554110574522792609L;
private Map<String, String[]> stringParameterMap;
private ParameterMap() {
}
public RequestParameter[] getValues(String name) {
return (RequestParameter[])this.get(name);
}
public RequestParameter getValue(String name) {
RequestParameter[] params = (RequestParameter[])this.get(name);
return params != null && params.length > 0 ? params[0] : null;
}
public String getStringValue(String name) {
RequestParameter param = this.getValue(name);
return param != null ? param.getString() : null;
}
public String[] getStringValues(String name) {
return ParameterMap.toStringArray(this.getValues(name));
}
public Map<String, String[]> getStringParameterMap() {
if (this.stringParameterMap == null) {
LinkedHashMap pm = new LinkedHashMap();
for (Map.Entry ppmEntry : this.entrySet()) {
pm.put(ppmEntry.getKey(), ParameterMap.toStringArray((RequestParameter[])ppmEntry.getValue()));
}
this.stringParameterMap = Collections.unmodifiableMap(pm);
}
return this.stringParameterMap;
}
private static String[] toStringArray(RequestParameter[] params) {
if (params == null) {
return null;
}
String[] ps = new String[params.length];
for (int i = 0; i < params.length; ++i) {
ps[i] = params[i].getString();
}
return ps;
}
}
private static class CustomParameterRequest
extends SlingHttpServletRequestWrapper {
private ParameterMap parameters;
public CustomParameterRequest(SlingHttpServletRequest request, ParameterMap params) {
super(request);
this.parameters = params;
}
public RequestParameter getRequestParameter(String name) {
return this.parameters.getValue(name);
}
public RequestParameterMap getRequestParameterMap() {
return this.parameters;
}
public RequestParameter[] getRequestParameters(String name) {
return this.parameters.getValues(name);
}
public String getParameter(String name) {
return this.parameters.getStringValue(name);
}
public Map getParameterMap() {
return this.parameters.getStringParameterMap();
}
public Enumeration getParameterNames() {
return Collections.enumeration(this.parameters.keySet());
}
public String[] getParameterValues(String name) {
return this.parameters.getStringValues(name);
}
}
public static class CommonAndPartial {
public Set<String> common = new HashSet<String>();
public Set<String> partial = new HashSet<String>();
}
}