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 @@
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
</dependencies>
<build>
@@ -86,4 +91,4 @@
</plugin>
</plugins>
</build>
-</project>
+</project>
\ 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;
-/** <code>RequestDispatcherOptions</code> are used in the
- * [EMAIL PROTECTED]
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:
- * <ul>
- * <li>
- * Forcing a resource type, to render a Resource in a specific way,
- * like for example <em>render myself in a suitable way for a
navigation box</em>.
- * </li>
- * <li>
- * Adding selectors when including a Resource, like for example
<em>add
+/**
+ * <code>RequestDispatcherOptions</code> are used in the
+ * [EMAIL PROTECTED]
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:
+ * <ul>
+ * <li> Forcing a resource type, to render a Resource in a specific way, like
+ * for example <em>render myself in a suitable way for a navigation box</em>.
+ * </li>
+ * <li> Adding selectors when including a Resource, like for example <em>add
* a "teaser" selector to the request that I'm including here</em>.
- * </li>
- * </ul>
- *
- * This class currently only inherits from Map, and defines some constants
- * for well-known options.
+ * </li>
+ * </ul>
+ * This class currently only inherits from Map, and defines some constants for
+ * well-known options.
*/
+public class RequestDispatcherOptions extends HashMap<String, String> {
-public class RequestDispatcherOptions extends HashMap <String,String> {
-
- /** When dispatching, use the value provided by this option as the
resource type,
- * instead of the one defined by the [EMAIL PROTECTED]
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
+ * [EMAIL PROTECTED] org.apache.sling.api.resource.Resource}.
*/
public static final String OPT_FORCE_RESOURCE_TYPE = "forceResourceType";
- /** When dispatching, replace [EMAIL PROTECTED] RequestPathInfo} selectors
by the
- * value provided by this option.
+ /**
+ * When dispatching, replace [EMAIL PROTECTED] 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
- * [EMAIL PROTECTED] RequestPathInfo} selectors.
+ /**
+ * When dispatching, add the value provided by this option to the
+ * [EMAIL PROTECTED] RequestPathInfo} selectors.
*/
public static final String OPT_ADD_SELECTORS = "addSelectors";
- /** When dispatching, replace the [EMAIL PROTECTED] RequestPathInfo}
suffix by
- * the value provided by this option
+ /**
+ * When dispatching, replace the [EMAIL PROTECTED] 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
+ * <code>options</code> string as follows:
+ * <ul>
+ * <li>If the string is empty or <code>null</code> no options are set.</li>
+ * <li>If the string neither contains a comma nor an equals sign, the
+ * string is assumed to be a resource type. Hence a
+ * <code>RequestDispatcherOptions</code> object is created with the
+ * [EMAIL PROTECTED] RequestDispatcherOptions#OPT_FORCE_RESOURCE_TYPE}
field set to the
+ * string.</li>
+ * <li>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 <code>RequestDispatcherOptions</code> object is created
+ * from the name value pair list.</li>
+ * </ul>
+ *
+ * @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 [EMAIL PROTECTED] #OPT_FORCE_RESOURCE_TYPE} option to the given
+ * <code>resourceType</code> if not <code>null</code>.
+ */
+ public void setForceResourceType(String resourceType) {
+ if (resourceType != null) {
+ put(OPT_FORCE_RESOURCE_TYPE, resourceType);
+ }
+ }
+
+ /**
+ * Returns the [EMAIL PROTECTED] #OPT_FORCE_RESOURCE_TYPE} option or
<code>null</code>
+ * if not set.
+ */
+ public String getForceResourceType() {
+ return get(OPT_FORCE_RESOURCE_TYPE);
+ }
+
+ /**
+ * Sets the [EMAIL PROTECTED] #OPT_ADD_SELECTORS} option to the given
+ * <code>additionalSelectors</code> if not <code>null</code>.
+ */
+ public void setAddSelectors(String additionalSelectors) {
+ if (additionalSelectors != null) {
+ put(OPT_ADD_SELECTORS, additionalSelectors);
+ }
+ }
+
+ /**
+ * Returns the [EMAIL PROTECTED] #OPT_ADD_SELECTORS} option or
<code>null</code> if
+ * not set.
+ */
+ public String getAddSelectors() {
+ return get(OPT_ADD_SELECTORS);
+ }
+
+ /**
+ * Sets the [EMAIL PROTECTED] #OPT_REPLACE_SELECTORS} option to the given
+ * <code>replaceSelectors</code> if not <code>null</code>.
+ */
+ public void setReplaceSelectors(String replaceSelectors) {
+ if (replaceSelectors != null) {
+ put(OPT_REPLACE_SELECTORS, replaceSelectors);
+ }
+ }
+
+ /**
+ * Returns the [EMAIL PROTECTED] #OPT_REPLACE_SELECTORS} option or
<code>null</code>
+ * if not set.
+ */
+ public String getReplaceSelectors() {
+ return get(OPT_REPLACE_SELECTORS);
+ }
+
+ /**
+ * Sets the [EMAIL PROTECTED] #OPT_REPLACE_SUFFIX} option to the given
+ * <code>replaceSuffix</code> if not <code>null</code>.
+ */
+ public void setReplaceSuffix(String replaceSuffix) {
+ if (replaceSuffix != null) {
+ put(OPT_REPLACE_SUFFIX, replaceSuffix);
+ }
+ }
+
+ /**
+ * Returns the [EMAIL PROTECTED] #OPT_REPLACE_SUFFIX} option or
<code>null</code> 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 @@
* </pre>
*
* <p>
- * The request dispatcher options provided to this method as a string are
- * internally parsed into an [EMAIL PROTECTED] RequestDispatcherOptions}
object as
- * follows:
- * <ul>
- * <li>If the string neither contains a comma nor an equals sign, the
- * string is assumed to be a resource type. Hence a
- * <code>RequestDispatcherOptions</code> object is created with the
- * [EMAIL PROTECTED] RequestDispatcherOptions#OPT_FORCE_RESOURCE_TYPE}
field set to the
- * string.</li>
- * <li>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 <code>RequestDispatcherOptions</code> object is created
- * from the name value pair list.</li>
- * </ul>
+ * This method creates a <code>RequestDispatcherOptions</code> object by
+ * calling the
+ * [EMAIL PROTECTED]
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 <code>ServletException</code>
* 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());
+ }
+
+}