ComponentHelper.java
17 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* com.day.cq.i18n.I18n
* javax.servlet.RequestDispatcher
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* javax.servlet.http.HttpServletRequest
* javax.servlet.jsp.PageContext
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestDispatcherOptions
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.scripting.SlingBindings
* org.apache.sling.api.scripting.SlingScriptHelper
* org.apache.sling.api.servlets.ServletResolver
* org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper
*/
package com.adobe.granite.ui.components;
import com.adobe.granite.ui.components.AttrBuilder;
import com.adobe.granite.ui.components.Config;
import com.adobe.granite.ui.components.ExpressionHelper;
import com.adobe.granite.ui.components.ExpressionResolver;
import com.adobe.granite.ui.components.LayoutBuilder;
import com.adobe.granite.ui.components.State;
import com.adobe.granite.ui.components.Tag;
import com.adobe.granite.ui.components.Value;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.EmptyDataSource;
import com.adobe.granite.ui.components.ds.ResourceDataSource;
import com.adobe.granite.ui.components.rendercondition.RenderCondition;
import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestDispatcherOptions;
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.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ComponentHelper {
private static final String DEFAULT_LAYOUT_RT = "granite/ui/components/foundation/layouts/container";
private static final String ATTRIBUTE_CACHE_RC = ComponentHelper.class.getName() + ".cache.rc";
private PageContext pageContext;
private SlingScriptHelper sling;
private SlingHttpServletRequest request;
private I18n i18n;
private XSSAPI xss;
private Config config;
private Value value;
private ExpressionHelper ex;
private State state;
private OptionsHolder optionsHolder;
public ComponentHelper(PageContext pageContext) {
this.pageContext = pageContext;
SlingBindings bindings = (SlingBindings)pageContext.getRequest().getAttribute(SlingBindings.class.getName());
this.request = bindings.getRequest();
this.sling = bindings.getSling();
this.optionsHolder = (OptionsHolder)this.request.getAttribute(OptionsHolder.class.getName());
this.request.removeAttribute(OptionsHolder.class.getName());
}
public I18n getI18n() {
if (this.i18n == null) {
this.i18n = new I18n((HttpServletRequest)this.request);
}
return this.i18n;
}
public XSSAPI getXss() {
if (this.xss == null) {
this.xss = ((XSSAPI)this.sling.getService(XSSAPI.class)).getRequestSpecificAPI(this.request);
}
return this.xss;
}
public Config getConfig() {
if (this.config == null) {
this.config = new Config(this.request.getResource());
}
return this.config;
}
public Value getValue() {
if (this.value == null) {
this.value = new Value(this.request, this.getConfig());
}
return this.value;
}
public ExpressionHelper getExpressionHelper() {
if (this.ex == null) {
this.ex = new ExpressionHelper((ExpressionResolver)this.sling.getService(ExpressionResolver.class), this.pageContext);
}
return this.ex;
}
public State getState() {
if (this.state == null) {
this.state = new State(this.request);
}
return this.state;
}
public Tag consumeTag() {
Tag tag = this.getOptions().tag();
return tag == null ? new Tag(new AttrBuilder((HttpServletRequest)this.request, this.getXss())) : tag;
}
public Resource consumeLayoutResource() {
Resource layout = this.getOptions().layoutResource();
return layout == null ? this.request.getResource().getChild(Config.LAYOUT) : layout;
}
public void populateCommonAttrs(AttrBuilder attrs) {
this.populateCommonAttrs(attrs, this.request.getResource());
}
public void populateCommonAttrs(AttrBuilder attrs, Resource src) {
I18n i18n = this.getI18n();
Config config = new Config(src);
ExpressionHelper ex = this.getExpressionHelper();
attrs.add("id", ex.getString(config.get("granite:id", String.class)));
attrs.addRel(ex.getString(config.get("granite:rel", String.class)));
attrs.addClass(ex.getString(config.get("granite:class", String.class)));
attrs.add("title", i18n.getVar(config.get("granite:title", String.class)));
attrs.addBoolean("hidden", config.get("granite:hidden", false));
attrs.addBoolean("itemscope", config.get("granite:itemscope", false));
attrs.add("itemtype", config.get("granite:itemtype", String.class));
attrs.add("itemprop", config.get("granite:itemprop", String.class));
Resource data = src.getChild("granite:data");
if (data == null) {
return;
}
for (Map.Entry e : data.getValueMap().entrySet()) {
String key = (String)e.getKey();
if (key.indexOf(":") >= 0) continue;
Object v = e.getValue();
if (v instanceof String) {
v = ex.getString(v.toString());
}
attrs.addOther(key, v.toString());
}
}
public Options getOptions() {
if (this.optionsHolder != null && this.optionsHolder.getPath().equals(this.request.getResource().getPath())) {
return this.optionsHolder.getOptions();
}
return new Options();
}
public LayoutBuilder getLayout() {
return LayoutBuilder.from(this.consumeLayoutResource(), "granite/ui/components/foundation/layouts/container");
}
public String getReadOnlyResourceType() {
return this.getReadOnlyResourceType(this.request.getResource());
}
public String getReadOnlyResourceType(Resource resource) {
String resourceType = ComponentHelper.getResourceType(resource);
if (resourceType == null) {
return null;
}
Resource r = this.request.getResourceResolver().getResource(resourceType);
if (r == null) {
return null;
}
Resource ro = r.getChild("readonly");
if (ro != null) {
return ro.getPath();
}
return new Config(r).get("granite:readOnlyResourceType", String.class);
}
public DataSource getItemDataSource() throws ServletException, IOException {
return this.getItemDataSource(this.request.getResource());
}
public DataSource getItemDataSource(Resource resource) throws ServletException, IOException {
Resource items = resource.getChild(Config.ITEMS);
if (items != null) {
return new ResourceDataSource(items);
}
Resource datasource = resource.getChild(Config.DATASOURCE);
if (datasource != null) {
return this.asDataSource(datasource, resource);
}
return EmptyDataSource.instance();
}
public DataSource asDataSource(Resource datasource) throws ServletException, IOException {
return this.asDataSource(datasource, null);
}
public DataSource asDataSource(Resource datasource, Resource context) throws ServletException, IOException {
DataSource ds;
if (datasource == null) {
return null;
}
if (context == null) {
context = datasource;
}
return (ds = this.fetchData(context, ComponentHelper.getResourceType(datasource), DataSource.class)) != null ? ds : EmptyDataSource.instance();
}
public RenderCondition getRenderCondition() throws ServletException, IOException {
return this.getRenderCondition(this.request.getResource());
}
public RenderCondition getRenderCondition(Resource resource) throws ServletException, IOException {
RenderCondition rc = null;
Resource condition = resource.getChild("granite:rendercondition");
if (condition == null) {
condition = resource.getChild(Config.RENDERCONDITION);
}
if (condition != null) {
String resourceType = ComponentHelper.getResourceType(condition, "granite/ui/components/foundation/renderconditions/simple");
rc = this.fetchData(condition, resourceType, RenderCondition.class);
}
if (rc == null) {
rc = SimpleRenderCondition.TRUE;
}
return rc;
}
public RenderCondition getRenderCondition(Resource resource, boolean cache) throws ServletException, IOException {
String key;
Map<String, RenderCondition> cacheMap = this.getRenderConditionCache();
RenderCondition rc = cacheMap.get(key = resource.getPath());
if (rc != null) {
return rc;
}
Resource condition = resource.getChild("granite:rendercondition");
if (condition != null) {
rc = this.fetchData(condition, ComponentHelper.getResourceType(condition), RenderCondition.class);
}
if (rc == null) {
rc = SimpleRenderCondition.TRUE;
}
if (cache) {
cacheMap.put(key, rc);
}
return rc;
}
private Map<String, RenderCondition> getRenderConditionCache() {
HashMap cache = (HashMap)this.request.getAttribute(ATTRIBUTE_CACHE_RC);
if (cache == null) {
cache = new HashMap();
this.request.setAttribute(ATTRIBUTE_CACHE_RC, cache);
}
return cache;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private <T> T fetchData(Resource resource, String resourceType, Class<T> type) throws ServletException, IOException {
if (resourceType == null) {
return null;
}
try {
RequestDispatcher dispatcher = this.request.getRequestDispatcher(resource, new RequestDispatcherOptions(resourceType));
if (dispatcher != null) {
dispatcher.include((ServletRequest)this.request, (ServletResponse)new JspSlingHttpServletResponseWrapper(this.pageContext));
Object object = this.request.getAttribute(type.getName());
return (T)object;
}
T t = null;
return t;
}
finally {
this.request.removeAttribute(type.getName());
}
}
public String getIconClass(String icon) {
if (icon == null) {
return null;
}
if (!icon.startsWith("icon-")) {
return icon;
}
return "coral-Icon--" + ComponentHelper.toCamel(icon.substring(5));
}
private static String toCamel(String s) {
String[] parts = s.split("-");
StringBuilder b = new StringBuilder();
b.append(parts[0]);
for (int i = 1; i < parts.length; ++i) {
b.append(parts[i].substring(0, 1).toUpperCase());
b.append(parts[i].substring(1));
}
return b.toString();
}
private static String getResourceType(Resource resource) {
return new Config(resource).get("sling:resourceType", String.class);
}
private static String getResourceType(Resource resource, String defaultValue) {
return new Config(resource).get("sling:resourceType", defaultValue);
}
public void include(Resource resource, Tag tag) throws ServletException, IOException {
this.include(resource, null, tag);
}
public void include(Resource resource, Options options) throws ServletException, IOException {
this.include(resource, null, options);
}
public void include(Resource resource, String resourceType, Tag tag) throws ServletException, IOException {
this.include(resource, resourceType, new Options().tag(tag));
}
public void include(Resource resource, String resourceType, Options options) throws ServletException, IOException {
this.include(resource, resourceType, null, options);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void include(Resource resource, String resourceType, String selectors, Options options) throws ServletException, IOException {
try {
RequestDispatcher dispatcher;
OptionsHolder holder = new OptionsHolder(options, resource.getPath());
this.request.setAttribute(OptionsHolder.class.getName(), (Object)holder);
RequestDispatcherOptions dispatcherOptions = new RequestDispatcherOptions(resourceType);
if (selectors != null && selectors.length() > 0) {
dispatcherOptions.setReplaceSelectors(selectors);
}
if ((dispatcher = this.request.getRequestDispatcher(resource, dispatcherOptions)) != null) {
dispatcher.include((ServletRequest)this.request, (ServletResponse)new JspSlingHttpServletResponseWrapper(this.pageContext));
}
}
finally {
this.request.removeAttribute(OptionsHolder.class.getName());
}
}
public void includeForLayout(Resource resource, Options options) throws ServletException, IOException {
this.includeForLayout(resource, null, options);
}
public void includeForLayout(Resource resource, Resource layoutResource, Options options) throws ServletException, IOException {
String resourceType = null;
if (layoutResource == null) {
Resource r = resource.getChild(Config.LAYOUT);
if (r != null) {
resourceType = ComponentHelper.getResourceType(r);
}
} else {
resourceType = ComponentHelper.getResourceType(layoutResource);
options.layoutResource(layoutResource);
}
if (resourceType == null) {
resourceType = "granite/ui/components/foundation/layouts/container";
}
this.include(resource, resourceType, options);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void call(String script, Options options) throws ServletException, IOException {
try {
OptionsHolder holder = new OptionsHolder(options, this.request.getResource().getPath());
this.request.setAttribute(OptionsHolder.class.getName(), (Object)holder);
ServletResolver servletResolver = (ServletResolver)this.sling.getService(ServletResolver.class);
Servlet servlet = servletResolver.resolveServlet(this.request.getResource(), script);
if (servlet == null) {
throw new ServletException("Could not find script " + script);
}
servlet.service((ServletRequest)this.request, (ServletResponse)new JspSlingHttpServletResponseWrapper(this.pageContext));
}
finally {
this.request.removeAttribute(OptionsHolder.class.getName());
}
}
public static class Options {
private Tag tag;
private boolean rootField = true;
private Resource layout;
public Tag tag() {
return this.tag;
}
public Options tag(Tag tag) {
this.tag = tag;
return this;
}
public boolean rootField() {
return this.rootField;
}
public Options rootField(boolean flag) {
this.rootField = flag;
return this;
}
public Resource layoutResource() {
return this.layout;
}
public Options layoutResource(Resource r) {
this.layout = r;
return this;
}
}
private class OptionsHolder {
private Options options;
private String path;
public OptionsHolder(Options options, String path) {
this.options = options;
this.path = path;
}
public Options getOptions() {
return this.options;
}
public String getPath() {
return this.path;
}
}
}