AllowedComponentList.java
7.78 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.sightly.WCMUsePojo
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentManager
* com.day.cq.wcm.api.policies.ContentPolicy
* com.day.cq.wcm.api.policies.ContentPolicyMapping
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.wcm.foundation;
import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentManager;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyMapping;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
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.ValueMap;
public class AllowedComponentList
extends WCMUsePojo {
private static final String PLACEHOLDER_COMPONENT_PATH = "wcm/foundation/components/parsys/placeholder";
private static final String PN_POLICY = "cq:policy";
private static final String PN_COMPONENTS = "components";
private static final String COMPONENT_GROUP_PREFIX = "group:";
public static final String STRUCTURE_JCR_CONTENT = "/structure/jcr:content/";
public static final String POLICIES_JCR_CONTENT = "/policies/jcr:content/";
private Collection<Component> components;
private String[] componentPaths = null;
public void activate() throws Exception {
ComponentManager componentManager = (ComponentManager)this.getResourceResolver().adaptTo(ComponentManager.class);
this.components = componentManager.getComponents();
}
public String getTitle() {
return (this.getComponentPathList().length > 0 ? "Allowed Components" : "No allowed component") + " for " + this.getComponent().getTitle();
}
public boolean isApplicable() {
Template template;
ValueMap valueMap;
Page page = this.getCurrentPage();
if (page != null && (template = page.getTemplate()) != null && template.hasStructureSupport() && (valueMap = this.getResource().getValueMap()) != null && valueMap.containsKey((Object)"editable")) {
return (Boolean)valueMap.get("editable", Boolean.class);
}
return false;
}
private String[] getComponentPathList() {
String resourcePath;
String policyMappingPath;
Resource resource;
if (this.componentPaths != null) {
return this.componentPaths;
}
ResourceResolver resourceResolver = this.getResourceResolver();
Resource policyMappingResource = resourceResolver.getResource(policyMappingPath = (resourcePath = (resource = this.getResource()).getPath()).replace("/structure/jcr:content/", "/policies/jcr:content/"));
if (policyMappingResource != null) {
ContentPolicyMapping contentPolicyMapping;
ValueMap policyValueMap;
ContentPolicy policy;
ValueMap policyMappingValueMap = policyMappingResource.getValueMap();
if (!policyMappingValueMap.containsKey((Object)"cq:policy")) {
for (Resource parent = policyMappingResource.getParent(); !policyMappingValueMap.containsKey((Object)"cq:policy") && parent != null; parent = parent.getParent()) {
ValueMap parentMappingVM = parent.getValueMap();
if (!parentMappingVM.containsKey((Object)"cq:policy")) {
continue;
}
policyMappingResource = parent;
break;
}
}
if ((contentPolicyMapping = (ContentPolicyMapping)policyMappingResource.adaptTo(ContentPolicyMapping.class)) != null && (policy = contentPolicyMapping.getPolicy()) != null && (policyValueMap = policy.getProperties()) != null && policyValueMap.containsKey((Object)"components")) {
this.componentPaths = (String[])policyValueMap.get("components", String[].class);
return this.componentPaths;
}
}
this.componentPaths = new String[0];
return this.componentPaths;
}
private String getRelativePath(Resource resource) {
if (resource == null) {
return null;
}
String[] searchPaths = resource.getResourceResolver().getSearchPath();
for (int i = 0; i < searchPaths.length; ++i) {
String searchPath = searchPaths[i];
if (!resource.getPath().startsWith(searchPath)) continue;
String resourcePath = resource.getPath();
return resourcePath.substring(searchPath.length(), resourcePath.length());
}
return null;
}
public List getComponents() throws UnsupportedEncodingException {
ArrayList<ComponentMapping> resources = new ArrayList<ComponentMapping>();
String[] componentPaths = this.getComponentPathList();
ArrayList<Component> sortedComponents = new ArrayList<Component>();
if (componentPaths != null && componentPaths.length > 0) {
String resourcePath = this.getResource().getPath();
for (int i = 0; i < componentPaths.length; ++i) {
String compPath = componentPaths[i];
compPath = compPath != null && compPath.startsWith("group:") ? compPath.substring("group:".length(), compPath.length()) : compPath;
for (Component component : this.components) {
String componentGroup = component.getComponentGroup();
if (!component.getPath().endsWith(compPath) && (StringUtils.isEmpty((String)componentGroup) || !componentGroup.equals(compPath))) continue;
sortedComponents.add(component);
}
}
Collections.sort(sortedComponents, new ComponentComparator());
for (Component component : sortedComponents) {
Resource componentResource = (Resource)component.adaptTo(Resource.class);
String key = resourcePath + "/" + this.getRelativePath(componentResource);
this.getRequest().setAttribute(key, (Object)componentResource);
resources.add(new ComponentMapping(key, "wcm/foundation/components/parsys/placeholder", "aem-AllowedComponent--component"));
}
}
return resources;
}
public String getCssClass() {
return this.isApplicable() ? "aem-AllowedComponent--list" : "";
}
public static class ComponentMapping {
private String path;
private String resourceType;
private String cssClass;
public ComponentMapping(String path, String resourceType, String cssClass) {
this.path = path;
this.resourceType = resourceType;
this.cssClass = cssClass;
}
public String getPath() {
return this.path;
}
public String getResourceType() {
return this.resourceType;
}
public String getCssClass() {
return this.cssClass;
}
}
private class ComponentComparator
implements Comparator<Component> {
private ComponentComparator() {
}
@Override
public int compare(Component comp1, Component comp2) {
return comp1.getTitle().compareTo(comp2.getTitle());
}
}
}