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
new file mode 100644
index 0000000..5677aa4
--- /dev/null
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/SlingScriptResolverImpl.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.servlets.resolver.internal;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceUtil;
+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;
+
+/**
+ * The <code>SlingServletResolver</code> has two functions: It resolves scripts
+ * by implementing the {@link SlingScriptResolver} interface and it resolves a
+ * servlet for a request by implementing the {@link ServletResolver} interface.
+ *
+ * The resolver uses an own session to find the scripts.
+ *
+ */
+@Component(service = { SlingScriptResolver.class },
+ configurationPid = SlingServletResolver.Config.PID,
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Apache Sling Script Resolver",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+ })
+public class SlingScriptResolverImpl
+ implements SlingScriptResolver {
+
+ /**
+ * The allowed execution paths.
+ */
+ private String[] executionPaths;
+
+ @Activate
+ private void activate(final SlingServletResolver.Config config) {
+ this.executionPaths = AbstractResourceCollector.getExecutionPaths(config.servletresolver_paths());
+ }
+
+ /**
+ * @see org.apache.sling.api.scripting.SlingScriptResolver#findScript(org.apache.sling.api.resource.ResourceResolver, java.lang.String)
+ */
+ @Override
+ public SlingScript findScript(final ResourceResolver resourceResolver, final String name)
+ throws SlingException {
+
+ // is the path absolute
+ SlingScript script = null;
+ if (name.startsWith("/")) {
+
+ final String path = ResourceUtil.normalize(name);
+ if ( AbstractResourceCollector.isPathAllowed(path, this.executionPaths) ) {
+ final Resource resource = resourceResolver.getResource(path);
+ if ( resource != null ) {
+ script = resource.adaptTo(SlingScript.class);
+ }
+ }
+ } else {
+
+ // relative script resolution against search path
+ 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) ) {
+ final Resource resource = resourceResolver.getResource(scriptPath);
+ if (resource != null) {
+ script = resource.adaptTo(SlingScript.class);
+ }
+ }
+ }
+
+ }
+
+ // log result
+ if (script != null) {
+ SlingServletResolver.LOGGER.debug("findScript: Using script {} for {}", script.getScriptResource().getPath(), name);
+ } else {
+ SlingServletResolver.LOGGER.info("findScript: No script {} found in path", name);
+ }
+
+ // and finally return the script (or null)
+ return script;
+ }
+}
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 f29f08c..e78ce68 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
@@ -56,7 +56,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingConstants;
-import org.apache.sling.api.SlingException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
@@ -75,7 +74,6 @@
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.apache.sling.api.scripting.SlingScript;
-import org.apache.sling.api.scripting.SlingScriptResolver;
import org.apache.sling.api.servlets.OptingServlet;
import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.api.servlets.ServletResolverConstants;
@@ -111,15 +109,14 @@
import org.slf4j.LoggerFactory;
/**
- * The <code>SlingServletResolver</code> has two functions: It resolves scripts
- * by implementing the {@link SlingScriptResolver} interface and it resolves a
+ * The <code>SlingServletResolver</code> resolves a
* servlet for a request by implementing the {@link ServletResolver} interface.
*
* The resolver uses an own session to find the scripts.
*
*/
-@Component(name = "org.apache.sling.servlets.resolver.SlingServletResolver",
- service = { ServletResolver.class, SlingScriptResolver.class, ErrorHandler.class, SlingRequestListener.class },
+@Component(name = SlingServletResolver.Config.PID,
+ service = { ServletResolver.class,ErrorHandler.class, SlingRequestListener.class },
property = {
Constants.SERVICE_DESCRIPTION + "=Apache Sling Servlet Resolver and Error Handler",
Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
@@ -127,7 +124,6 @@
@Designate(ocd = SlingServletResolver.Config.class)
public class SlingServletResolver
implements ServletResolver,
- SlingScriptResolver,
SlingRequestListener,
ErrorHandler,
EventHandler,
@@ -142,6 +138,9 @@
"servlets and scripts as is used to resolve request processing servlets and "+
"scripts.")
public @interface Config {
+
+ String PID = "org.apache.sling.servlets.resolver.SlingServletResolver";
+
/**
* The default servlet root is the first search path (which is usually /apps)
*/
@@ -372,51 +371,6 @@
// ---------- ScriptResolver interface ------------------------------------
- /**
- * @see org.apache.sling.api.scripting.SlingScriptResolver#findScript(org.apache.sling.api.resource.ResourceResolver, java.lang.String)
- */
- @Override
- public SlingScript findScript(final ResourceResolver resourceResolver, final String name)
- throws SlingException {
-
- // is the path absolute
- SlingScript script = null;
- if (name.startsWith("/")) {
-
- final String path = ResourceUtil.normalize(name);
- if ( this.isPathAllowed(path) ) {
- final Resource resource = resourceResolver.getResource(path);
- if ( resource != null ) {
- script = resource.adaptTo(SlingScript.class);
- }
- }
- } else {
-
- // relative script resolution against search path
- final String[] path = resourceResolver.getSearchPath();
- for (int i = 0; script == null && i < path.length; i++) {
- final String scriptPath = ResourceUtil.normalize(path[i] + name);
- if ( this.isPathAllowed(scriptPath) ) {
- final Resource resource = resourceResolver.getResource(scriptPath);
- if (resource != null) {
- script = resource.adaptTo(SlingScript.class);
- }
- }
- }
-
- }
-
- // some logging
- if (script != null) {
- LOGGER.debug("findScript: Using script {} for {}", script.getScriptResource().getPath(), name);
- } else {
- LOGGER.info("findScript: No script {} found in path", name);
- }
-
- // and finally return the script (null or not)
- return script;
- }
-
// ---------- ErrorHandler interface --------------------------------------
/**
@@ -828,27 +782,7 @@
}
createAllServlets(refs);
- // execution paths
- this.executionPaths = config.servletresolver_paths();
- if ( this.executionPaths != null ) {
- // if we find a string combination that basically allows all paths,
- // we simply set the array to null
- if ( this.executionPaths.length == 0 ) {
- this.executionPaths = null;
- } else {
- boolean hasRoot = false;
- for(int i = 0 ; i < this.executionPaths.length; i++) {
- final String path = this.executionPaths[i];
- if ( path == null || path.length() == 0 || path.equals("/") ) {
- hasRoot = true;
- break;
- }
- }
- if ( hasRoot ) {
- this.executionPaths = null;
- }
- }
- }
+ this.executionPaths = AbstractResourceCollector.getExecutionPaths(config.servletresolver_paths());
this.defaultExtensions = config.servletresolver_defaultExtensions();
// create cache - if a cache size is configured
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 7ee1cea..c6c1c67 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
@@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -262,6 +261,34 @@
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) {