PageResourceProvider.java
6.23 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.ApiResourceProvider
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.rest.content.impl;
import com.adobe.cq.rest.content.impl.PageResourceFactory;
import com.adobe.granite.rest.ApiResourceProvider;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PageResourceProvider
implements ApiResourceProvider {
private static final Logger log = LoggerFactory.getLogger(PageResourceProvider.class);
public static final String TYPE = "content";
public static final String CONTENT_ROOT = "/content";
private PageResourceFactory resourceFactory;
private String apiContextPath;
private Map<String, String> inclusionMapping;
private String[] excludePaths;
public PageResourceProvider(String contextPath, Map<String, String> inclusionMapping, String[] excludePaths) {
this.inclusionMapping = inclusionMapping;
this.excludePaths = excludePaths;
this.apiContextPath = contextPath + "/" + "content";
this.resourceFactory = new PageResourceFactory(this.apiContextPath);
}
public Resource getResource(ResourceResolver resolver, String path) {
Resource resource = null;
if (!StringUtils.isEmpty((String)path)) {
String resourcePath = this.unmap(this.apiContextPath + path);
if (resourcePath != null) {
resource = resolver.getResource(resourcePath);
}
} else {
resource = resolver.getResource("/content");
}
if (resource == null) {
return null;
}
return this.resourceFactory.createResource(resource, this.apiContextPath + path);
}
public Iterator<Resource> listChildren(Resource parent) {
log.debug("Listing children for resource {}.", (Object)parent);
String parentPath = parent.getPath();
if (!StringUtils.isEmpty((String)parentPath)) {
ResourceResolver resolver = parent.getResourceResolver();
if (this.apiContextPath.equals(parentPath)) {
return this.getRootChildren(resolver);
}
String resourcePath = this.unmap(parentPath);
Resource parentResource = resolver.getResource(resourcePath);
if (parentResource == null) {
log.warn("Parent resource {} not found", (Object)resourcePath);
return null;
}
return this.getResourceChildren(parentResource);
}
return null;
}
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
throw new UnsupportedOperationException("Operation not supported for resource");
}
public void delete(ResourceResolver resolver, String path) throws PersistenceException {
throw new UnsupportedOperationException("Operation not supported for resource");
}
private Iterator<Resource> getRootChildren(ResourceResolver resolver) {
LinkedList<Resource> children = new LinkedList<Resource>();
for (Map.Entry<String, String> e : this.inclusionMapping.entrySet()) {
String root = e.getKey();
Resource parent = resolver.getResource(root);
if (parent == null) {
log.warn("Resource for path {} not found.", (Object)root);
continue;
}
children.addAll(this.getChildren(parent));
}
return children.iterator();
}
private Iterator<Resource> getResourceChildren(Resource parent) {
return this.getChildren(parent).iterator();
}
private List<Resource> getChildren(Resource parent) {
LinkedList<Resource> children = new LinkedList<Resource>();
Iterator it = parent.listChildren();
while (it.hasNext()) {
Resource childRes;
String apiPath;
Resource child = (Resource)it.next();
String[] includePaths = this.inclusionMapping.keySet().toArray(new String[0]);
if (!this.matches(includePaths, child.getPath()) || this.matches(this.excludePaths, child.getPath()) || (apiPath = this.map(child.getPath())) == null || (childRes = this.resourceFactory.createResource(child, apiPath)) == null) continue;
children.add(childRes);
}
return children;
}
private boolean matches(String[] paths, String path) {
for (String p : paths) {
if (!path.startsWith(p)) continue;
return true;
}
return false;
}
String unmap(String apiPath) {
String resourcePath = apiPath.replaceFirst(this.apiContextPath, "").replaceFirst("/", "");
String root = "/content";
String suffix = null;
for (Map.Entry<String, String> entry : this.inclusionMapping.entrySet()) {
String path = entry.getKey();
String map = entry.getValue();
if (!resourcePath.startsWith(map)) continue;
root = path;
suffix = map;
}
if (suffix == null) {
return null;
}
return apiPath.replaceFirst(this.apiContextPath + "/" + suffix, root);
}
String map(String resourcePath) {
String root = "/content";
String suffix = null;
for (Map.Entry<String, String> entry : this.inclusionMapping.entrySet()) {
String path = entry.getKey();
String map = entry.getValue();
if (!resourcePath.startsWith(path)) continue;
root = path;
suffix = map;
}
if (suffix == null) {
return null;
}
return resourcePath.replaceFirst(root, this.apiContextPath + "/" + suffix);
}
}