TemplateUtils.java
15.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentContext
* com.day.cq.wcm.api.policies.ContentPolicy
* com.day.cq.wcm.api.policies.ContentPolicyMapping
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.lang3.StringUtils
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyMapping;
import com.day.cq.wcm.core.impl.TemplateConfImpl;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TemplateUtils {
private static final Logger log = LoggerFactory.getLogger(TemplateUtils.class);
private static final String MAPPING_RESOURCE_TYPE = "wcm/core/components/policies/mapping";
private static final String JCR_CONTENT_PATH_CHUNK = "/jcr:content/";
public static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
public static final String PN_CQ_POLICY = "cq:policy";
protected static Resource getDialog(ResourceResolver resolver, Component c, String dialogName) {
Component sc;
Resource resource = (Resource)c.adaptTo(Resource.class);
Resource dialog = resolver.getResource(resource, "cq:" + dialogName);
if (dialog == null && (dialog = resolver.getResource(resource, dialogName)) == null && (sc = c.getSuperComponent()) != null) {
dialog = TemplateUtils.getDialog(resolver, sc, dialogName);
}
return dialog;
}
public static boolean isAuthoredTemplate(Resource resource) {
boolean isAuthored = false;
if (resource != null) {
isAuthored = resource.getChild("initial") != null && resource.getChild("structure") != null && resource.getChild("policies") != null;
}
return isAuthored;
}
public static Template getTemplate(ComponentContext componentContext) {
if (componentContext == null) {
return null;
}
Page page = componentContext.getPage();
return page != null ? page.getTemplate() : null;
}
public static boolean isPageAuthoredTemplated(Page page) {
if (page != null) {
Template template = page.getTemplate();
return template != null && template.hasStructureSupport();
}
return false;
}
public static boolean isPageOfAuthoredTemplate(Page page) {
return TemplateUtils.isPageOfAuthoredTemplate((Resource)page.adaptTo(Resource.class));
}
public static boolean isPageOfAuthoredTemplate(Resource resource) {
if (resource == null) {
return false;
}
boolean isNamedAccordingly = "structure".equals(resource.getName()) || "initial".equals(resource.getName());
boolean isOfPageResourceType = "cq:Page".equals(resource.getValueMap().get((Object)"jcr:primaryType"));
Resource parent = resource.getParent();
boolean isParentOfTemplateResourceType = false;
boolean isParentAuthoredTempalte = false;
if (parent != null) {
isParentOfTemplateResourceType = "cq:Template".equals(resource.getParent().getValueMap().get((Object)"jcr:primaryType"));
isParentAuthoredTempalte = TemplateUtils.isAuthoredTemplate(resource.getParent());
}
return isNamedAccordingly && isOfPageResourceType && isParentOfTemplateResourceType && isParentAuthoredTempalte;
}
public static boolean isTemplateStructureComponent(ComponentContext componentContext) {
return TemplateUtils.isTemplateStructureResource(componentContext.getResource());
}
public static boolean isTemplateStructureResource(Resource resource) {
Resource structureResource = TemplateUtils.getStructureResource(resource);
if (structureResource != null && ResourceUtil.isStarResource((Resource)structureResource)) {
structureResource = structureResource.getParent();
}
return structureResource != null;
}
public static boolean isTemplateStructureComponentLocked(ComponentContext componentContext) {
return TemplateUtils.isTemplateStructureResourceLocked(TemplateUtils.getStructureResource(componentContext.getResource()));
}
public static boolean isTemplateStructureResourceLocked(Resource structureResource) {
boolean locked = false;
if (structureResource != null) {
ValueMap properties;
if (ResourceUtil.isStarResource((Resource)structureResource)) {
structureResource = structureResource.getParent();
}
if (structureResource != null && (properties = (ValueMap)structureResource.adaptTo(ValueMap.class)) != null) {
Boolean editable = (Boolean)properties.get("editable", Boolean.class);
locked = editable == null || editable == false;
}
}
return locked;
}
private static boolean isPageAuthoredTemplateOfName(Page page, String name) {
if (StringUtils.isEmpty((CharSequence)name)) {
return false;
}
boolean isTemplateStructurePage = false;
Template template = page.getTemplate();
if (page != null && template != null && template.hasStructureSupport() && name.equals(page.getName())) {
isTemplateStructurePage = true;
}
return isTemplateStructurePage;
}
public static boolean isTemplateInitialPage(ComponentContext componentContext) {
return componentContext != null && TemplateUtils.isPageAuthoredTemplateOfName(componentContext.getPage(), "initial");
}
public static boolean isTemplateInitialPage(Page page) {
return TemplateUtils.isPageAuthoredTemplateOfName(page, "initial");
}
public static boolean isTemplateStructurePage(ComponentContext componentContext) {
return componentContext != null && TemplateUtils.isPageAuthoredTemplateOfName(componentContext.getPage(), "structure");
}
public static boolean isTemplateStructurePage(Page page) {
return TemplateUtils.isPageAuthoredTemplateOfName(page, "structure");
}
public static String getComponentRelativePath(Resource resource) {
if (resource == null) {
return null;
}
ResourceResolver resourceResolver = resource.getResourceResolver();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
if (pageManager == null) {
return null;
}
Page page = pageManager.getContainingPage(resource);
if (page == null) {
return null;
}
String resourcePath = resource.getPath();
if (resourcePath.equals(page.getPath())) {
return resourcePath;
}
resourcePath = (resourcePath = resourcePath.substring(page.getPath().length(), resourcePath.length())).startsWith("/") && resourcePath.length() > 1 ? resourcePath.substring(1, resourcePath.length()) : resourcePath;
return resourcePath;
}
public static String getPageContentRelativePath(Resource resource) {
if (resource == null) {
return null;
}
PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.getContainingPage(resource);
if (page != null) {
if (resource.getPath().equals(page.getContentResource().getPath())) {
return "";
}
return resource.getPath().replace(page.getContentResource().getPath() + "/", "");
}
return null;
}
private static Resource getTemplatePageRoot(Template template, String childName) {
Resource templateResource;
Resource templatePageRoot = null;
if (template != null && (templateResource = (Resource)template.adaptTo(Resource.class)) != null) {
templatePageRoot = templateResource.getChild(childName);
}
return templatePageRoot;
}
public static Resource getStructureResource(Resource resource) {
return TemplateUtils.getAspectResource(resource, "structure");
}
private static Resource getAspectResource(Resource resource, String aspectName) {
Resource templateStructureRoot;
Resource structureResource = null;
ResourceResolver resourceResolver = resource.getResourceResolver();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
if (pageManager == null) {
return null;
}
Page page = pageManager.getContainingPage(resource);
if (page == null) {
return null;
}
Template template = page.getTemplate();
if (template != null && template.hasStructureSupport() && (templateStructureRoot = TemplateUtils.getTemplatePageRoot(template, aspectName)) != null) {
if (resource.getPath().startsWith(templateStructureRoot.getPath())) {
return resource;
}
String componentRelativePath = TemplateUtils.getComponentRelativePath(resource);
if (StringUtils.isNotBlank((CharSequence)componentRelativePath)) {
structureResource = templateStructureRoot.getChild(componentRelativePath);
}
}
return structureResource;
}
public static void removePropertyRecursive(Node node, String propertyName) throws RepositoryException {
if (node != null && node.hasNodes()) {
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
Node child = iter.nextNode();
String childName = child.getName();
if ("cq:responsive".equals(childName)) continue;
if (!"jcr:content".equals(childName)) {
child.setProperty(propertyName, (String[])null);
}
TemplateUtils.removePropertyRecursive(child, propertyName);
}
}
}
public static Resource getCorrespondingStructureResource(Template template, Resource resource) {
Resource templateStructureRoot;
if (resource != null && template != null && template.hasStructureSupport() && (templateStructureRoot = TemplateUtils.getTemplatePageRoot(template, "structure")) != null) {
if (resource.getPath().startsWith(templateStructureRoot.getPath())) {
return resource;
}
String resourceRelativePath = TemplateUtils.getPageContentRelativePath(resource);
if (StringUtils.isNotEmpty((CharSequence)resourceRelativePath)) {
return templateStructureRoot.getChild("jcr:content/" + resourceRelativePath);
}
}
return null;
}
public static String getPageContentRelativePath(String resourcePath) {
int jcrContentLength = "/jcr:content/".length();
int jcrIndex = resourcePath.indexOf("/jcr:content/");
return jcrIndex > -1 && jcrIndex + jcrContentLength < resourcePath.length() ? resourcePath.substring(jcrIndex + jcrContentLength, resourcePath.length()) : "";
}
public static Resource getPolicyResource(ResourceResolver resolver, String policyMappingResourcePath) {
ContentPolicyMapping contentPolicyMapping;
ContentPolicy policy;
Resource policyMappingResource = resolver.getResource(policyMappingResourcePath);
if (policyMappingResource != null && (contentPolicyMapping = (ContentPolicyMapping)policyMappingResource.adaptTo(ContentPolicyMapping.class)) != null && (policy = contentPolicyMapping.getPolicy()) != null) {
return (Resource)policy.adaptTo(Resource.class);
}
return null;
}
public static boolean hasPolicyMapping(ResourceResolver resolver, String policyMappingResourcePath) {
return TemplateUtils.getPolicyResource(resolver, policyMappingResourcePath) != null;
}
public static String getRelativePath(Resource resource) {
if (resource == null) {
return null;
}
return TemplateUtils.getRelativePath(resource.getResourceResolver(), resource.getPath());
}
public static String getRelativePath(ResourceResolver resolver, String path) {
if (StringUtils.isEmpty((CharSequence)path)) {
return null;
}
String[] searchPaths = resolver.getSearchPath();
for (int i = 0; i < searchPaths.length; ++i) {
String searchPath = searchPaths[i];
if (!path.startsWith(searchPath)) continue;
return path.substring(searchPath.length(), path.length());
}
return path;
}
public static String getPolicyDirectoryPath(ComponentContext componentContext) {
return TemplateUtils.getPolicyDirectoryPath(componentContext.getResource());
}
public static String getPolicyDirectoryPath(Resource resource) {
TemplateConfImpl conf;
Resource policyDirectory;
Resource templateResource;
ResourceResolver resourceResolver = resource.getResourceResolver();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
if (pageManager == null) {
return null;
}
Page page = pageManager.getContainingPage(resource);
if (page == null) {
return null;
}
Template template = page.getTemplate();
if (template != null && template.hasStructureSupport() && (templateResource = (Resource)template.adaptTo(Resource.class)) != null && (conf = new TemplateConfImpl(templateResource)) != null && (policyDirectory = conf.getItemResource("wcm/policies")) != null) {
return policyDirectory.getPath();
}
return null;
}
public static boolean isAuthoredTemplateRestricted(ComponentContext componentContext) {
Page page = componentContext.getPage();
Template template = TemplateUtils.getTemplate(componentContext);
String resourcePath = componentContext.getResource().getPath();
if (template != null && TemplateUtils.isAuthoredTemplate((Resource)template.adaptTo(Resource.class))) {
String structureResourcePath = resourcePath.indexOf("/structure/jcr:content/") > -1 || resourcePath.indexOf("/structure/_jcr_content/") > -1 ? resourcePath : template.getPath() + "/" + "structure" + resourcePath.replace(page.getPath(), "");
structureResourcePath = structureResourcePath.replace("/*", "");
Resource structureResource = componentContext.getResource().getResourceResolver().getResource(structureResourcePath);
if (structureResource != null) {
Boolean editable = (Boolean)structureResource.getValueMap().get("editable", Boolean.class);
if (!TemplateUtils.isPageOfAuthoredTemplate(page) && (resourcePath.startsWith(template.getPath()) || structureResource != null && editable == null || !editable.booleanValue())) {
return true;
}
}
}
return false;
}
}