PageResourceProviderFactory.java
3.83 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.ApiResourceProvider
* com.adobe.granite.rest.ApiResourceProviderFactory
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
*/
package com.adobe.cq.rest.content.impl;
import com.adobe.cq.rest.content.impl.PageResourceProvider;
import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.ApiResourceProviderFactory;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
@Component
@Service
@Properties(value={@Property(name="provider.type", value={"content"})})
public class PageResourceProviderFactory
implements ApiResourceProviderFactory {
@Property(label="Included", description="Paths exposed by this ResourceProvider under /api/content/<suffix> in the form of <path>=<api suffix>.", value={"/content=sites", "/etc/commerce/products=products"})
private static final String CFG_INCLUDES = "include.paths";
@Property(label="Excluded", description="Paths not exposed by this ResourceProvider.", value={"/content/catalogs", "/content/communities", "/content/dam", "/content/launches", "/content/mac", "/content/publications", "/content/usergenerated", "/etc/commerce/products/csvimporter"})
private static final String CFG_EXCLUDES = "exclude.paths";
private Map<String, String> inclusionMapping = new HashMap<String, String>();
private String[] excludePaths;
public String getContextPath() {
return "content";
}
public ApiResourceProvider getResourceProvider(String rootContextPath) {
return new PageResourceProvider(rootContextPath, this.inclusionMapping, this.excludePaths);
}
@Activate
protected void activate(ComponentContext context) {
Dictionary dict = context.getProperties();
this.inclusionMapping = PropertiesUtil.toMap(dict.get("include.paths"), (String[])new String[0]);
this.excludePaths = PropertiesUtil.toStringArray(dict.get("exclude.paths"), (String[])new String[0]);
}
public String unmap(String apiContextPath, String apiPath) {
String resourcePath = apiPath.replaceFirst(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(apiContextPath + "/" + suffix, root);
}
public String map(String apiContextPath, 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, apiContextPath + "/" + suffix);
}
}