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 final class ResourceParsysConfigProvider {
46
47 private static final String NN_PATHS = "paths";
48 private static final String PN_PATH = "path";
49 private static final String PN_PATTERN = "pattern";
50 private static final String PN_ALLOWEDCHILDREN = "allowedChildren";
51 private static final String PN_DENIEDDCHILDREN = "deniedChildren";
52 private static final String PN_ALLOWEDPARENTS = "allowedParents";
53 private static final String PN_PARENTANCESTORLEVEL = "parentAncestorLevel";
54 private static final String PN_INHERIT = "inherit";
55
56 private final List<ParsysConfig> pathDefs;
57
58
59
60
61 ResourceParsysConfigProvider(Resource pageComponentResource) {
62 this.pathDefs = getPathDefs(pageComponentResource);
63 }
64
65 private static List<ParsysConfig> getPathDefs(Resource pageComponentResource) {
66 List<ParsysConfig> pathDefs = new ArrayList<>();
67
68 ResourceResolver resourceResolver = pageComponentResource.getResourceResolver();
69 Resource pathsResource = resourceResolver.getResource(pageComponentResource, "./" + NN_PARSYS_CONFIG + "/" + NN_PATHS);
70 if (pathsResource != null) {
71 Iterator<Resource> pathDefResources = resourceResolver.listChildren(pathsResource);
72 while (pathDefResources.hasNext()) {
73 Resource pathDefResource = pathDefResources.next();
74 pathDefs.add(new PathDef(pathDefResource, pageComponentResource.getResourceType()));
75 }
76 }
77
78 return pathDefs;
79 }
80
81
82
83
84 public List<ParsysConfig> getPathDefs() {
85 return this.pathDefs;
86 }
87
88
89
90
91 private static class PathDef implements ParsysConfig {
92
93 private final String pageComponentPath;
94 private final Pattern pathPattern;
95 private final Set<String> allowedChildren;
96 private final Set<String> deniedChildren;
97 private final Set<String> allowedParents;
98 private final int parentAncestorLevel;
99 private final boolean inheritFromSuperType;
100
101
102
103
104
105 PathDef(Resource pathDefResource, String pageComponentPath) {
106 this.pageComponentPath = pageComponentPath;
107 ValueMap pathDefProps = pathDefResource.getValueMap();
108
109
110 String path = pathDefProps.get(PN_PATH, String.class);
111 String patternString = pathDefProps.get(PN_PATTERN, String.class);
112 if (StringUtils.isNotEmpty(patternString)) {
113 this.pathPattern = Pattern.compile(patternString);
114 }
115 else if (StringUtils.isNotBlank(path)) {
116 this.pathPattern = Pattern.compile("^" + Pattern.quote(path) + "$");
117 }
118 else {
119 String name = Text.getName(pathDefResource.getPath());
120 this.pathPattern = Pattern.compile("^" + Pattern.quote(JcrConstants.JCR_CONTENT + "/" + name) + "$");
121 }
122
123
124 this.allowedChildren = Set.of(pathDefProps.get(PN_ALLOWEDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
125 this.deniedChildren = Set.of(pathDefProps.get(PN_DENIEDDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
126 this.allowedParents = Set.of(pathDefProps.get(PN_ALLOWEDPARENTS, ArrayUtils.EMPTY_STRING_ARRAY));
127
128
129 this.parentAncestorLevel = pathDefProps.get(PN_PARENTANCESTORLEVEL, 1);
130
131
132 this.inheritFromSuperType = pathDefProps.get(PN_INHERIT, true);
133
134 }
135
136 @Override
137 public @NotNull String getPageComponentPath() {
138 return this.pageComponentPath;
139 }
140
141 @Override
142 public Pattern getPathPattern() {
143 return this.pathPattern;
144 }
145
146 @Override
147 public @NotNull Set<String> getAllowedChildren() {
148 return this.allowedChildren;
149 }
150
151 @Override
152 public @NotNull Set<String> getDeniedChildren() {
153 return this.deniedChildren;
154 }
155
156 @Override
157 public @NotNull Set<String> getAllowedParents() {
158 return this.allowedParents;
159 }
160
161 @Override
162 public int getParentAncestorLevel() {
163 return this.parentAncestorLevel;
164 }
165
166 @Override
167 public boolean isInherit() {
168 return this.inheritFromSuperType;
169 }
170
171 @Override
172 public String toString() {
173 return this.pathPattern.toString() + ", "
174 + "allowedChildren=[" + StringUtils.join(this.allowedChildren, ",") + "], "
175 + "deniedChildren=[" + StringUtils.join(this.deniedChildren, ",") + "], "
176 + "allowedParents=[" + StringUtils.join(this.allowedParents, ",") + "], "
177 + "parentAncestorLevel=" + this.parentAncestorLevel + ","
178 + "inheritFromSuperType=" + this.inheritFromSuperType;
179 }
180
181 }
182
183 }