ApiEndpointResourceProvider.java
5.72 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.ModifyingResourceProvider
* 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.granite.rest.impl;
import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.ApiResourceProviderFactory;
import com.adobe.granite.rest.impl.ApiEndpointResource;
import com.adobe.granite.rest.impl.ApiEndpointResourceProviderFactory;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifyingResourceProvider;
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 ApiEndpointResourceProvider
implements ModifyingResourceProvider {
private static final Logger log = LoggerFactory.getLogger(ApiEndpointResourceProvider.class);
private String apiContextPath = "";
private ApiEndpointResourceProviderFactory apiEndpointProviderFactory;
public ApiEndpointResourceProvider(ApiEndpointResourceProviderFactory apiEndpointProviderFactory) {
this.apiEndpointProviderFactory = apiEndpointProviderFactory;
this.apiContextPath = apiEndpointProviderFactory.getRootContextPath();
}
public Resource getResource(ResourceResolver resolver, String path) {
return this.getResource(resolver, null, path);
}
public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
if (this.apiContextPath.equals(path)) {
try {
Collection<ApiResourceProviderFactory> providerFactories = this.apiEndpointProviderFactory.getApiProviderFactories();
return new ApiEndpointResource(resolver, path, providerFactories);
}
catch (Exception e) {
log.error("Unable to return ApiEndpointResource", (Throwable)e);
return null;
}
}
String providerContextPath = this.getResourceProviderContextPath(path);
String resourcePath = this.getResourcePath(path);
ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
if (resourceProvider != null) {
return resourceProvider.getResource(resolver, resourcePath);
}
return null;
}
public Iterator<Resource> listChildren(Resource parent) {
String path = parent.getPath();
String providerContextPath = this.getResourceProviderContextPath(path);
ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
if (resourceProvider != null) {
return resourceProvider.listChildren(parent);
}
return null;
}
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
String providerContextPath = this.getResourceProviderContextPath(path);
String resourcePath = this.getResourcePath(path);
ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
if (resourceProvider != null) {
return resourceProvider.create(resolver, resourcePath, properties);
}
return null;
}
public void delete(ResourceResolver resolver, String path) throws PersistenceException {
String providerContextPath = this.getResourceProviderContextPath(path);
String resourcePath = this.getResourcePath(path);
ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
if (resourceProvider != null) {
resourceProvider.delete(resolver, resourcePath);
}
}
public void revert(ResourceResolver resolver) {
}
public void commit(ResourceResolver resolver) throws PersistenceException {
}
public boolean hasChanges(ResourceResolver resolver) {
return false;
}
private ApiResourceProvider getResourceProviderByContext(String contextPath) {
if (contextPath == null) {
return null;
}
Collection<ApiResourceProviderFactory> providerFactories = this.apiEndpointProviderFactory.getApiProviderFactories();
for (ApiResourceProviderFactory providerFactory : providerFactories) {
if (!contextPath.equals(providerFactory.getContextPath())) continue;
return providerFactory.getResourceProvider(this.apiContextPath);
}
return null;
}
private String getResourcePath(String path) {
String resourcePath = "";
if (StringUtils.countMatches((String)path, (String)"/") > 2) {
path = StringUtils.removeStart((String)path, (String)this.apiContextPath);
path = StringUtils.removeStart((String)path, (String)"/");
resourcePath = path = path.substring(path.indexOf("/"));
}
return resourcePath;
}
private String getResourceProviderContextPath(String path) {
String providerType = null;
if (path.matches("^/(.*?)/(.*?)(/.*)?")) {
path = StringUtils.removeStart((String)path, (String)this.apiContextPath);
int idx = (path = StringUtils.removeStart((String)path, (String)"/")).indexOf("/");
providerType = idx > 0 ? path.substring(0, idx) : path;
}
return providerType;
}
}