Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 87520 invoked from network); 26 Nov 2008 21:27:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 26 Nov 2008 21:27:48 -0000 Received: (qmail 87328 invoked by uid 500); 26 Nov 2008 21:27:59 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 87280 invoked by uid 500); 26 Nov 2008 21:27:58 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 87271 invoked by uid 99); 26 Nov 2008 21:27:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Nov 2008 13:27:58 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Nov 2008 21:26:40 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id ED94323888A6; Wed, 26 Nov 2008 13:27:26 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r720984 - in /incubator/sling/whiteboard/fmeschbe/servlet_resolver: ./ src/main/java/org/apache/sling/servlets/resolver/helper/ src/test/java/org/apache/sling/servlets/resolver/helper/ Date: Wed, 26 Nov 2008 21:27:26 -0000 To: sling-commits@incubator.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081126212726.ED94323888A6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: fmeschbe Date: Wed Nov 26 13:27:26 2008 New Revision: 720984 URL: http://svn.apache.org/viewvc?rev=720984&view=rev Log: Prototype new servlet resolution Removed: incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGet.java incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetCalculatePrefixWeightTest.java Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/pom.xml incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollector.java incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/WeightedResource.java incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetServletsTest.java incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ScriptSelectionTest.java Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/pom.xml URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/servlet_resolver/pom.xml?rev=720984&r1=720983&r2=720984&view=diff ============================================================================== --- incubator/sling/whiteboard/fmeschbe/servlet_resolver/pom.xml (original) +++ incubator/sling/whiteboard/fmeschbe/servlet_resolver/pom.xml Wed Nov 26 13:27:26 2008 @@ -29,7 +29,7 @@ org.apache.sling.servlets.resolver bundle - 2.0.5-incubator-SNAPSHOT + 2.0.5-fmeschbe-incubator-SNAPSHOT Sling - Servlet Resolver Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollector.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollector.java?rev=720984&r1=720983&r2=720984&view=diff ============================================================================== --- incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollector.java (original) +++ incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/ResourceCollector.java Wed Nov 26 13:27:26 2008 @@ -25,12 +25,15 @@ import java.util.TreeSet; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.request.RequestPathInfo; 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.resource.SyntheticResource; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.servlets.resolver.resource.ServletResourceProviderFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The ResourceCollector class provides a single public method - @@ -40,6 +43,18 @@ */ public class ResourceCollector { + /** + * The special value returned by + * {@link #calculatePrefixMethodWeight(Resource, String, boolean)} if the + * resource is not suitable to handle the request according to the location + * prefix, request selectors and request extension (value is + * Integer.MIN_VALUE). + */ + protected static final int WEIGHT_NO_MATCH = Integer.MIN_VALUE; + + /** default log */ + private final Logger log = LoggerFactory.getLogger(getClass()); + // the request method name used to indicate the script name private final String methodName; @@ -47,6 +62,22 @@ // case the default servlet name will be used as the base name private final String baseResourceType; + // the request selectors as a string converted to a realtive path or + // null if the request has no selectors + private final String[] requestSelectors; + + // the number of request selectors of the request or 0 if none + private final int numRequestSelectors; + + // the request extension or null if the request has no extension + private final String extension; + + // request is GET or HEAD + private final boolean isGet; + + // request is GET or HEAD and extension is html + private final boolean isHtml; + /** * Creates a ResourceCollector for the given * request. If the request is a GET or HEAD request, a @@ -61,12 +92,7 @@ * suitable for handling the request. */ public static ResourceCollector create(SlingHttpServletRequest request) { - if (HttpConstants.METHOD_GET.equals(request.getMethod()) - || HttpConstants.METHOD_HEAD.equals(request.getMethod())) { - return new ResourceCollectorGet(request); - } - - return new ResourceCollector(request.getMethod(), null); + return new ResourceCollector(request); } /** @@ -84,6 +110,37 @@ public ResourceCollector(String methodName, String baseResourceType) { this.methodName = methodName; this.baseResourceType = baseResourceType; + this.requestSelectors = new String[0]; + this.numRequestSelectors = 0; + this.extension = null; + this.isGet = false; + this.isHtml = false; + } + + /** + * Creates a ResourceCollector finding servlets and scripts + * for the given methodName. + * + * @param methodName The methodName used to find scripts for. + * This must not be null. + * @param baseResourceType The basic resource type to use as a final + * resource super type. If this is null the + * default value + * {@link org.apache.sling.servlets.resolver.ServletResolverConstants#DEFAULT_SERVLET_NAME} + * is assumed. + */ + private ResourceCollector(SlingHttpServletRequest request) { + this.methodName = request.getMethod(); + this.baseResourceType = null; + + RequestPathInfo requestpaInfo = request.getRequestPathInfo(); + + requestSelectors = requestpaInfo.getSelectors(); + numRequestSelectors = requestSelectors.length; + extension = request.getRequestPathInfo().getExtension(); + + isGet = "GET".equals(methodName) || "HEAD".equals(methodName); + isHtml = isGet && "html".equals(extension); } public final Collection getServlets(Resource resource) { @@ -92,7 +149,7 @@ ResourceResolver resolver = resource.getResourceResolver(); Iterator locations = new LocationIterator(resource, - getBaseResourceType()); + baseResourceType); while (locations.hasNext()) { String location = locations.next(); @@ -106,35 +163,82 @@ return resources; } - /** - * Returns all resources inside the location whose base name - * equals the {@link #getMethodName()} and which just have a single - * extension after the base name. In addition, the special resource at - * location.getPath + ".servlet" is checked to find servlets - * which are registered with no specific method name. - * - * @param resources The collection into which any resources found are added. - * @param location The location in the resource tree where the servlets and - * scripts are to be found. - */ protected void getWeightedResources(Set resources, Resource location) { - // now list the children and check them - Iterator children = location.getResourceResolver().listChildren( - location); - while (children.hasNext()) { - Resource child = children.next(); - - String name = ResourceUtil.getName(child.getPath()); - String[] parts = name.split("\\."); - - // require method name plus script extension - if (parts.length == 2 && getMethodName().equals(parts[0])) { - addWeightedResource(resources, child, 0, - WeightedResource.WEIGHT_NONE); + ResourceResolver resolver = location.getResourceResolver(); + Resource current = location; + String parentName = ResourceUtil.getName(current); + + int selIdx = 0; + String selector; + do { + selector = (selIdx < numRequestSelectors) + ? requestSelectors[selIdx] + : null; + + Iterator children = resolver.listChildren(current); + while (children.hasNext()) { + Resource child = children.next(); + + String scriptName = ResourceUtil.getName(child); + int lastDot = scriptName.lastIndexOf('.'); + if (lastDot < 0) { + // no extension in the name, this is not a script + continue; + } + + scriptName = scriptName.substring(0, lastDot); + + if (isGet) { + + if (selector != null + && scriptName.equals(selector + "." + extension)) { + addWeightedResource(resources, child, selIdx + 1, + WeightedResource.WEIGHT_EXTENSION); + continue; + } + + if (scriptName.equals(parentName + "." + extension)) { + addWeightedResource(resources, child, selIdx, + WeightedResource.WEIGHT_EXTENSION + + WeightedResource.WEIGHT_PREFIX); + continue; + } + + if (scriptName.equals(extension)) { + addWeightedResource(resources, child, selIdx, + WeightedResource.WEIGHT_EXTENSION); + continue; + } + + if (isHtml) { + if (selector != null && scriptName.equals(selector)) { + addWeightedResource(resources, child, selIdx + 1, + WeightedResource.WEIGHT_NONE); + continue; + } + if (scriptName.equals(parentName)) { + addWeightedResource(resources, child, selIdx, + WeightedResource.WEIGHT_PREFIX); + continue; + } + } + } + + if (scriptName.equals(methodName)) { + addWeightedResource(resources, child, selIdx, + WeightedResource.WEIGHT_NONE); + continue; + } } - } + + if (selector != null) { + current = resolver.getResource(current, selector); + parentName = selector; + selIdx++; + } + } while (selector != null && current != null); // special treatment for servlets registered with neither a method // name nor extensions and selectors @@ -148,20 +252,6 @@ } /** - * Returns the basic resource type assigned to this instance - */ - public final String getBaseResourceType() { - return baseResourceType; - } - - /** - * Returns the method name assigned to this instance - */ - public final String getMethodName() { - return methodName; - } - - /** * Creates a {@link WeightedResource} and adds it to the set of resources. * The number of resources already present in the set is used as the ordinal * number for the newly created resource. Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/WeightedResource.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/WeightedResource.java?rev=720984&r1=720983&r2=720984&view=diff ============================================================================== --- incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/WeightedResource.java (original) +++ incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/main/java/org/apache/sling/servlets/resolver/helper/WeightedResource.java Wed Nov 26 13:27:26 2008 @@ -44,26 +44,27 @@ Comparable { /** - * Weight value assigned to an instance just bearing request method name. + * Weight value assigned to an instance just bearing request method name + * (value is 0). */ static final int WEIGHT_NONE = 0; /** * Weight value assigned to an instance if the the resource name neither * contains the parent resource name as a prefix, nor the request method - * name nor the request extension. + * name nor the request extension (value is -1). */ static final int WEIGHT_LAST_RESSORT = -1; /** * Weight value added to method/prefix weight if the resource name contains - * the the name of the parent resource as its prefix. + * the the name of the parent resource as its prefix (value is 1). */ static final int WEIGHT_PREFIX = 1; /** * Weight value added to method/prefix weight if the resource name contains - * the request extension. + * the request extension (value is 2). */ static final int WEIGHT_EXTENSION = 2; Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetServletsTest.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetServletsTest.java?rev=720984&r1=720983&r2=720984&view=diff ============================================================================== --- incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetServletsTest.java (original) +++ incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ResourceCollectorGetServletsTest.java Wed Nov 26 13:27:26 2008 @@ -48,9 +48,11 @@ "/print/a4.esp", // 5 "/print.html.esp", // 6 "/print/a4.html.esp", // 7 + "/print", // resource to enable walking the tree + "/print", // resource to enable walking the tree }; - int[] baseIdxs = { 0, 1, 1, 0, 0, 1, 0, 1 }; + int[] baseIdxs = { 0, 1, 1, 0, 0, 1, 0, 1, 0, 1 }; int[] indices = { 7, 5, 6, 4, 2, 3, 0, 1 }; effectiveTest(names, baseIdxs, indices); @@ -65,9 +67,11 @@ "/print/other.esp", // 5 "/print.html.esp", // 6 "/print/a4.html.esp", // 7 + "/print", // resource to enable walking the tree + "/print", // resource to enable walking the tree }; - int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; + int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; int[] indices = { 7, 6, 3, 2, 0, 1 }; effectiveTest(names, baseIdxs, indices); @@ -83,9 +87,11 @@ "/print.other.esp", // 6 "/print.html.esp", // 7 "/print/a4.html.esp", // 8 + "/print", // resource to enable walking the tree + "/print", // resource to enable walking the tree }; - int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; + int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; int[] indices = { 8, 7, 2, 3, 0, 1 }; effectiveTest(names, baseIdxs, indices); @@ -102,9 +108,11 @@ "/print.other.esp", // 7 "/print.html.esp", // 8 "/print/a4.html.esp", // 9 + "/print", // resource to enable walking the tree + "/print", // resource to enable walking the tree }; - int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; + int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; int[] indices = { 9, 8, 3, 4, 1, 2, 0 }; effectiveTest(names, baseIdxs, indices); @@ -122,9 +130,11 @@ "/print.other.esp", // 8 "/print.html.esp", // 9 "/print/a4.html.esp", // 10 + "/print", // resource to enable walking the tree + "/print", // resource to enable walking the tree }; - int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }; + int[] baseIdxs = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 }; int[] indices = { 10, 9, 3, 4, 1, 2, 0 }; effectiveTest(names, baseIdxs, indices); Modified: incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ScriptSelectionTest.java URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ScriptSelectionTest.java?rev=720984&r1=720983&r2=720984&view=diff ============================================================================== --- incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ScriptSelectionTest.java (original) +++ incubator/sling/whiteboard/fmeschbe/servlet_resolver/src/test/java/org/apache/sling/servlets/resolver/helper/ScriptSelectionTest.java Wed Nov 26 13:27:26 2008 @@ -23,7 +23,6 @@ import org.apache.sling.api.resource.Resource; import org.apache.sling.commons.testing.sling.MockResource; import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest; -import org.apache.sling.servlets.resolver.helper.ResourceCollector; /** Various tests that explain and demonstrate how scripts are * selected. See the assertScript methods for how to interpret @@ -36,9 +35,15 @@ "/apps/foo/bar/html.esp", "/apps/foo/bar/POST.esp", "/apps/foo/bar/print.esp", + "/apps/foo/bar/print", "/apps/foo/bar/print/POST.esp", "/apps/foo/bar/xml.esp", - "/apps/foo/bar/print.xml.esp" + "/apps/foo/bar/print.xml.esp", + "/apps/foo/bar/print/DELETE.esp", + "/apps/foo/bar/DELETE.esp", + "/apps/foo/bar/SPURIOUS.esp", + "/apps/foo/bar/UNKNOWN.esp" + }; /** Given a list of available scripts and the request method, selectors @@ -131,6 +136,7 @@ public void testMultipleSelectorsA() { final String [] scripts = { + "/apps/foo/bar/print", "/apps/foo/bar/print/a4.esp", "/apps/foo/bar/print.a4.esp", "/apps/foo/bar/html.print.a4.esp", @@ -177,6 +183,7 @@ "/apps/foo/bar/print.esp", "/apps/foo/bar/a4.esp", "/apps/foo/bar/bar.a4.esp", + "/apps/foo/bar/print", "/apps/foo/bar/print/a4.esp", "/apps/foo/bar/html.esp" }; @@ -225,7 +232,17 @@ } public void testHtmlPostSelectors() { - assertScript("POST", "print.a4", "html", SET_A, "/apps/foo/bar/POST.esp"); + assertScript("POST", "print.a4", "html", SET_A, "/apps/foo/bar/print/POST.esp"); + assertScript("POST", "print", "html", SET_A, "/apps/foo/bar/print/POST.esp"); + assertScript("POST", "a4.print", "html", SET_A, "/apps/foo/bar/POST.esp"); + assertScript("POST", null, "html", SET_A, "/apps/foo/bar/POST.esp"); + } + + public void testHtmlMethodSelectors() { + assertScript("DELETE", "print.a4", "html", SET_A, "/apps/foo/bar/print/DELETE.esp"); + assertScript("SPURIOUS", "print", "html", SET_A, "/apps/foo/bar/SPURIOUS.esp"); + assertScript("UNKNOWN", "a4.print", "html", SET_A, "/apps/foo/bar/UNKNOWN.esp"); + assertScript("UNKNOWN", null, "html", SET_A, "/apps/foo/bar/UNKNOWN.esp"); } public void testHtmlPostSelectorsAreIgnored() {