WCMBindingsValuesProvider.java
10.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentContext
* com.day.cq.wcm.api.components.EditContext
* com.day.cq.wcm.api.designer.Cell
* com.day.cq.wcm.api.designer.Design
* com.day.cq.wcm.api.designer.Designer
* com.day.cq.wcm.api.designer.Style
* com.day.cq.wcm.api.policies.ContentPolicy
* com.day.cq.wcm.api.policies.ContentPolicyManager
* com.day.cq.wcm.commons.WCMUtils
* javax.servlet.ServletRequest
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.scripting.SlingScriptHelper
* org.apache.sling.scripting.api.BindingsValuesProvider
*/
package com.day.cq.wcm.scripting.impl;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.components.EditContext;
import com.day.cq.wcm.api.designer.Cell;
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.day.cq.wcm.commons.WCMUtils;
import com.day.cq.wcm.scripting.impl.ContentPolicyStyle;
import java.util.HashMap;
import javax.script.Bindings;
import javax.servlet.ServletRequest;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.scripting.api.BindingsValuesProvider;
public class WCMBindingsValuesProvider
implements BindingsValuesProvider {
public static final String NAME_COMPONENT_CONTEXT = "componentContext";
public static final String NAME_EDIT_CONTEXT = "editContext";
public static final String NAME_PROPERTIES = "properties";
public static final String NAME_PAGE_MANAGER = "pageManager";
public static final String NAME_CURRENT_PAGE = "currentPage";
public static final String NAME_RESOURCE_PAGE = "resourcePage";
public static final String NAME_PAGE_PROPERTIES = "pageProperties";
public static final String NAME_COMPONENT = "component";
public static final String NAME_DESIGNER = "designer";
public static final String NAME_CURRENT_DESIGN = "currentDesign";
public static final String NAME_RESOURCE_DESIGN = "resourceDesign";
public static final String NAME_CURRENT_STYLE = "currentStyle";
public static final String NAME_XSSAPI = "xssAPI";
public static final String NAME_CURRENT_CONTENT_POLICY = "currentContentPolicy";
public static final String NAME_CURRENT_CONTENT_POLICY_PROPS = "currentContentPolicyProperties";
private static final String REQ_ATTR_PREFIX = WCMBindingsValuesProvider.class.getName() + ":design:";
private static final String REQ_ATTR_PAGE_CACHE = WCMBindingsValuesProvider.class.getName() + ":pageCache";
private XSSAPI xssAPI;
WCMBindingsValuesProvider(XSSAPI xssAPI) {
this.xssAPI = xssAPI;
}
public void addBindings(Bindings bindings) {
ContentPolicyManager policyManager;
SlingHttpServletRequest request = (SlingHttpServletRequest)bindings.get("request");
SlingScriptHelper sling = (SlingScriptHelper)bindings.get("sling");
if (request == null || sling == null) {
return;
}
Resource resource = request.getResource();
ResourceResolver resolver = request.getResourceResolver();
if (resolver == null) {
return;
}
XSSAPI requestSpecificXssAPI = this.xssAPI.getRequestSpecificAPI(request);
ComponentContext componentContext = WCMUtils.getComponentContext((ServletRequest)request);
EditContext editContext = componentContext == null ? null : componentContext.getEditContext();
ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
HierarchyNodeInheritanceValueMap pageProperties = null;
Page currentPage = null;
Page resourcePage = null;
if (pageManager != null) {
resourcePage = WCMBindingsValuesProvider.getContainingPage(pageManager, resource, request);
Page page = currentPage = componentContext == null ? null : componentContext.getPage();
if (currentPage == null) {
currentPage = resourcePage;
}
pageProperties = currentPage == null ? null : new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
}
Component component = WCMUtils.getComponent((Resource)resource);
Designer designer = (Designer)request.getResourceResolver().adaptTo(Designer.class);
if (designer != null) {
Design resourceDesign;
Design currentDesign;
if (currentPage == null) {
currentDesign = null;
} else {
String currentDesignKey = REQ_ATTR_PREFIX + currentPage.getPath();
Object cachedCurrentDesign = request.getAttribute(currentDesignKey);
if (cachedCurrentDesign != null) {
currentDesign = (Design)cachedCurrentDesign;
} else {
currentDesign = designer.getDesign(currentPage);
request.setAttribute(currentDesignKey, (Object)currentDesign);
}
}
if (resourcePage == null) {
resourceDesign = null;
} else {
String resourceDesignkey = REQ_ATTR_PREFIX + resourcePage.getPath();
Object cachedresourceDesign = request.getAttribute(resourceDesignkey);
if (cachedresourceDesign != null) {
resourceDesign = (Design)cachedresourceDesign;
} else {
resourceDesign = designer.getDesign(resourcePage);
request.setAttribute(resourceDesignkey, (Object)resourceDesign);
}
}
Style currentStyle = currentDesign == null || componentContext == null ? null : currentDesign.getStyle(componentContext.getCell());
bindings.put("designer", (Object)designer);
if (currentDesign != null) {
bindings.put("currentDesign", (Object)currentDesign);
}
if (resourceDesign != null) {
bindings.put("resourceDesign", (Object)resourceDesign);
}
if (currentStyle != null) {
bindings.put("currentStyle", (Object)currentStyle);
}
}
ContentPolicy currentPolicy = null;
ValueMap currentPolicyProperties = null;
ContentPolicyStyle contentPolicyStyle = null;
if (componentContext != null && (policyManager = (ContentPolicyManager)resolver.adaptTo(ContentPolicyManager.class)) != null && (currentPolicy = policyManager.getPolicy(componentContext)) != null) {
currentPolicyProperties = currentPolicy.getProperties();
contentPolicyStyle = new ContentPolicyStyle(currentPolicy, componentContext.getCell());
}
if (requestSpecificXssAPI != null) {
bindings.put("xssAPI", (Object)requestSpecificXssAPI);
}
if (componentContext != null) {
bindings.put("componentContext", (Object)componentContext);
}
if (editContext != null) {
bindings.put("editContext", (Object)editContext);
}
if (properties != null) {
bindings.put("properties", (Object)properties);
}
if (pageManager != null) {
bindings.put("pageManager", (Object)pageManager);
}
if (currentPage != null) {
bindings.put("currentPage", (Object)currentPage);
}
if (resourcePage != null) {
bindings.put("resourcePage", (Object)resourcePage);
}
if (pageProperties != null) {
bindings.put("pageProperties", (Object)pageProperties);
}
if (component != null) {
bindings.put("component", (Object)component);
}
if (currentPolicy != null) {
bindings.put("currentContentPolicy", (Object)currentPolicy);
}
if (currentPolicyProperties != null) {
bindings.put("currentContentPolicyProperties", (Object)currentPolicyProperties);
}
if (contentPolicyStyle != null) {
bindings.put("currentStyle", (Object)contentPolicyStyle);
}
}
private static Page getContainingPage(PageManager pageManager, Resource resource, SlingHttpServletRequest request) {
Page page;
String cacheKey = WCMBindingsValuesProvider.getContainingPageCacheKey(resource);
if (cacheKey == null) {
page = null;
} else {
HashMap<String, Page> pageCache = (HashMap<String, Page>)request.getAttribute(REQ_ATTR_PAGE_CACHE);
if (pageCache == null) {
pageCache = new HashMap<String, Page>();
request.setAttribute(REQ_ATTR_PAGE_CACHE, pageCache);
}
if ((page = (Page)pageCache.get(cacheKey)) == null && !pageCache.containsKey(cacheKey)) {
page = pageManager.getContainingPage(resource);
pageCache.put(cacheKey, page);
}
}
return page;
}
static String getContainingPageCacheKey(Resource resource) {
String path;
int idx;
String string = path = resource != null ? resource.getPath() : null;
if (path != null && (idx = path.indexOf("/jcr:content")) >= 0) {
path = path.substring(0, idx);
}
return path;
}
}