1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.wcm.parsys.componentinfo.impl;
21
22 import static io.wcm.wcm.parsys.ParsysNameConstants.NN_PARSYS_CONFIG;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.regex.Pattern;
29
30 import org.apache.commons.lang3.ArrayUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.jackrabbit.util.Text;
33 import org.apache.sling.api.resource.Resource;
34 import org.apache.sling.api.resource.ResourceResolver;
35 import org.apache.sling.api.resource.ValueMap;
36 import org.jetbrains.annotations.NotNull;
37
38 import com.day.cq.commons.jcr.JcrConstants;
39
40 import io.wcm.wcm.parsys.componentinfo.ParsysConfig;
41
42
43
44
45
46 final class ResourceParsysConfigProvider {
47
48 private static final String NN_PATHS = "paths";
49 private static final String PN_PATH = "path";
50 private static final String PN_PATTERN = "pattern";
51 private static final String PN_ALLOWEDCHILDREN = "allowedChildren";
52 private static final String PN_DENIEDDCHILDREN = "deniedChildren";
53 private static final String PN_ALLOWEDPARENTS = "allowedParents";
54 private static final String PN_PARENTANCESTORLEVEL = "parentAncestorLevel";
55 private static final String PN_INHERIT = "inherit";
56
57 private final List<ParsysConfig> pathDefs;
58
59
60
61
62 ResourceParsysConfigProvider(Resource pageComponentResource) {
63 this.pathDefs = getPathDefs(pageComponentResource);
64 }
65
66 private static List<ParsysConfig> getPathDefs(Resource pageComponentResource) {
67 List<ParsysConfig> pathDefs = new ArrayList<>();
68
69 ResourceResolver resourceResolver = pageComponentResource.getResourceResolver();
70 Resource pathsResource = resourceResolver.getResource(pageComponentResource, "./" + NN_PARSYS_CONFIG + "/" + NN_PATHS);
71 if (pathsResource != null) {
72 Iterator<Resource> pathDefResources = resourceResolver.listChildren(pathsResource);
73 while (pathDefResources.hasNext()) {
74 Resource pathDefResource = pathDefResources.next();
75 pathDefs.add(new PathDef(pathDefResource, pageComponentResource.getResourceType()));
76 }
77 }
78
79 return pathDefs;
80 }
81
82
83
84
85 public List<ParsysConfig> getPathDefs() {
86 return this.pathDefs;
87 }
88
89
90
91
92 private static class PathDef implements ParsysConfig {
93
94 private final String pageComponentPath;
95 private final Pattern pathPattern;
96 private final Set<String> allowedChildren;
97 private final Set<String> deniedChildren;
98 private final Set<String> allowedParents;
99 private final int parentAncestorLevel;
100 private final boolean inheritFromSuperType;
101
102
103
104
105
106 PathDef(Resource pathDefResource, String pageComponentPath) {
107 this.pageComponentPath = pageComponentPath;
108 ValueMap pathDefProps = pathDefResource.getValueMap();
109
110
111 String path = pathDefProps.get(PN_PATH, String.class);
112 String patternString = pathDefProps.get(PN_PATTERN, String.class);
113 if (StringUtils.isNotEmpty(patternString)) {
114 this.pathPattern = Pattern.compile(patternString);
115 }
116 else if (StringUtils.isNotBlank(path)) {
117 this.pathPattern = Pattern.compile("^" + Pattern.quote(path) + "$");
118 }
119 else {
120 String name = Text.getName(pathDefResource.getPath());
121 this.pathPattern = Pattern.compile("^" + Pattern.quote(JcrConstants.JCR_CONTENT + "/" + name) + "$");
122 }
123
124
125 this.allowedChildren = Set.of(pathDefProps.get(PN_ALLOWEDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
126 this.deniedChildren = Set.of(pathDefProps.get(PN_DENIEDDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
127 this.allowedParents = Set.of(pathDefProps.get(PN_ALLOWEDPARENTS, ArrayUtils.EMPTY_STRING_ARRAY));
128
129
130 this.parentAncestorLevel = pathDefProps.get(PN_PARENTANCESTORLEVEL, 1);
131
132
133 this.inheritFromSuperType = pathDefProps.get(PN_INHERIT, true);
134
135 }
136
137 @Override
138 public @NotNull String getPageComponentPath() {
139 return this.pageComponentPath;
140 }
141
142 @Override
143 public Pattern getPathPattern() {
144 return this.pathPattern;
145 }
146
147 @Override
148 public @NotNull Set<String> getAllowedChildren() {
149 return this.allowedChildren;
150 }
151
152 @Override
153 public @NotNull Set<String> getDeniedChildren() {
154 return this.deniedChildren;
155 }
156
157 @Override
158 public @NotNull Set<String> getAllowedParents() {
159 return this.allowedParents;
160 }
161
162 @Override
163 public int getParentAncestorLevel() {
164 return this.parentAncestorLevel;
165 }
166
167 @Override
168 public boolean isInherit() {
169 return this.inheritFromSuperType;
170 }
171
172 @Override
173 public String toString() {
174 return this.pathPattern.toString() + ", "
175 + "allowedChildren=[" + StringUtils.join(this.allowedChildren, ",") + "], "
176 + "deniedChildren=[" + StringUtils.join(this.deniedChildren, ",") + "], "
177 + "allowedParents=[" + StringUtils.join(this.allowedParents, ",") + "], "
178 + "parentAncestorLevel=" + this.parentAncestorLevel + ","
179 + "inheritFromSuperType=" + this.inheritFromSuperType;
180 }
181
182 }
183
184 }