TemplateImpl.java
14.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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.LabeledResource
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.commons.Constants
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.commons.lang.StringUtils
* org.apache.sling.adapter.SlingAdaptable
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.commons.LabeledResource;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.commons.Constants;
import com.day.cq.wcm.core.impl.TemplateUtils;
import com.day.text.Text;
import java.util.regex.Pattern;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.adapter.SlingAdaptable;
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.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TemplateImpl
extends SlingAdaptable
implements Template {
public static final String NN_INITIAL = "initial";
public static final String NN_STRUCTURE = "structure";
public static final String NN_POLICIES = "policies";
public static final String NT_EDITABLE = "editable";
public static final String PN_STATUS = "status";
public static final String PN_STATUS_VALUE_ENABLED = "enabled";
private static final Logger log = LoggerFactory.getLogger(TemplateImpl.class);
public static final String RESOURCE_TYPE = "cq:Template";
private final Resource resource;
private final Node node;
private ValueMap cachedProperties;
public TemplateImpl(Resource resource) {
this.resource = resource;
this.node = (Node)resource.adaptTo(Node.class);
if (this.node == null) {
throw new IllegalArgumentException("Resource argument to must be adaptable to Node");
}
}
public String getPath() {
return this.resource.getPath();
}
public String getName() {
try {
return this.node.getName();
}
catch (RepositoryException e) {
return "";
}
}
public String getTitle() {
return (String)this.getProperties().get("jcr:title", String.class);
}
public String getShortTitle() {
return (String)this.getProperties().get("shortTitle", String.class);
}
public String getDescription() {
return (String)this.getProperties().get("jcr:description", String.class);
}
public String getIconPath() {
try {
if (this.node.hasNode("icon.png")) {
return this.getPath() + "/" + "icon.png";
}
}
catch (RepositoryException e) {
// empty catch block
}
return null;
}
public String getThumbnailPath() {
try {
if (this.node.hasNode("thumbnail.png")) {
return this.getPath() + "/" + "thumbnail.png";
}
}
catch (RepositoryException e) {
// empty catch block
}
return null;
}
public Long getRanking() {
return (Long)this.getProperties().get("ranking", Long.class);
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == Resource.class) {
return (AdapterType)this.resource;
}
if (type == LabeledResource.class) {
return (AdapterType)((Object)this);
}
if (type == Node.class) {
return (AdapterType)this.node;
}
return (AdapterType)super.adaptTo(type);
}
public boolean isAllowed(String parentPath) {
if (parentPath == null) {
return true;
}
Resource parentRes = this.resource.getResourceResolver().getResource(parentPath);
return this.isAllowed(parentRes);
}
public boolean isAllowed(Resource parentRes) {
String status;
String[] allowedPaths;
ValueMap templateProperties;
String parentPath;
String string = parentPath = parentRes != null ? parentRes.getPath() : null;
if (log.isDebugEnabled()) {
log.debug(".");
log.debug("checking isAllowed for {} under {} ...", (Object)this.getPath(), (Object)parentPath);
}
if (!StringUtils.isEmpty((String)(status = (String)(templateProperties = this.getProperties()).get("status", String.class))) && !"enabled".equals(status)) {
log.debug("not allowed because {} is not enabled", (Object)this.getPath());
return false;
}
String[] allowedTemplates = Constants.EMPTY_STRING_ARRAY;
String appName = "";
String pagePath = null;
if (parentRes != null) {
Resource pageResource = parentRes;
try {
int depth;
while (allowedTemplates.length == 0 && pageResource != null && (depth = ((Node)pageResource.adaptTo(Node.class)).getDepth()) != 0) {
Resource content;
if (depth == 2) {
appName = pageResource.getName();
}
if ((content = pageResource.getChild("jcr:content")) != null) {
allowedTemplates = (String[])content.getValueMap().get("cq:allowedTemplates", (Object)Constants.EMPTY_STRING_ARRAY);
pagePath = pageResource.getPath() + "/" + "jcr:content";
}
if (allowedTemplates.length == 0) {
allowedTemplates = (String[])pageResource.getValueMap().get("cq:allowedTemplates", (Object)Constants.EMPTY_STRING_ARRAY);
}
if (allowedTemplates.length != 0) continue;
pageResource = pageResource.getParent();
}
}
catch (RepositoryException e) {
log.warn("error during evaluation of allowed templates: " + (Object)e);
}
if (allowedTemplates.length > 0) {
String path = this.getPath();
boolean allowed = false;
for (int i = 0; !allowed && i < allowedTemplates.length; ++i) {
try {
if (!Pattern.matches(allowedTemplates[i], path)) continue;
allowed = true;
continue;
}
catch (Exception e) {
log.error("Error while checking if " + path + " matches " + allowedTemplates[i], (Throwable)e);
}
}
if (!allowed) {
log.debug("not allowed because not included in {}/{}", (Object)pagePath, (Object)"cq:allowedTemplates");
return false;
}
}
}
if ((allowedPaths = (String[])templateProperties.get("allowedPaths", (Object)Constants.EMPTY_STRING_ARRAY)).length > 0) {
boolean allowed = false;
for (int i = 0; !allowed && i < allowedPaths.length; ++i) {
try {
if (!Pattern.matches(allowedPaths[i], parentPath)) continue;
allowed = true;
continue;
}
catch (Exception e) {
log.error("Error while checking if " + parentPath + " matches " + allowedPaths[i] + " on " + this.getPath(), (Throwable)e);
}
}
if (!allowed) {
log.debug("not allowed because parent path not included in {}/{} (deprecated)", (Object)this.getPath(), (Object)"allowedPaths");
return false;
}
}
if (allowedPaths.length == 0 && allowedTemplates.length == 0 && !this.getPath().startsWith("/apps/" + appName + "/")) {
log.debug("not allowed because not under /apps/{} (2nd level from {})", (Object)appName, (Object)parentPath);
return false;
}
Page parentPage = parentRes == null ? null : (Page)parentRes.adaptTo(Page.class);
Template parentTemplate = parentPage == null ? null : parentPage.getTemplate();
String[] allowedParents = (String[])this.getProperties().get("allowedParents", (Object)Constants.EMPTY_STRING_ARRAY);
if (parentTemplate == null) {
if (log.isDebugEnabled()) {
if (allowedParents.length == 0) {
log.debug("ALLOWED because no parent template found");
} else {
log.debug("not allowed because no parent template found, but {}/{} is set", (Object)this.getPath(), (Object)"allowedParents");
}
}
return allowedParents.length == 0;
}
if (allowedParents.length > 0 && !this.matches(allowedParents, parentTemplate.getPath())) {
log.debug("not allowed because the parent template is not included in {}/{}", (Object)this.getPath(), (Object)"allowedParents");
return false;
}
String[] allowedChildren = this.getAllowedChildren(parentTemplate);
if (allowedChildren.length > 0 && !this.matches(allowedChildren, this.getPath())) {
log.debug("not allowed because this template is not included in {}/{}", (Object)parentTemplate.getPath(), (Object)"allowedChildren");
return false;
}
if (log.isDebugEnabled()) {
if (allowedParents.length == 0 && allowedChildren.length == 0) {
log.debug("ALLOWED (no {} or {} defined)", (Object)"allowedParents", (Object)"allowedChildren");
} else if (allowedChildren.length > 0) {
log.debug("ALLOWED because this template is included in {}/{}", (Object)parentTemplate.getPath(), (Object)"allowedChildren");
} else {
log.debug("ALLOWED because the parent template is included in {}/{}", (Object)this.getPath(), (Object)"allowedParents");
}
}
return true;
}
private boolean matches(String[] allowed, String path) {
for (String p : allowed) {
try {
if (!Pattern.matches(p, path)) continue;
return true;
}
catch (Exception e) {
log.error("Error while checking if " + path + " matches " + p + " on " + this.getPath(), (Throwable)e);
}
}
return false;
}
private String[] getAllowedChildren(Template template) {
Resource parentTplRes = (Resource)template.adaptTo(Resource.class);
if (parentTplRes == null) {
return Constants.EMPTY_STRING_ARRAY;
}
return (String[])((ValueMap)parentTplRes.adaptTo(ValueMap.class)).get("allowedChildren", (Object)Constants.EMPTY_STRING_ARRAY);
}
public boolean isAllowedChild(Template template) {
String[] allowedChildren = (String[])this.getProperties().get("allowedChildren", (Object)Constants.EMPTY_STRING_ARRAY);
String tpPath = template.getPath();
if (allowedChildren.length == 0) {
return true;
}
for (String path : allowedChildren) {
try {
if (!Pattern.matches(path, tpPath)) continue;
return true;
}
catch (Exception e) {
log.error("Error while checking if " + tpPath + " matches " + path + " on " + this.getPath(), (Throwable)e);
}
}
return false;
}
public void write(JSONWriter jsonWriter) throws JSONException {
jsonWriter.object();
String title = this.getTitle();
if (title != null && title.length() > 0) {
jsonWriter.key("title").value((Object)title);
} else {
jsonWriter.key("title").value((Object)Text.getName((String)this.getPath()));
}
jsonWriter.key("ranking").value((Object)this.getRanking());
String desc = this.getDescription();
if (desc != null && desc.length() > 0) {
jsonWriter.key("description").value((Object)desc);
} else {
jsonWriter.key("description").value((Object)"");
}
jsonWriter.key("path").value((Object)this.getPath());
if (this.getThumbnailPath() != null) {
jsonWriter.key("thumbnailPath").value((Object)this.getThumbnailPath());
}
if (this.getIconPath() != null) {
jsonWriter.key("iconPath").value((Object)this.getIconPath());
}
jsonWriter.endObject();
}
private ValueMap internalGetProperties(Resource resource) {
ValueMap properties = ValueMap.EMPTY;
if (TemplateUtils.isAuthoredTemplate(resource)) {
Resource contentResource = resource.getChild("jcr:content");
if (contentResource != null) {
properties = (ValueMap)contentResource.adaptTo(ValueMap.class);
}
} else {
properties = (ValueMap)resource.adaptTo(ValueMap.class);
}
return properties;
}
public String getInitialContentPath() {
Resource initialContentResource = this.getInitialContentResource();
return initialContentResource != null ? initialContentResource.getPath() : null;
}
public ValueMap getProperties() {
if (this.cachedProperties == null) {
this.cachedProperties = this.internalGetProperties(this.resource);
}
return this.cachedProperties;
}
public boolean hasStructureSupport() {
return this.resource.getChild("structure") != null;
}
public String getPageTypePath() {
Resource initialContentResource = this.getInitialContentResource();
return initialContentResource != null ? initialContentResource.getResourceType() : null;
}
private Resource getInitialContentResource() {
if (this.hasStructureSupport()) {
return this.resource.getChild("initial/jcr:content");
}
return this.resource.getChild("jcr:content");
}
}