SLING-9475 - Do not register servlets using the default methods for bundled scripts
diff --git a/src/main/java/org/apache/sling/servlets/resolver/bundle/tracker/internal/BundledScriptTracker.java b/src/main/java/org/apache/sling/servlets/resolver/bundle/tracker/internal/BundledScriptTracker.java
index 57b5ada..1b5f943 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/bundle/tracker/internal/BundledScriptTracker.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/bundle/tracker/internal/BundledScriptTracker.java
@@ -46,17 +46,19 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.servlets.resolver.bundle.tracker.BundledRenderUnit;
+import org.apache.sling.servlets.resolver.bundle.tracker.BundledRenderUnitCapability;
import org.apache.sling.servlets.resolver.bundle.tracker.BundledRenderUnitFinder;
import org.apache.sling.servlets.resolver.bundle.tracker.ResourceType;
-import org.apache.sling.servlets.resolver.bundle.tracker.BundledRenderUnitCapability;
import org.apache.sling.servlets.resolver.bundle.tracker.TypeProvider;
import org.apache.sling.servlets.resolver.internal.resource.ServletMounter;
import org.jetbrains.annotations.NotNull;
@@ -98,6 +100,8 @@
public static final String AT_SCRIPT_ENGINE = "scriptEngine";
public static final String AT_SCRIPT_EXTENSION = "scriptExtension";
public static final String AT_EXTENDS = "extends";
+ private static final String[] DEFAULT_SERVLET_METHODS = {
+ HttpConstants.METHOD_GET, HttpConstants.METHOD_HEAD };
@Reference
private BundledRenderUnitFinder bundledRenderUnitFinder;
@@ -192,6 +196,40 @@
properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, finalExecutable.getPath());
}
});
+ if (!bundledRenderUnitCapability.getResourceTypes().isEmpty() && bundledRenderUnitCapability.getSelectors().isEmpty() &&
+ StringUtils.isEmpty(bundledRenderUnitCapability.getExtension()) &&
+ StringUtils.isEmpty(bundledRenderUnitCapability.getMethod())) {
+ String scriptName = FilenameUtils.getName(executable.getPath());
+ String scriptNameNoExtension = scriptName.substring(0, scriptName.lastIndexOf('.'));
+ boolean noMatch =
+ bundledRenderUnitCapability.getResourceTypes().stream().noneMatch(resourceType -> {
+ String resourceTypePath = resourceType.toString();
+ String label;
+ int lastSlash = resourceTypePath.lastIndexOf('/');
+ if (lastSlash > -1) {
+ label = resourceTypePath.substring(lastSlash + 1);
+ } else {
+ label = resourceTypePath;
+ }
+ return label.equals(scriptNameNoExtension);
+ });
+ if (noMatch) {
+ List<String> paths = new ArrayList<>();
+ paths.add(finalExecutable.getPath());
+ bundledRenderUnitCapability.getResourceTypes().forEach(resourceType -> {
+ String resourceTypePath = resourceType.toString();
+ String label;
+ int lastSlash = resourceTypePath.lastIndexOf('/');
+ if (lastSlash > -1) {
+ label = resourceTypePath.substring(lastSlash + 1);
+ } else {
+ label = resourceTypePath;
+ }
+ paths.add(resourceTypePath + "/" + label + ".servlet");
+ });
+ properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, paths.toArray(new String[0]));
+ }
+ }
if (executable.getPath().equals(bundledRenderUnitCapability.getPath())) {
properties.put(ServletResolverConstants.SLING_SERVLET_PATHS, executable.getPath());
}
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
index 11198bc..629d25b 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
@@ -239,6 +239,11 @@
*/
private void addByType(Set<String> pathSet, ServiceReference<Servlet> ref) {
String[] types = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES));
+ String[] paths = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_PATHS));
+ boolean hasPathRegistration = true;
+ if (paths == null || paths.length == 0) {
+ hasPathRegistration = false;
+ }
if (types == null || types.length == 0) {
if (log.isDebugEnabled()) {
log.debug("addByType({}): no resource types declared",
@@ -261,17 +266,13 @@
if (methods == null || methods.length == 0) {
// SLING-512 only, set default methods if no extensions are declared
- if (extensions == null || extensions.length == 0) {
- String[] paths = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_PATHS));
- if (paths == null || paths.length == 0) {
- if (log.isDebugEnabled())
- {
- log.debug(
- "addByType({}): No methods declared, assuming GET/HEAD",
- getServiceReferenceInfo(ref));
- }
- methods = DEFAULT_SERVLET_METHODS;
+ if ((extensions == null || extensions.length == 0) && !hasPathRegistration) {
+ if (log.isDebugEnabled()) {
+ log.debug(
+ "addByType({}): No methods declared, assuming GET/HEAD",
+ getServiceReferenceInfo(ref));
}
+ methods = DEFAULT_SERVLET_METHODS;
}
} else if (methods.length == 1 && ALL_METHODS.equals(methods[0])) {
@@ -331,8 +332,8 @@
}
}
- // if neither methods nore extensions were added
- if (!pathAdded) {
+ // if neither methods nor extensions were added
+ if (!pathAdded && !hasPathRegistration) {
pathSet.add(selPath.substring(0, selPath.length() - 1)
+ SERVLET_PATH_EXTENSION);
}