Skip to content

Commit 017a5ec

Browse files
committed
SLING-9306 - Provide a path capability for scripts which are not part of a resource type
* scripts which are not part of a resource type will generate a path capability * switched to using the ServletResolverConstants for generating the capabilities' attributes
1 parent 5d8445c commit 017a5ec

File tree

14 files changed

+694
-328
lines changed

14 files changed

+694
-328
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@
195195
<scope>compile</scope>
196196
<version>0.1.1-SNAPSHOT</version>
197197
</dependency>
198+
<dependency>
199+
<groupId>org.apache.sling</groupId>
200+
<artifactId>org.apache.sling.api</artifactId>
201+
<version>2.22.0</version>
202+
<scope>compile</scope>
203+
</dependency>
198204

199205
<dependency>
200206
<groupId>org.osgi</groupId>

src/main/java/org/apache/sling/scriptingbundle/maven/plugin/Capabilities.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
~ Licensed to the Apache Software Foundation (ASF) under one
3+
~ or more contributor license agreements. See the NOTICE file
4+
~ distributed with this work for additional information
5+
~ regarding copyright ownership. The ASF licenses this file
6+
~ to you under the Apache License, Version 2.0 (the
7+
~ "License"); you may not use this file except in compliance
8+
~ with the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing,
13+
~ software distributed under the License is distributed on an
14+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
~ KIND, either express or implied. See the License for the
16+
~ specific language governing permissions and limitations
17+
~ under the License.
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
19+
package org.apache.sling.scriptingbundle.maven.plugin;
20+
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.HashSet;
26+
import java.util.LinkedHashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
31+
import org.apache.commons.lang3.StringUtils;
32+
import org.apache.maven.plugin.logging.Log;
33+
import org.apache.sling.scripting.bundle.tracker.ResourceType;
34+
import org.apache.sling.scriptingbundle.maven.plugin.capability.ProvidedResourceTypeCapability;
35+
import org.apache.sling.scriptingbundle.maven.plugin.capability.RequiredResourceTypeCapability;
36+
import org.jetbrains.annotations.NotNull;
37+
import org.osgi.framework.VersionRange;
38+
39+
public class FileProcessor {
40+
41+
private final Log log;
42+
private final Set<String> searchPaths;
43+
private final Map<String, String> scriptEngineMappings;
44+
45+
FileProcessor(Log log, Set<String> searchPaths, Map<String, String> scriptEngineMappings) {
46+
this.log = log;
47+
this.searchPaths = searchPaths;
48+
this.scriptEngineMappings = scriptEngineMappings;
49+
}
50+
51+
void processExtendsFile(@NotNull ResourceType resourceType, @NotNull Path extendsFile,
52+
@NotNull Set<ProvidedResourceTypeCapability> providedCapabilities,
53+
@NotNull Set<RequiredResourceTypeCapability> requiredCapabilities) {
54+
try {
55+
List<String> extendResources = Files.readAllLines(extendsFile, StandardCharsets.UTF_8);
56+
if (extendResources.size() == 1) {
57+
String extend = extendResources.get(0);
58+
if (StringUtils.isNotEmpty(extend)) {
59+
String[] extendParts = extend.split(";");
60+
String extendedResourceType = extendParts[0];
61+
String extendedResourceTypeVersion = extendParts.length > 1 ? extendParts[1] : null;
62+
providedCapabilities.add(
63+
ProvidedResourceTypeCapability.builder()
64+
.withResourceTypes(processSearchPathResourceTypes(resourceType))
65+
.withVersion(resourceType.getVersion())
66+
.withExtendsResourceType(extendedResourceType)
67+
.build());
68+
RequiredResourceTypeCapability.Builder requiredBuilder =
69+
RequiredResourceTypeCapability.builder().withResourceType(extendedResourceType);
70+
extractVersionRange(extendsFile, requiredBuilder, extendedResourceTypeVersion);
71+
requiredCapabilities.add(requiredBuilder.build());
72+
}
73+
}
74+
} catch (IOException e) {
75+
log.error(String.format("Unable to read file %s.", extendsFile.toString()), e);
76+
}
77+
}
78+
79+
void processRequiresFile(@NotNull Path requiresFile,
80+
@NotNull Set<RequiredResourceTypeCapability> requiredCapabilities) {
81+
try {
82+
List<String> requiredResourceTypes = Files.readAllLines(requiresFile, StandardCharsets.UTF_8);
83+
for (String requiredResourceType : requiredResourceTypes) {
84+
if (StringUtils.isNotEmpty(requiredResourceType)) {
85+
String[] requireParts = requiredResourceType.split(";");
86+
String resourceType = requireParts[0];
87+
String version = requireParts.length > 1 ? requireParts[1] : null;
88+
RequiredResourceTypeCapability.Builder requiredBuilder =
89+
RequiredResourceTypeCapability.builder().withResourceType(resourceType);
90+
extractVersionRange(requiresFile, requiredBuilder, version);
91+
requiredCapabilities.add(requiredBuilder.build());
92+
}
93+
}
94+
} catch (IOException e) {
95+
log.error(String.format("Unable to read file %s.", requiresFile.toString()), e);
96+
}
97+
}
98+
99+
void processScriptFile(@NotNull Path resourceTypeDirectory, @NotNull Path scriptPath,
100+
@NotNull ResourceType resourceType, @NotNull Set<ProvidedResourceTypeCapability> providedCapabilities) {
101+
Path scriptFile = scriptPath.getFileName();
102+
if (scriptFile != null) {
103+
Path relativeResourceTypeFolder = resourceTypeDirectory.relativize(scriptPath);
104+
int pathSegments = relativeResourceTypeFolder.getNameCount();
105+
LinkedHashSet<String> selectors = new LinkedHashSet<>();
106+
if (pathSegments > 1) {
107+
for (int i = 0; i < pathSegments - 1; i++) {
108+
selectors.add(relativeResourceTypeFolder.getName(i).toString());
109+
}
110+
}
111+
String scriptFileName = scriptFile.toString();
112+
Script script = Script.parseScript(scriptFileName);
113+
if (script != null) {
114+
String scriptEngine = scriptEngineMappings.get(script.getScriptExtension());
115+
if (scriptEngine != null) {
116+
String scriptName = script.getName();
117+
Set<String> searchPathProcessesResourceTypes = processSearchPathResourceTypes(resourceType);
118+
if (!resourceType.getResourceLabel().equals(scriptName)) {
119+
if (scriptFileName.split("\\.").length == 2 && scriptName != null &&
120+
scriptName.equals(script.getRequestExtension())) {
121+
LinkedHashSet<String> capSelectors = new LinkedHashSet<>(selectors);
122+
capSelectors.add(scriptName);
123+
providedCapabilities.add(
124+
ProvidedResourceTypeCapability.builder()
125+
.withResourceTypes(searchPathProcessesResourceTypes)
126+
.withVersion(resourceType.getVersion())
127+
.withSelectors(capSelectors)
128+
.withRequestMethod(script.getRequestMethod())
129+
.withScriptEngine(scriptEngine)
130+
.withScriptExtension(script.getScriptExtension())
131+
.build()
132+
);
133+
} else {
134+
if (scriptName != null && !MetadataMojo.METHODS.contains(scriptName)) {
135+
selectors.add(script.getName());
136+
}
137+
}
138+
}
139+
providedCapabilities.add(
140+
ProvidedResourceTypeCapability.builder()
141+
.withResourceTypes(searchPathProcessesResourceTypes)
142+
.withVersion(resourceType.getVersion())
143+
.withSelectors(selectors)
144+
.withRequestExtension(script.getRequestExtension())
145+
.withRequestMethod(script.getRequestMethod())
146+
.withScriptEngine(scriptEngine)
147+
.withScriptExtension(script.getScriptExtension())
148+
.build()
149+
);
150+
} else {
151+
log.warn(String.format("Cannot find a script engine mapping for script %s.", scriptPath.toString()));
152+
}
153+
} else {
154+
log.warn(String.format("File %s does not denote a script.", scriptPath.toString()));
155+
}
156+
} else {
157+
log.warn(String.format("Skipping path %s since it has 0 elements.", scriptPath.toString()));
158+
}
159+
}
160+
161+
private Set<String> processSearchPathResourceTypes(@NotNull ResourceType resourceType) {
162+
Set<String> resourceTypes = new HashSet<>();
163+
for (String searchPath : searchPaths) {
164+
if (!searchPath.endsWith("/")) {
165+
searchPath = searchPath + "/";
166+
}
167+
String absoluteType = "/" + resourceType.getType();
168+
if (absoluteType.startsWith(searchPath)) {
169+
resourceTypes.add(absoluteType);
170+
resourceTypes.add(absoluteType.substring(searchPath.length()));
171+
}
172+
}
173+
if (resourceTypes.isEmpty()) {
174+
resourceTypes.add(resourceType.getType());
175+
}
176+
return resourceTypes;
177+
}
178+
179+
private void extractVersionRange(@NotNull Path requiresFile, @NotNull RequiredResourceTypeCapability.Builder requiredBuilder,
180+
String version) {
181+
try {
182+
if (version != null) {
183+
requiredBuilder.withVersionRange(VersionRange.valueOf(version.substring(version.indexOf('=') + 1)));
184+
}
185+
} catch (IllegalArgumentException ignored) {
186+
log.warn(String.format("Invalid version range format %s in file %s.", version, requiresFile.toString()));
187+
}
188+
}
189+
}

0 commit comments

Comments
 (0)