Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 79382 invoked from network); 7 Feb 2008 11:31:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Feb 2008 11:31:49 -0000 Received: (qmail 11352 invoked by uid 500); 7 Feb 2008 11:31:41 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 11314 invoked by uid 500); 7 Feb 2008 11:31:41 -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 11304 invoked by uid 99); 7 Feb 2008 11:31:41 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Feb 2008 03:31:41 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Feb 2008 11:31:30 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 3A94E1A9832; Thu, 7 Feb 2008 03:31:21 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r619365 - in /incubator/sling/trunk/api: ./ src/main/java/org/apache/sling/api/request/ src/main/java/org/apache/sling/api/scripting/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/... Date: Thu, 07 Feb 2008 11:31:18 -0000 To: sling-commits@incubator.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080207113121.3A94E1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: fmeschbe Date: Thu Feb 7 03:31:05 2008 New Revision: 619365 URL: http://svn.apache.org/viewvc?rev=619365&view=rev Log: SLING-234 move request dispatcher option parsing to a RequestDispatcherOptions constructor, add unit tests and provide getters and setters for predefined options Added: incubator/sling/trunk/api/src/test/ incubator/sling/trunk/api/src/test/java/ incubator/sling/trunk/api/src/test/java/org/ incubator/sling/trunk/api/src/test/java/org/apache/ incubator/sling/trunk/api/src/test/java/org/apache/sling/ incubator/sling/trunk/api/src/test/java/org/apache/sling/api/ incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/ incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java Modified: incubator/sling/trunk/api/pom.xml incubator/sling/trunk/api/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java Modified: incubator/sling/trunk/api/pom.xml URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/pom.xml?rev=619365&r1=619364&r2=619365&view=diff ============================================================================== --- incubator/sling/trunk/api/pom.xml (original) +++ incubator/sling/trunk/api/pom.xml Thu Feb 7 03:31:05 2008 @@ -60,6 +60,11 @@ javax.servlet servlet-api + + + junit + junit + @@ -86,4 +91,4 @@ - + \ No newline at end of file Modified: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java?rev=619365&r1=619364&r2=619365&view=diff ============================================================================== --- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java (original) +++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java Thu Feb 7 03:31:05 2008 @@ -19,48 +19,163 @@ package org.apache.sling.api.request; import java.util.HashMap; +import java.util.StringTokenizer; -/** RequestDispatcherOptions are used in the - * {@link SlingHttpServletRequest#getRequestDispatcher(org.apache.sling.api.resource.Resource, RequestDispatcherOptions)} - * method, to give more control on some aspects of the include/forward - * mechanism. - * - * Typical use cases include: - *
    - *
  • - * Forcing a resource type, to render a Resource in a specific way, - * like for example render myself in a suitable way for a navigation box. - *
  • - *
  • - * Adding selectors when including a Resource, like for example add +/** + * RequestDispatcherOptions are used in the + * {@link SlingHttpServletRequest#getRequestDispatcher(org.apache.sling.api.resource.Resource, RequestDispatcherOptions)} + * method, to give more control on some aspects of the include/forward + * mechanism. Typical use cases include: + *
      + *
    • Forcing a resource type, to render a Resource in a specific way, like + * for example render myself in a suitable way for a navigation box. + *
    • + *
    • Adding selectors when including a Resource, like for example add * a "teaser" selector to the request that I'm including here. - *
    • - *
    - * - * This class currently only inherits from Map, and defines some constants - * for well-known options. + *
  • + *
+ * This class currently only inherits from Map, and defines some constants for + * well-known options. */ +public class RequestDispatcherOptions extends HashMap { -public class RequestDispatcherOptions extends HashMap { - - /** When dispatching, use the value provided by this option as the resource type, - * instead of the one defined by the {@link org.apache.sling.api.resource.Resource}. + /** + * When dispatching, use the value provided by this option as the resource + * type, instead of the one defined by the + * {@link org.apache.sling.api.resource.Resource}. */ public static final String OPT_FORCE_RESOURCE_TYPE = "forceResourceType"; - /** When dispatching, replace {@link RequestPathInfo} selectors by the - * value provided by this option. + /** + * When dispatching, replace {@link RequestPathInfo} selectors by the value + * provided by this option. */ public static final String OPT_REPLACE_SELECTORS = "replaceSelectors"; - /** When dispatching, add the value provided by this option to the - * {@link RequestPathInfo} selectors. + /** + * When dispatching, add the value provided by this option to the + * {@link RequestPathInfo} selectors. */ public static final String OPT_ADD_SELECTORS = "addSelectors"; - /** When dispatching, replace the {@link RequestPathInfo} suffix by - * the value provided by this option + /** + * When dispatching, replace the {@link RequestPathInfo} suffix by the value + * provided by this option + */ + public static final String OPT_REPLACE_SUFFIX = "replaceSuffix"; + + /** + * Creates an instance with no options set. */ - public static final String REPLACE_SUFFIX = "replaceSuffix"; + public RequestDispatcherOptions() { + } + /** + * Creates a new instances setting options by parsing the given + * options string as follows: + *
    + *
  • If the string is empty or null no options are set.
  • + *
  • If the string neither contains a comma nor an equals sign, the + * string is assumed to be a resource type. Hence a + * RequestDispatcherOptions object is created with the + * {@link RequestDispatcherOptions#OPT_FORCE_RESOURCE_TYPE} field set to the + * string.
  • + *
  • Otherwise the string is assumed to be a comma separated list of name + * value pairs where the equals sign is used to separate the name from its + * value. Hence a RequestDispatcherOptions object is created + * from the name value pair list.
  • + *
+ * + * @param options The options to set. + */ + public RequestDispatcherOptions(String options) { + + if (options != null && options.length() > 0) { + if (options.indexOf(',') < 0 && options.indexOf('=') < 0) { + setForceResourceType(options.trim()); + } else { + final StringTokenizer tk = new StringTokenizer(options, ","); + while (tk.hasMoreTokens()) { + String entry = tk.nextToken(); + int equals = entry.indexOf('='); + if (equals > 0 && equals < entry.length() - 1) { + put(entry.substring(0, equals).trim(), entry.substring( + equals + 1).trim()); + } + } + } + } + } + + /** + * Sets the {@link #OPT_FORCE_RESOURCE_TYPE} option to the given + * resourceType if not null. + */ + public void setForceResourceType(String resourceType) { + if (resourceType != null) { + put(OPT_FORCE_RESOURCE_TYPE, resourceType); + } + } + + /** + * Returns the {@link #OPT_FORCE_RESOURCE_TYPE} option or null + * if not set. + */ + public String getForceResourceType() { + return get(OPT_FORCE_RESOURCE_TYPE); + } + + /** + * Sets the {@link #OPT_ADD_SELECTORS} option to the given + * additionalSelectors if not null. + */ + public void setAddSelectors(String additionalSelectors) { + if (additionalSelectors != null) { + put(OPT_ADD_SELECTORS, additionalSelectors); + } + } + + /** + * Returns the {@link #OPT_ADD_SELECTORS} option or null if + * not set. + */ + public String getAddSelectors() { + return get(OPT_ADD_SELECTORS); + } + + /** + * Sets the {@link #OPT_REPLACE_SELECTORS} option to the given + * replaceSelectors if not null. + */ + public void setReplaceSelectors(String replaceSelectors) { + if (replaceSelectors != null) { + put(OPT_REPLACE_SELECTORS, replaceSelectors); + } + } + + /** + * Returns the {@link #OPT_REPLACE_SELECTORS} option or null + * if not set. + */ + public String getReplaceSelectors() { + return get(OPT_REPLACE_SELECTORS); + } + + /** + * Sets the {@link #OPT_REPLACE_SUFFIX} option to the given + * replaceSuffix if not null. + */ + public void setReplaceSuffix(String replaceSuffix) { + if (replaceSuffix != null) { + put(OPT_REPLACE_SUFFIX, replaceSuffix); + } + } + + /** + * Returns the {@link #OPT_REPLACE_SUFFIX} option or null if + * not set. + */ + public String getReplaceSuffix() { + return get(OPT_REPLACE_SUFFIX); + } } Modified: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java?rev=619365&r1=619364&r2=619365&view=diff ============================================================================== --- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java (original) +++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java Thu Feb 7 03:31:05 2008 @@ -75,20 +75,10 @@ * * *

- * The request dispatcher options provided to this method as a string are - * internally parsed into an {@link RequestDispatcherOptions} object as - * follows: - *

    - *
  • If the string neither contains a comma nor an equals sign, the - * string is assumed to be a resource type. Hence a - * RequestDispatcherOptions object is created with the - * {@link RequestDispatcherOptions#OPT_FORCE_RESOURCE_TYPE} field set to the - * string.
  • - *
  • Otherwise the string is assumed to be a comma separated list of name - * value pairs where the equals sign is used to separate the name from its - * value. Hence a RequestDispatcherOptions object is created - * from the name value pair list.
  • - *
+ * This method creates a RequestDispatcherOptions object by + * calling the + * {@link RequestDispatcherOptions#RequestDispatcherOptions(String)} + * constructor. * * @param path The path to the resource to include. * @param requestDispatcherOptions influence the rendering of the included @@ -97,7 +87,7 @@ * while handling the include. * @throws SlingServletException Wrapping a ServletException * thrown while handling the include. - * @see RequestDispatcherOptions + * @see RequestDispatcherOptions#RequestDispatcherOptions(String) * @see #include(String, RequestDispatcherOptions) */ void include(String path, String requestDispatcherOptions); Added: incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java?rev=619365&view=auto ============================================================================== --- incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java (added) +++ incubator/sling/trunk/api/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java Thu Feb 7 03:31:05 2008 @@ -0,0 +1,85 @@ +/* + * 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.api.request; + +import junit.framework.TestCase; + +public class RequestDispatcherOptionsTest extends TestCase { + + public void testNullString() { + final RequestDispatcherOptions result = new RequestDispatcherOptions( + null); + assertTrue(result.isEmpty()); + } + + public void testEmptyString() { + final RequestDispatcherOptions result = new RequestDispatcherOptions(""); + assertTrue(result.isEmpty()); + } + + public void testSingleOption() { + final RequestDispatcherOptions result = new RequestDispatcherOptions( + "forceResourceType= widget"); + assertNotNull(result); + assertEquals("Expected option found (" + result + ")", "widget", + result.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE)); + assertEquals("Expected option found (" + result + ")", "widget", + result.getForceResourceType()); + } + + public void testResourceTypeSlashShortcut() { + // a single option with no comma or colon means "forceResourceType" + final RequestDispatcherOptions result = new RequestDispatcherOptions( + "\t components/widget "); + assertNotNull(result); + assertEquals("Expected option found (" + result + ")", + "components/widget", + result.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE)); + assertEquals("Expected option found (" + result + ")", + "components/widget", result.getForceResourceType()); + } + + public void testResourceTypeColonShortcut() { + // a single option with no comma or colon means "forceResourceType" + final RequestDispatcherOptions result = new RequestDispatcherOptions( + "\t components:widget "); + assertNotNull(result); + assertEquals("Expected option found (" + result + ")", + "components:widget", + result.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE)); + assertEquals("Expected option found (" + result + ")", + "components:widget", result.getForceResourceType()); + } + + public void testTwoOptions() { + final RequestDispatcherOptions result = new RequestDispatcherOptions( + "forceResourceType= components:widget, replaceSelectors = xyz ,"); + assertNotNull(result); + assertEquals("Expected option found (" + result + ")", + "components:widget", + result.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE)); + assertEquals("Expected option found (" + result + ")", + "components:widget", result.getForceResourceType()); + assertEquals("Expected option found (" + result + ")", "xyz", + result.get(RequestDispatcherOptions.OPT_REPLACE_SELECTORS)); + assertEquals("Expected option found (" + result + ")", "xyz", + result.getReplaceSelectors()); + } + +}