You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2021/09/29 15:45:32 UTC

[sling-org-apache-sling-api] 01/01: SLING-8742 : Allow overriding the extension when using the RequestDispatcher

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch issues/SLING-8742
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git

commit 85a39643f35cca10e6fd6ee95c8a834ca24861a1
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Sep 29 17:45:07 2021 +0200

    SLING-8742 : Allow overriding the extension when using the RequestDispatcher
---
 .../api/request/RequestDispatcherOptions.java      | 29 +++++++++++++++--
 .../org/apache/sling/api/request/package-info.java |  2 +-
 .../api/request/RequestDispatcherOptionsTest.java  | 36 +++++++++++++++++-----
 3 files changed, 56 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java b/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java
index 929e81d..720be44 100644
--- a/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java
+++ b/src/main/java/org/apache/sling/api/request/RequestDispatcherOptions.java
@@ -34,8 +34,6 @@ import java.util.StringTokenizer;
  *          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.
  */
 public class RequestDispatcherOptions extends HashMap<String, String> {
 
@@ -68,6 +66,13 @@ public class RequestDispatcherOptions extends HashMap<String, String> {
     public static final String OPT_REPLACE_SUFFIX = "replaceSuffix";
 
     /**
+     * When dispatching, replace the {@link RequestPathInfo} extension by the value
+     * provided by this option
+     * @since 2.5.0
+     */
+    public static final String OPT_REPLACE_EXTENSION = "replaceExtension";
+
+    /**
      * Creates an instance with no options set.
      */
     public RequestDispatcherOptions() {
@@ -191,4 +196,24 @@ public class RequestDispatcherOptions extends HashMap<String, String> {
     public String getReplaceSuffix() {
         return get(OPT_REPLACE_SUFFIX);
     }
+
+   /**
+     * Sets the {@link #OPT_REPLACE_EXTENSION} option to the given
+     * <code>replaceExtension</code> if not <code>null</code>.
+     * @param replaceExtension The replace extension
+     */
+    public void setReplaceExtension(String replaceExtension) {
+        if (replaceExtension != null) {
+            put(OPT_REPLACE_EXTENSION, replaceExtension);
+        }
+    }
+
+    /**
+     * Returns the {@link #OPT_REPLACE_EXTENSION} option or <code>null</code> if
+     * not set.
+     * @return The replace extension
+     */
+    public String getReplaceExtension() {
+        return get(OPT_REPLACE_EXTENSION);
+    }
 }
diff --git a/src/main/java/org/apache/sling/api/request/package-info.java b/src/main/java/org/apache/sling/api/request/package-info.java
index 052e56d..eb8d106 100644
--- a/src/main/java/org/apache/sling/api/request/package-info.java
+++ b/src/main/java/org/apache/sling/api/request/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("2.4.4")
+@Version("2.5.0")
 package org.apache.sling.api.request;
 
 import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java b/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java
index e7a6eed..a1ad768 100644
--- a/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java
+++ b/src/test/java/org/apache/sling/api/request/RequestDispatcherOptionsTest.java
@@ -18,22 +18,28 @@
  */
 package org.apache.sling.api.request;
 
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
-public class RequestDispatcherOptionsTest extends TestCase {
+import org.junit.Test;
 
-    public void testNullString() {
+
+public class RequestDispatcherOptionsTest {
+
+    @Test public void testNullString() {
         final RequestDispatcherOptions result = new RequestDispatcherOptions(
             null);
         assertTrue(result.isEmpty());
     }
 
-    public void testEmptyString() {
+    @Test public void testEmptyString() {
         final RequestDispatcherOptions result = new RequestDispatcherOptions("");
         assertTrue(result.isEmpty());
     }
 
-    public void testSingleOption() {
+    @Test public void testSingleOption() {
         final RequestDispatcherOptions result = new RequestDispatcherOptions(
             "forceResourceType= widget");
         assertNotNull(result);
@@ -43,7 +49,7 @@ public class RequestDispatcherOptionsTest extends TestCase {
             result.getForceResourceType());
     }
 
-    public void testResourceTypeSlashShortcut() {
+    @Test public void testResourceTypeSlashShortcut() {
         // a single option with no comma or colon means "forceResourceType"
         final RequestDispatcherOptions result = new RequestDispatcherOptions(
             "\t components/widget  ");
@@ -55,7 +61,7 @@ public class RequestDispatcherOptionsTest extends TestCase {
             "components/widget", result.getForceResourceType());
     }
 
-    public void testResourceTypeColonShortcut() {
+    @Test public void testResourceTypeColonShortcut() {
         // a single option with no comma or colon means "forceResourceType"
         final RequestDispatcherOptions result = new RequestDispatcherOptions(
             "\t components:widget  ");
@@ -67,7 +73,7 @@ public class RequestDispatcherOptionsTest extends TestCase {
             "components:widget", result.getForceResourceType());
     }
 
-    public void testTwoOptions() {
+    @Test public void testTwoOptions() {
         final RequestDispatcherOptions result = new RequestDispatcherOptions(
             "forceResourceType= components:widget, replaceSelectors = xyz  ,");
         assertNotNull(result);
@@ -81,4 +87,18 @@ public class RequestDispatcherOptionsTest extends TestCase {
         assertEquals("Expected option found (" + result + ")", "xyz",
             result.getReplaceSelectors());
     }
+
+    @Test public void testReplaceExtensionSetterGetter() {
+        final RequestDispatcherOptions result = new RequestDispatcherOptions();
+        assertNull(result.getReplaceExtension());
+        result.setReplaceExtension("foo");
+        assertEquals("foo", result.getReplaceExtension());
+        result.setReplaceExtension(null);
+        assertEquals("foo", result.getReplaceExtension());
+    } 
+
+    @Test public void testReplaceExtensionConstructor() {
+        final RequestDispatcherOptions result = new RequestDispatcherOptions("replaceExtension=foo");
+        assertEquals("foo", result.getReplaceExtension());
+    } 
 }