SLING-7536 : Refactor implementation
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingScriptResolverImpl.java b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingScriptResolverImpl.java
index 1941cb2..61340e1 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingScriptResolverImpl.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingScriptResolverImpl.java
@@ -25,7 +25,6 @@
import org.apache.sling.api.scripting.SlingScript;
import org.apache.sling.api.scripting.SlingScriptResolver;
import org.apache.sling.api.servlets.ServletResolver;
-import org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
@@ -36,8 +35,9 @@
* servlet for a request by implementing the {@link ServletResolver} interface.
*
* The resolver uses an own session to find the scripts.
- *
+ * @deprecated The API is deprecated
*/
+@Deprecated
@Component(service = { SlingScriptResolver.class },
configurationPid = ResolverConfig.PID,
property = {
@@ -54,7 +54,7 @@
@Activate
private void activate(final ResolverConfig config) {
- this.executionPaths = AbstractResourceCollector.getExecutionPaths(config.servletresolver_paths());
+ this.executionPaths = SlingServletResolver.getExecutionPaths(config.servletresolver_paths());
}
/**
@@ -69,7 +69,7 @@
if (name.startsWith("/")) {
final String path = ResourceUtil.normalize(name);
- if ( AbstractResourceCollector.isPathAllowed(path, this.executionPaths) ) {
+ if ( SlingServletResolver.isPathAllowed(path, this.executionPaths) ) {
final Resource resource = resourceResolver.getResource(path);
if ( resource != null ) {
script = resource.adaptTo(SlingScript.class);
@@ -81,7 +81,7 @@
final String[] path = resourceResolver.getSearchPath();
for (int i = 0; script == null && i < path.length; i++) {
final String scriptPath = ResourceUtil.normalize(path[i] + name);
- if ( AbstractResourceCollector.isPathAllowed(scriptPath, this.executionPaths) ) {
+ if ( SlingServletResolver.isPathAllowed(scriptPath, this.executionPaths) ) {
final Resource resource = resourceResolver.getResource(scriptPath);
if (resource != null) {
script = resource.adaptTo(SlingScript.class);
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
index d3be338..fa2020d 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
@@ -433,7 +433,7 @@
// path of a servlet (or script)
if (scriptName.charAt(0) == '/') {
final String scriptPath = ResourceUtil.normalize(scriptName);
- if ( AbstractResourceCollector.isPathAllowed(scriptPath, this.executionPaths) ) {
+ if ( isPathAllowed(scriptPath, this.executionPaths) ) {
final Resource res = resolver.getResource(scriptPath);
servlet = this.getServlet(res);
if (servlet != null && LOGGER.isDebugEnabled()) {
@@ -642,7 +642,7 @@
this.sharedScriptResolver =
resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object)SERVICE_USER));
- this.executionPaths = AbstractResourceCollector.getExecutionPaths(config.servletresolver_paths());
+ this.executionPaths = getExecutionPaths(config.servletresolver_paths());
this.defaultExtensions = config.servletresolver_defaultExtensions();
// setup default servlet
@@ -677,4 +677,77 @@
this.sharedScriptResolver = null;
}
}
+
+ /**
+ * This method checks whether a path is allowed to be executed.
+ *
+ * @param path The path to check (must not be {@code null} or empty)
+ * @param executionPaths The path to check against
+ * @return {@code true} if the executionPaths is {@code null} or empty or if
+ * the path equals one entry or one of the executionPaths entries is
+ * a prefix to the path. Otherwise or if path is {@code null}
+ * {@code false} is returned.
+ */
+ public static boolean isPathAllowed(final String path, final String[] executionPaths) {
+ if (executionPaths == null || executionPaths.length == 0) {
+ LOGGER.debug("Accepting servlet at '{}' as there are no configured execution paths.",
+ path);
+ return true;
+ }
+
+ if (path == null || path.length() == 0) {
+ LOGGER.debug("Ignoring servlet with empty path.");
+ return false;
+ }
+
+ for (final String config : executionPaths) {
+ if (config.endsWith("/")) {
+ if (path.startsWith(config)) {
+ LOGGER.debug(
+ "Accepting servlet at '{}' as the path is prefixed with configured execution path '{}'.", path,
+ config);
+ return true;
+ }
+ } else if (path.equals(config)) {
+ LOGGER.debug(
+ "Accepting servlet at '{}' as the path equals configured execution path '{}'.", path, config);
+ return true;
+ }
+ }
+
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug(
+ "Ignoring servlet at '{}' as the path is not in the configured execution paths.", path);
+ }
+
+ return false;
+ }
+
+ /**
+ * Calculate the execution paths from the configured execution paths
+ * @param paths The configured paths
+ * @return The execution paths or {@code null} for all paths.
+ */
+ public static String[] getExecutionPaths(final String[] paths) {
+ String[] executionPaths = paths;
+ if ( executionPaths != null ) {
+ // if we find a string combination that basically allows all paths,
+ // we simply set the array to null
+ if ( executionPaths.length == 0 ) {
+ executionPaths = null;
+ } else {
+ boolean hasRoot = false;
+ for(final String path : executionPaths) {
+ if ( path == null || path.length() == 0 || path.equals("/") ) {
+ hasRoot = true;
+ break;
+ }
+ }
+ if ( hasRoot ) {
+ executionPaths = null;
+ }
+ }
+ }
+ return executionPaths;
+ }
}
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java b/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
index 79ca6dc..58ffcf0 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
@@ -47,7 +47,7 @@
import org.apache.sling.engine.impl.request.SlingRequestPathInfo;
import org.apache.sling.serviceusermapping.ServiceUserMapped;
import org.apache.sling.servlets.resolver.internal.ResolverConfig;
-import org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector;
+import org.apache.sling.servlets.resolver.internal.SlingServletResolver;
import org.apache.sling.servlets.resolver.internal.helper.ResourceCollector;
import org.apache.sling.servlets.resolver.internal.resolution.ResolutionCache;
import org.osgi.framework.Constants;
@@ -99,7 +99,7 @@
@Activate
@Modified
protected void activate(final ResolverConfig config) {
- this.executionPaths = AbstractResourceCollector.getExecutionPaths(config.servletresolver_paths());
+ this.executionPaths = SlingServletResolver.getExecutionPaths(config.servletresolver_paths());
this.defaultExtensions = config.servletresolver_defaultExtensions();
}
@@ -281,7 +281,7 @@
Resource candidateResource = iterator.next();
Servlet candidate = candidateResource.adaptTo(Servlet.class);
if (candidate != null) {
- final boolean allowed = AbstractResourceCollector.isPathAllowed(candidateResource.getPath(), this.executionPaths);
+ final boolean allowed = SlingServletResolver.isPathAllowed(candidateResource.getPath(), this.executionPaths);
pw.print("<li>");
if ( !allowed ) {
pw.print("<del>");
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
index c6c1c67..9c72f35 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
@@ -32,7 +32,6 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.SyntheticResource;
-import org.apache.sling.servlets.resolver.internal.SlingServletResolver;
/**
* The <code>ResourceCollector</code> class provides a single public method -
@@ -212,83 +211,6 @@
return s1.equals(s2);
}
- protected boolean isPathAllowed(final String path) {
- return isPathAllowed(path, this.executionPaths);
- }
-
- /**
- * This method checks whether a path is allowed to be executed.
- *
- * @param path The path to check (must not be {@code null} or empty)
- * @param executionPaths The path to check against
- * @return {@code true} if the executionPaths is {@code null} or empty or if
- * the path equals one entry or one of the executionPaths entries is
- * a prefix to the path. Otherwise or if path is {@code null}
- * {@code false} is returned.
- */
- public static boolean isPathAllowed(final String path, final String[] executionPaths) {
- if (executionPaths == null || executionPaths.length == 0) {
- SlingServletResolver.LOGGER.debug("Accepting servlet at '{}' as there are no configured execution paths.",
- path);
- return true;
- }
-
- if (path == null || path.length() == 0) {
- SlingServletResolver.LOGGER.debug("Ignoring servlet with empty path.");
- return false;
- }
-
- for (final String config : executionPaths) {
- if (config.endsWith("/")) {
- if (path.startsWith(config)) {
- SlingServletResolver.LOGGER.debug(
- "Accepting servlet at '{}' as the path is prefixed with configured execution path '{}'.", path,
- config);
- return true;
- }
- } else if (path.equals(config)) {
- SlingServletResolver.LOGGER.debug(
- "Accepting servlet at '{}' as the path equals configured execution path '{}'.", path, config);
- return true;
- }
- }
-
- if (SlingServletResolver.LOGGER.isDebugEnabled()) {
- SlingServletResolver.LOGGER.debug(
- "Ignoring servlet at '{}' as the path is not in the configured execution paths.", path);
- }
-
- return false;
- }
-
- /**
- * Calculate the execution paths from the configured execution paths
- * @param paths The configured paths
- * @return The execution paths or {@code null} for all paths.
- */
- public static String[] getExecutionPaths(final String[] paths) {
- String[] executionPaths = paths;
- if ( executionPaths != null ) {
- // if we find a string combination that basically allows all paths,
- // we simply set the array to null
- if ( executionPaths.length == 0 ) {
- executionPaths = null;
- } else {
- boolean hasRoot = false;
- for(final String path : executionPaths) {
- if ( path == null || path.length() == 0 || path.equals("/") ) {
- hasRoot = true;
- break;
- }
- }
- if ( hasRoot ) {
- executionPaths = null;
- }
- }
- }
- return executionPaths;
- }
-
private String getScriptExtension(String scriptName) {
int lastIndexOf = scriptName.lastIndexOf('.');
if (lastIndexOf > -1 && lastIndexOf < scriptName.length() - 1) {
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
index 10b2c64..0ed8a9b 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
@@ -25,6 +25,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.ServletResolverConstants;
+import org.apache.sling.servlets.resolver.internal.SlingServletResolver;
/**
* The <code>ResourceCollector</code> class provides a single public method -
@@ -90,7 +91,7 @@
// if extension is set, we first check for an exact script match
if ( this.extension != null ) {
final String path = ResourceUtil.normalize(location.getPath() + '/' + this.scriptName);
- if ( this.isPathAllowed(path) ) {
+ if ( SlingServletResolver.isPathAllowed(path, this.executionPaths) ) {
final Resource current = resolver.getResource(path);
if ( current != null ) {
this.addWeightedResource(resources, current, 0, WeightedResource.WEIGHT_EXTENSION);
@@ -113,7 +114,7 @@
while (children.hasNext()) {
final Resource child = children.next();
- if ( !this.isPathAllowed(child.getPath()) ) {
+ if ( !SlingServletResolver.isPathAllowed(child.getPath(), this.executionPaths) ) {
continue;
}
final String currentScriptName = child.getName();
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
index 20eacc2..689e249 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
@@ -28,6 +28,7 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.ServletResolverConstants;
+import org.apache.sling.servlets.resolver.internal.SlingServletResolver;
import org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory;
/**
@@ -201,7 +202,7 @@
while (children.hasNext()) {
Resource child = children.next();
- if (!this.isPathAllowed(child.getPath())) {
+ if (!SlingServletResolver.isPathAllowed(child.getPath(), this.executionPaths)) {
continue;
}
String scriptName = child.getName();
@@ -337,7 +338,7 @@
final Resource location) {
final String path = location.getPath()
+ ServletResourceProviderFactory.SERVLET_PATH_EXTENSION;
- if (this.isPathAllowed(path)) {
+ if (SlingServletResolver.isPathAllowed(path, this.executionPaths)) {
final Resource servlet = location.getResourceResolver().getResource(
path);
if (servlet != null) {