incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r939362 - in /sling/trunk/bundles/servlets/resolver: ./ src/main/java/org/apache/sling/servlets/resolver/internal/ src/main/java/org/apache/sling/servlets/resolver/internal/helper/ src/test/java/org/apache/sling/servlets/resolver/internal/ ...
Date Thu, 29 Apr 2010 16:19:57 GMT
Author: cziegeler
Date: Thu Apr 29 16:19:57 2010
New Revision: 939362

URL: http://svn.apache.org/viewvc?rev=939362&view=rev
Log:
SLING-1512 : Make script execution paths configurable

Modified:
    sling/trunk/bundles/servlets/resolver/pom.xml
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
    sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/SlingServletResolverTest.java
    sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollectorGetServletsTest.java
    sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ScriptSelectionTest.java

Modified: sling/trunk/bundles/servlets/resolver/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/pom.xml?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/pom.xml (original)
+++ sling/trunk/bundles/servlets/resolver/pom.xml Thu Apr 29 16:19:57 2010
@@ -92,7 +92,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.osgi</artifactId>
-            <version>2.0.2-incubator</version>
+            <version>2.0.6</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
Thu Apr 29 16:19:57 2010
@@ -155,11 +155,18 @@ public class SlingServletResolver implem
 
     private static final String REF_SERVLET = "Servlet";
 
+    /**
+     * @scr.property values="/"
+     */
+    public static final String PROP_PATHS = "servletresolver.paths";
+
+    private static final String[] DEFAULT_PATHS = new String[] {"/"};
+
     /** @scr.reference */
     private ServletContext servletContext;
 
     /** @scr.reference */
-    private JcrResourceResolverFactory resourceResolverFactory;
+    private JcrResourceResolverFactory jcrResourceResolverFactory;
 
     /** @scr.reference */
     private SlingRepository repository;
@@ -174,8 +181,6 @@ public class SlingServletResolver implem
 
     private WorkspaceResourceResolver defaultScriptResolver;
 
-    private String[] searchPath;
-
     private Map<ServiceReference, ServiceRegistration> servletsByReference = new HashMap<ServiceReference,
ServiceRegistration>();
 
     private List<ServiceReference> pendingServlets = new ArrayList<ServiceReference>();
@@ -193,8 +198,10 @@ public class SlingServletResolver implem
     // a request. This field is set on demand by getDefaultErrorServlet()
     private Servlet fallbackErrorServlet;
 
+    /** The script resolution cache. */
     private Map<AbstractResourceCollector, Servlet> cache;
 
+    /** Registration as event handler. */
     private ServiceRegistration eventHandlerReg;
 
     /**
@@ -216,6 +223,11 @@ public class SlingServletResolver implem
      */
     private boolean useDefaultWorkspace;
 
+    /**
+     * The allowed execution paths.
+     */
+    private String[] executionPaths;
+
     // ---------- ServletResolver interface -----------------------------------
 
     /**
@@ -328,6 +340,7 @@ public class SlingServletResolver implem
         return servlet;
     }
 
+    /** Internal method to resolve a servlet. */
     private Servlet resolveServlet(final ResourceResolver resolver,
             final Resource resource,
             final String scriptName) {
@@ -336,19 +349,20 @@ public class SlingServletResolver implem
         // first check whether the type of a resource is the absolute
         // path of a servlet (or script)
         if (scriptName.charAt(0) == '/') {
-            final Resource res = resolver.getResource(scriptName);
-            if (res != null) {
-                servlet = res.adaptTo(Servlet.class);
-            }
-            if (servlet != null && log.isDebugEnabled()) {
-                log.debug("Servlet {} found using absolute resource type {}", RequestUtil.getServletName(servlet),
-                                scriptName);
+            if ( this.isPathAllowed(scriptName) ) {
+                final Resource res = resolver.getResource(scriptName);
+                if (res != null) {
+                    servlet = res.adaptTo(Servlet.class);
+                }
+                if (servlet != null && log.isDebugEnabled()) {
+                    log.debug("Servlet {} found using absolute resource type {}", RequestUtil.getServletName(servlet),
+                                    scriptName);
+                }
             }
-        }
+        } else {
 
-        // the resource type is not absolute, so lets go for the deep search
-        if (servlet == null) {
-            final NamedScriptResourceCollector locationUtil = NamedScriptResourceCollector.create(scriptName,
resource);
+            // the resource type is not absolute, so lets go for the deep search
+            final NamedScriptResourceCollector locationUtil = NamedScriptResourceCollector.create(scriptName,
resource, this.executionPaths);
             servlet = getServlet(locationUtil, null, resolver);
 
             if (log.isDebugEnabled()) {
@@ -360,26 +374,33 @@ public class SlingServletResolver implem
     }
     // ---------- ScriptResolver interface ------------------------------------
 
-    public SlingScript findScript(ResourceResolver resourceResolver, String name) throws
SlingException {
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptResolver#findScript(org.apache.sling.api.resource.ResourceResolver,
java.lang.String)
+     */
+    public SlingScript findScript(final ResourceResolver resourceResolver, final String name)
+    throws SlingException {
 
         // is the path absolute
         SlingScript script = null;
         if (name.startsWith("/")) {
 
-            Resource resource = resourceResolver.getResource(name);
-            if (resource != null) {
-                script = resource.adaptTo(SlingScript.class);
+            if ( this.isPathAllowed(name) ) {
+                final Resource resource = resourceResolver.getResource(name);
+                if (resource != null) {
+                    script = resource.adaptTo(SlingScript.class);
+                }
             }
-
         } else {
 
             // relative script resolution against search path
-            String[] path = resourceResolver.getSearchPath();
+            final String[] path = resourceResolver.getSearchPath();
             for (int i = 0; script == null && i < path.length; i++) {
-                String scriptPath = path[i] + name;
-                Resource resource = resourceResolver.getResource(scriptPath);
-                if (resource != null) {
-                    script = resource.adaptTo(SlingScript.class);
+                final String scriptPath = path[i] + name;
+                if ( this.isPathAllowed(scriptPath) ) {
+                    final Resource resource = resourceResolver.getResource(scriptPath);
+                    if (resource != null) {
+                        script = resource.adaptTo(SlingScript.class);
+                    }
                 }
             }
 
@@ -425,7 +446,8 @@ public class SlingServletResolver implem
 
             // find a servlet for the status as the method name
             ResourceCollector locationUtil = new ResourceCollector(String.valueOf(status),
-                    ServletResolverConstants.ERROR_HANDLER_PATH, resource, scriptResolver.getWorkspaceName());
+                    ServletResolverConstants.ERROR_HANDLER_PATH, resource, scriptResolver.getWorkspaceName(),
+                    this.executionPaths);
             Servlet servlet = getServlet(locationUtil, request, scriptResolver);
 
             // fall back to default servlet if none
@@ -483,7 +505,8 @@ public class SlingServletResolver implem
             while (servlet == null && tClass != Object.class) {
                 // find a servlet for the simple class name as the method name
                 ResourceCollector locationUtil = new ResourceCollector(tClass.getSimpleName(),
-                        ServletResolverConstants.ERROR_HANDLER_PATH, resource, scriptResolver.getWorkspaceName());
+                        ServletResolverConstants.ERROR_HANDLER_PATH, resource, scriptResolver.getWorkspaceName(),
+                        this.executionPaths);
                 servlet = getServlet(locationUtil, request, scriptResolver);
 
                 // go to the base class
@@ -543,20 +566,19 @@ public class SlingServletResolver implem
         // first check whether the type of a resource is the absolute
         // path of a servlet (or script)
         if (type.charAt(0) == '/') {
-            final Resource res = resolver.getResource(type);
-            if (res != null) {
-                servlet = res.adaptTo(Servlet.class);
-            }
-            if (servlet != null && log.isDebugEnabled()) {
-                log
-                        .debug("Servlet {} found using absolute resource type {}", RequestUtil.getServletName(servlet),
-                                type);
+            if ( this.isPathAllowed(type) ) {
+                final Resource res = resolver.getResource(type);
+                if (res != null) {
+                    servlet = res.adaptTo(Servlet.class);
+                }
+                if (servlet != null && log.isDebugEnabled()) {
+                    log.debug("Servlet {} found using absolute resource type {}", RequestUtil.getServletName(servlet),
+                                    type);
+                }
             }
-        }
-
-        // the resource type is not absolute, so lets go for the deep search
-        if (servlet == null) {
-            final ResourceCollector locationUtil = ResourceCollector.create(request, resolver.getWorkspaceName());
+        } else {
+            // the resource type is not absolute, so lets go for the deep search
+            final ResourceCollector locationUtil = ResourceCollector.create(request, resolver.getWorkspaceName(),
this.executionPaths);
             servlet = getServlet(locationUtil, request, resolver);
 
             if (log.isDebugEnabled()) {
@@ -685,7 +707,8 @@ public class SlingServletResolver implem
         final ResourceCollector locationUtil = new ResourceCollector(
             ServletResolverConstants.DEFAULT_ERROR_HANDLER_NAME,
             ServletResolverConstants.ERROR_HANDLER_PATH, resource,
-            scriptResolver.getWorkspaceName());
+            scriptResolver.getWorkspaceName(),
+            this.executionPaths);
         final Servlet servlet = getServlet(locationUtil, request,
             scriptResolver);
         if (servlet != null) {
@@ -760,7 +783,7 @@ public class SlingServletResolver implem
             scriptSession = sessionFromMap;
         }
 
-        scriptResolver = new WorkspaceResourceResolver(this.resourceResolverFactory.getResourceResolver(scriptSession),
+        scriptResolver = new WorkspaceResourceResolver(this.jcrResourceResolverFactory.getResourceResolver(scriptSession),
                 wspName);
         WorkspaceResourceResolver resolverFromMap = scriptResolvers.putIfAbsent(wspName,
scriptResolver);
         if (resolverFromMap != null) {
@@ -794,60 +817,88 @@ public class SlingServletResolver implem
 
     // ---------- SCR Integration ----------------------------------------------
 
+    /**
+     * Activate this component.
+     */
     protected void activate(ComponentContext context) {
-
         // from configuration if available
-        Dictionary<?, ?> properties = context.getProperties();
+        final Dictionary<?, ?> properties = context.getProperties();
         Object servletRoot = properties.get(PROP_SERVLET_ROOT);
         if (servletRoot == null) {
             servletRoot = DEFAULT_SERVLET_ROOT;
         }
 
-        Collection<ServiceReference> refs;
+        final Collection<ServiceReference> refs;
         synchronized (this) {
 
             refs = pendingServlets;
             pendingServlets = new ArrayList<ServiceReference>();
 
-            searchPath = this.resourceResolverFactory.getResourceResolver(null).getSearchPath();
-
-            servletResourceProviderFactory = new ServletResourceProviderFactory(servletRoot,
searchPath);
-
             // register servlets immediately from now on
             this.context = context;
 
-        }
-
-        this.useDefaultWorkspace = OsgiUtil.toBoolean(properties.get(PROP_USE_DEFAULT_WORKSPACE),
DEFAULT_USE_DEFAULT_WORKSPACE);
-        this.useRequestWorkspace = OsgiUtil.toBoolean(properties.get(PROP_USE_REQUEST_WORKSPACE),
DEFAULT_USE_REQUEST_WORKSPACE);
+            // workspace handling and resource resolver creation
+            this.useDefaultWorkspace = OsgiUtil.toBoolean(properties.get(PROP_USE_DEFAULT_WORKSPACE),
DEFAULT_USE_DEFAULT_WORKSPACE);
+            this.useRequestWorkspace = OsgiUtil.toBoolean(properties.get(PROP_USE_REQUEST_WORKSPACE),
DEFAULT_USE_REQUEST_WORKSPACE);
+
+            this.scriptSessions = new ConcurrentHashMap<String, Session>();
+            this.scriptResolvers = new ConcurrentHashMap<String, WorkspaceResourceResolver>();
+
+            String defaultWorkspaceProp = (String) properties.get(PROP_DEFAULT_SCRIPT_WORKSPACE);
+            if ( defaultWorkspaceProp != null && defaultWorkspaceProp.trim().length()
== 0 ) {
+                defaultWorkspaceProp = null;
+            }
+            this.defaultScriptSession = createScriptSession(defaultWorkspaceProp);
+
+            // we load the workspaceName out of the session to ensure the value is
+            // non-null
+            this.defaultScriptWorkspaceName = this.defaultScriptSession.getWorkspace().getName();
+            this.defaultScriptResolver = new WorkspaceResourceResolver(jcrResourceResolverFactory
+                    .getResourceResolver(defaultScriptSession), defaultScriptWorkspaceName);
 
-        this.scriptSessions = new ConcurrentHashMap<String, Session>();
-        this.scriptResolvers = new ConcurrentHashMap<String, WorkspaceResourceResolver>();
+            servletResourceProviderFactory = new ServletResourceProviderFactory(servletRoot,
+                    this.defaultScriptResolver.getSearchPath());
+        }
+        createAllServlets(refs);
 
-        String defaultWorkspaceProp = (String) properties.get(PROP_DEFAULT_SCRIPT_WORKSPACE);
-        if ( defaultWorkspaceProp != null && defaultWorkspaceProp.trim().length()
== 0 ) {
-            defaultWorkspaceProp = null;
-        }
-        this.defaultScriptSession = createScriptSession(defaultWorkspaceProp);
-
-        // we load the workspaceName out of the session to ensure the value is
-        // non-null
-        this.defaultScriptWorkspaceName = this.defaultScriptSession.getWorkspace().getName();
-        this.defaultScriptResolver = new WorkspaceResourceResolver(resourceResolverFactory
-                .getResourceResolver(defaultScriptSession), defaultScriptWorkspaceName);
+        // execution paths
+        this.executionPaths = OsgiUtil.toStringArray(properties.get(PROP_PATHS), DEFAULT_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;
+                    }
+                    if ( !path.endsWith("/") ) {
+                        this.executionPaths[i] = path + '/';
+                    }
+                }
+                if ( hasRoot ) {
+                    this.executionPaths = null;
+                }
+            }
+        }
 
         // create cache - if a cache size is configured
         final int cacheSize = OsgiUtil.toInteger(properties.get(PROP_CACHE_SIZE), DEFAULT_CACHE_SIZE);
         if (cacheSize > 5) {
             this.cache = new ConcurrentHashMap<AbstractResourceCollector, Servlet>(cacheSize);
         }
-        createAllServlets(refs);
 
         // and finally register as event listener
-        this.eventHandlerReg = this.context.getBundleContext().registerService(EventHandler.class.getName(),
this,
+        this.eventHandlerReg = context.getBundleContext().registerService(EventHandler.class.getName(),
this,
                 properties);
     }
 
+    /**
+     * Deactivate this component.
+     */
     protected void deactivate(ComponentContext context) {
         // unregister event handler
         if (this.eventHandlerReg != null) {
@@ -857,10 +908,12 @@ public class SlingServletResolver implem
 
         // Copy the list of servlets first, to minimize the need for
         // synchronization
-        Collection<ServiceReference> refs;
+        final Collection<ServiceReference> refs;
         synchronized (this) {
             refs = new ArrayList<ServiceReference>(servletsByReference.keySet());
         }
+        // destroy all servlets
+        destroyAllServlets(refs);
 
         // destroy the fallback error handler servlet
         if (fallbackErrorServlet != null) {
@@ -873,8 +926,7 @@ public class SlingServletResolver implem
             }
         }
 
-        // destroy all servlets
-        destroyAllServlets(refs);
+        // close sessions
         if (this.scriptSessions != null && (!this.scriptSessions.isEmpty())) {
             for (Session session : this.scriptSessions.values()) {
                 session.logout();
@@ -958,7 +1010,7 @@ public class SlingServletResolver implem
             return false;
         }
 
-        Dictionary<String, Object> params = new Hashtable<String, Object>();
+        final Dictionary<String, Object> params = new Hashtable<String, Object>();
         params.put(ResourceProvider.ROOTS, provider.getServletPaths());
         params.put(Constants.SERVICE_DESCRIPTION, "ServletResourceProvider for Servlets at
"
                 + Arrays.asList(provider.getServletPaths()));
@@ -1025,7 +1077,7 @@ public class SlingServletResolver implem
                 if (path.contains(":")) {
                     path = path.substring(path.indexOf(":") + 1);
                 }
-                final String[] searchPaths = this.searchPath;
+                final String[] searchPaths = this.defaultScriptResolver.getSearchPath();
                 int index = 0;
                 while (!flushCache && index < searchPaths.length) {
                     if (path.startsWith(searchPaths[index])) {
@@ -1060,4 +1112,8 @@ public class SlingServletResolver implem
         }
         return servletName;
     }
+
+    private boolean isPathAllowed(final String path) {
+        return AbstractResourceCollector.isPathAllowed(path, this.executionPaths);
+    }
 }

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
Thu Apr 29 16:19:57 2010
@@ -49,14 +49,18 @@ public abstract class AbstractResourceCo
 
     protected final String resourceSuperType;
 
+    protected final String[] executionPaths;
+
     public AbstractResourceCollector(final String baseResourceType,
             final String resourceType,
             final String resourceSuperType,
-            final String extension) {
+            final String extension,
+            final String[] executionPaths) {
         this.baseResourceType = baseResourceType;
         this.resourceType = resourceType;
         this.resourceSuperType = resourceSuperType;
         this.extension = extension;
+        this.executionPaths = executionPaths;
     }
 
     public final Collection<Resource> getServlets(ResourceResolver resolver) {
@@ -170,4 +174,21 @@ public abstract class AbstractResourceCo
         }
         return s1.equals(s2);
     }
+
+    protected boolean isPathAllowed(final String path) {
+        return isPathAllowed(path, this.executionPaths);
+    }
+
+    public static boolean isPathAllowed(final String path, final String[] executionPaths)
{
+        if ( executionPaths == null ) {
+            return true;
+        }
+        for(final String prefix : executionPaths ) {
+            if ( path.startsWith(prefix) ) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 }

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
Thu Apr 29 16:19:57 2010
@@ -36,7 +36,9 @@ public class NamedScriptResourceCollecto
 
     private final String scriptName;
 
-    public static NamedScriptResourceCollector create(final String name, final Resource resource)
{
+    public static NamedScriptResourceCollector create(final String name,
+            final Resource resource,
+            final String[] executionPaths) {
         final String resourceType;
         final String resourceSuperType;
         final String baseResourceType;
@@ -62,15 +64,17 @@ public class NamedScriptResourceCollecto
                 resourceType,
                 resourceSuperType,
                 scriptName,
-                extension);
+                extension,
+                executionPaths);
     }
 
     public NamedScriptResourceCollector(final String baseResourceType,
                               final String resourceType,
                               final String resourceSuperType,
                               final String scriptName,
-                              final String extension) {
-        super(baseResourceType, resourceType, resourceSuperType, extension);
+                              final String extension,
+                              final String[] executionPaths) {
+        super(baseResourceType, resourceType, resourceSuperType, extension, executionPaths);
         this.scriptName = scriptName;
         // create the hash code once
         final String key = baseResourceType + ':' + this.scriptName + ':' +
@@ -85,9 +89,11 @@ public class NamedScriptResourceCollecto
         // if extension is set, we first check for an exact script match
         if ( this.extension != null ) {
             final String path = location.getPath() + '/' + this.scriptName;
-            final Resource current = resolver.getResource(path);
-            if ( current != null ) {
-                this.addWeightedResource(resources, current, 0, WeightedResource.WEIGHT_EXTENSION);
+            if ( this.isPathAllowed(path) ) {
+                final Resource current = resolver.getResource(path);
+                if ( current != null ) {
+                    this.addWeightedResource(resources, current, 0, WeightedResource.WEIGHT_EXTENSION);
+                }
             }
         }
         // if the script name denotes a path we have to get the denoted resource
@@ -106,6 +112,9 @@ public class NamedScriptResourceCollecto
         while (children.hasNext()) {
             final Resource child = children.next();
 
+            if ( !this.isPathAllowed(child.getPath()) ) {
+                continue;
+            }
             final String currentScriptName = ResourceUtil.getName(child);
             final int lastDot = currentScriptName.lastIndexOf('.');
             if (lastDot < 0) {
@@ -115,7 +124,6 @@ public class NamedScriptResourceCollecto
 
             if ( currentScriptName.substring(0, lastDot).equals(name) ) {
                 this.addWeightedResource(resources, child, 0, WeightedResource.WEIGHT_PREFIX);
-                continue;
             }
         }
     }

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
Thu Apr 29 16:19:57 2010
@@ -78,8 +78,10 @@ public class ResourceCollector extends A
      * @return The <code>ResourceCollector</code> to find servlets and scripts
      *         suitable for handling the <code>request</code>.
      */
-    public static ResourceCollector create(SlingHttpServletRequest request, String workspaceName)
{
-        return new ResourceCollector(request, workspaceName);
+    public static ResourceCollector create(final SlingHttpServletRequest request,
+            final String workspaceName,
+            final String[] executionPaths) {
+        return new ResourceCollector(request, workspaceName, executionPaths);
     }
 
     /**
@@ -95,11 +97,16 @@ public class ResourceCollector extends A
      *            is assumed.
      * @param resource the resource to invoke, the resource type and resource super type
are taken from this resource.
      */
-    public ResourceCollector(String methodName, String baseResourceType, Resource resource,
String workspaceName) {
+    public ResourceCollector(final String methodName,
+            final String baseResourceType,
+            final Resource resource,
+            final String workspaceName,
+            final String[] executionPaths) {
         super((baseResourceType != null ? baseResourceType : ServletResolverConstants.DEFAULT_SERVLET_NAME),
                 resource.getResourceType(),
                 resource.getResourceSuperType(),
-                null);
+                null,
+                executionPaths);
         this.methodName = methodName;
         this.requestSelectors = new String[0];
         this.numRequestSelectors = 0;
@@ -127,11 +134,14 @@ public class ResourceCollector extends A
      *            {@link org.apache.sling.servlets.resolver.internal.ServletResolverConstants#DEFAULT_SERVLET_NAME}
      *            is assumed.
      */
-    private ResourceCollector(SlingHttpServletRequest request, String workspaceName) {
+    private ResourceCollector(final SlingHttpServletRequest request,
+            final String workspaceName,
+            final String[] executionPaths) {
         super(ServletResolverConstants.DEFAULT_SERVLET_NAME,
                 request.getResource().getResourceType(),
                 request.getResource().getResourceSuperType(),
-                request.getRequestPathInfo().getExtension());
+                request.getRequestPathInfo().getExtension(),
+                executionPaths);
         this.methodName = request.getMethod();
 
         RequestPathInfo requestpaInfo = request.getRequestPathInfo();
@@ -168,6 +178,9 @@ public class ResourceCollector extends A
             while (children.hasNext()) {
                 Resource child = children.next();
 
+                if ( !this.isPathAllowed(child.getPath()) ) {
+                    continue;
+                }
                 String scriptName = ResourceUtil.getName(child);
                 int lastDot = scriptName.lastIndexOf('.');
                 if (lastDot < 0) {
@@ -238,10 +251,12 @@ public class ResourceCollector extends A
         // name nor extensions and selectors
         String path = location.getPath()
             + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION;
-        location = location.getResourceResolver().getResource(path);
-        if (location != null) {
-            addWeightedResource(resources, location, 0,
-                WeightedResource.WEIGHT_LAST_RESSORT);
+        if ( this.isPathAllowed(path) ) {
+            location = location.getResourceResolver().getResource(path);
+            if (location != null) {
+                addWeightedResource(resources, location, 0,
+                    WeightedResource.WEIGHT_LAST_RESSORT);
+            }
         }
     }
 

Modified: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/SlingServletResolverTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/SlingServletResolverTest.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/SlingServletResolverTest.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/SlingServletResolverTest.java
Thu Apr 29 16:19:57 2010
@@ -73,7 +73,11 @@ public class SlingServletResolverTest {
     private MockResourceResolver mockResourceResolver;
 
     @Before public void setUp() throws Exception {
-        mockResourceResolver = new MockResourceResolver();
+        mockResourceResolver = new MockResourceResolver() {
+            public void close() {
+                // nothing to do;
+            }
+        };
         mockResourceResolver.setSearchPath("/");
 
         final JcrResourceResolverFactory factory = new JcrResourceResolverFactory() {
@@ -100,7 +104,7 @@ public class SlingServletResolverTest {
         Class<?> resolverClass = servletResolver.getClass().getSuperclass();
 
         // set resource resolver factory
-        final Field resolverField = resolverClass.getDeclaredField("resourceResolverFactory");
+        final Field resolverField = resolverClass.getDeclaredField("jcrResourceResolverFactory");
         resolverField.setAccessible(true);
         resolverField.set(servletResolver, factory);
 

Modified: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollectorGetServletsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollectorGetServletsTest.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollectorGetServletsTest.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollectorGetServletsTest.java
Thu Apr 29 16:19:57 2010
@@ -154,7 +154,7 @@ public class ResourceCollectorGetServlet
             pathMap.put(name, path);
         }
 
-        ResourceCollector lu = ResourceCollector.create(request, "default");
+        ResourceCollector lu = ResourceCollector.create(request, "default", null);
         Collection<Resource> res = lu.getServlets(request.getResource().getResourceResolver());
         Iterator<Resource> rIter = res.iterator();
 

Modified: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ScriptSelectionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ScriptSelectionTest.java?rev=939362&r1=939361&r2=939362&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ScriptSelectionTest.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/helper/ScriptSelectionTest.java
Thu Apr 29 16:19:57 2010
@@ -67,7 +67,7 @@ public class ScriptSelectionTest extends
 
         // Create mock request and get scripts from ResourceCollector
         final MockSlingHttpServletRequest req = makeRequest(method, selectors, extension);
-        final ResourceCollector u = ResourceCollector.create(req, "default");
+        final ResourceCollector u = ResourceCollector.create(req, "default", null);
         final Collection<Resource> s = u.getServlets(req.getResource().getResourceResolver());
 
         if(expectedScript == null) {



Mime
View raw message