rombert closed pull request #2: Adding a new option to make adding the suffix 
to the include optional
URL: https://github.com/apache/sling-org-apache-sling-dynamic-include/pull/2
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/README.md b/README.md
index 0133558..1e3cdca 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,7 @@ Filter is delivered as a standard OSGi bundle. SDI is 
configured via the configu
 * **Required header** - SDI will be enabled only if the configured header is 
present in the request. By default it's `Server-Agent=Communique-Dispatcher` 
header, added by the AEM dispatcher. You may enter just the header name only or 
the name and the value split with `=`.
 * **Ignore URL params** - SDI normally skips all requests containing any GET 
parameters. This option allows to set a list of parameters that should be 
ignored in the test. See the [Ignoring URL 
parameters](https://docs.adobe.com/docs/en/dispatcher/disp-config.html#Ignoring%20URL%20Parameters)
 section in the dispatcher documentation.
 * **Include path rewriting** -- enable rewriting link (according to sling 
mappings) that is used for dynamic content including.
+* **Append suffix** -- ensures that the suffix of the parent request is 
included with the dynamic include.
 
 ## Compatibility with components
 
diff --git a/src/main/java/org/apache/sling/dynamicinclude/Configuration.java 
b/src/main/java/org/apache/sling/dynamicinclude/Configuration.java
index 027dae6..72579f6 100755
--- a/src/main/java/org/apache/sling/dynamicinclude/Configuration.java
+++ b/src/main/java/org/apache/sling/dynamicinclude/Configuration.java
@@ -62,6 +62,7 @@
     @Property(name = Configuration.PROPERTY_REQUIRED_HEADER, value = 
Configuration.DEFAULT_REQUIRED_HEADER, label = "Required header", description = 
"SDI will work only for requests with given header"),
     @Property(name = Configuration.PROPERTY_IGNORE_URL_PARAMS, cardinality = 
Integer.MAX_VALUE, label = "Ignore URL params", description = "SDI will process 
the request even if it contains configured GET parameters"),
     @Property(name = Configuration.PROPERTY_REWRITE_PATH, boolValue = 
Configuration.DEFAULT_REWRITE_DISABLED, label = "Include path rewriting", 
description = "Check to enable include path rewriting"),
+    @Property(name = Configuration.PROPERTY_APPEND_SUFFIX, boolValue = 
Configuration.DEFAULT_APPEND_SUFFIX, label = "Append suffix to dynamic 
includes", description = "Check to append the suffix of the parent request to 
the dynamic include."),
     @Property(name= Configuration.NAME_HINT_PROPERTY_NAME, 
value=Configuration.NAME_HINT_VALUE)})
 public class Configuration {
 
@@ -110,6 +111,10 @@
 
   static final boolean DEFAULT_REWRITE_DISABLED = false;
 
+  static final String PROPERTY_APPEND_SUFFIX = 
"include-filter.config.appendSuffix";
+
+  static final boolean DEFAULT_APPEND_SUFFIX = true;
+
   private PathMatcher pathMatcher;
 
   private boolean isEnabled;
@@ -132,6 +137,8 @@
 
   private boolean rewritePath;
 
+  private boolean appendSuffix;
+
   @Activate
   public void activate(ComponentContext context, Map<String, ?> properties) {
     isEnabled = 
PropertiesUtil.toBoolean(properties.get(PROPERTY_FILTER_ENABLED), 
DEFAULT_FILTER_ENABLED);
@@ -155,6 +162,7 @@ public void activate(ComponentContext context, Map<String, 
?> properties) {
     ignoreUrlParams = 
Arrays.asList(PropertiesUtil.toStringArray(properties.get(PROPERTY_IGNORE_URL_PARAMS),
         new String[0]));
     rewritePath = 
PropertiesUtil.toBoolean(properties.get(PROPERTY_REWRITE_PATH), 
DEFAULT_REWRITE_DISABLED);
+    appendSuffix = 
PropertiesUtil.toBoolean(properties.get(PROPERTY_APPEND_SUFFIX), 
DEFAULT_APPEND_SUFFIX);
   }
 
   private PathMatcher choosePathMatcher(String pathPattern) {
@@ -229,4 +237,8 @@ public String getRequiredHeader() {
   public boolean isRewritePath() {
     return rewritePath;
   }
+
+  public boolean isAppendSuffix() {
+      return appendSuffix;
+  }
 }
diff --git a/src/main/java/org/apache/sling/dynamicinclude/impl/UrlBuilder.java 
b/src/main/java/org/apache/sling/dynamicinclude/impl/UrlBuilder.java
index 9077cae..1905866 100644
--- a/src/main/java/org/apache/sling/dynamicinclude/impl/UrlBuilder.java
+++ b/src/main/java/org/apache/sling/dynamicinclude/impl/UrlBuilder.java
@@ -47,7 +47,9 @@ public static String buildUrl(String includeSelector, String 
resourceType, boole
                 builder.append('.').append(config.getExtension());
             }
         } else {
-            builder.append(StringUtils.defaultString(pathInfo.getSuffix()));
+            if (config.isAppendSuffix()) {
+                
builder.append(StringUtils.defaultString(pathInfo.getSuffix()));
+            }
         }
         return builder.toString();
     }
diff --git 
a/src/test/java/org/apache/sling/dynamicinclude/impl/UrlBuilderTest.java 
b/src/test/java/org/apache/sling/dynamicinclude/impl/UrlBuilderTest.java
index ccf6e67..c84e7b1 100644
--- a/src/test/java/org/apache/sling/dynamicinclude/impl/UrlBuilderTest.java
+++ b/src/test/java/org/apache/sling/dynamicinclude/impl/UrlBuilderTest.java
@@ -30,8 +30,7 @@
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.*;
 
 @RunWith(MockitoJUnitRunner.class)
 public class UrlBuilderTest {
@@ -107,6 +106,35 @@ public void shouldAppendSuffixForSyntheticResources() {
 
         assertThat(actualResult, 
is("/resource/path.foo.include.html/apps/example/resource/type"));
     }
+
+    @Test
+    public void shouldAppendSuffixWhenRequestedByDefault() {
+        givenAnHtmlRequestForResource("/resource/path");
+        withSelectorString("foo.include");
+        withSuffixString("/suffix/to/some/other/information");
+        boolean isSyntheticResource = false;
+
+        when(config.isAppendSuffix()).thenReturn(true);
+
+        String actualResult = UrlBuilder.buildUrl("include", 
"apps/example/resource/type", isSyntheticResource, config, requestPathInfo);
+
+        assertThat(actualResult, 
is("/resource/path.foo.include.html/suffix/to/some/other/information"));
+    }
+
+    @Test
+    public void shouldNotAppendSuffixWhenConfigured() {
+        givenAnHtmlRequestForResource("/resource/path");
+        withSelectorString("foo.include");
+        withSuffixString("/suffix/to/some/other/information");
+        boolean isSyntheticResource = false;
+
+        when(config.isAppendSuffix()).thenReturn(false);
+
+        String actualResult = UrlBuilder.buildUrl("include", 
"apps/example/resource/type", isSyntheticResource, config, requestPathInfo);
+
+        verify(requestPathInfo,times(0)).getSuffix();
+        assertThat(actualResult, is("/resource/path.foo.include.html"));
+    }
     
     @Test
     public void shouldAppendExtensionForSyntheticResources() {
@@ -132,4 +160,8 @@ private void withSelectorString(String selectorString) {
         when(requestPathInfo.getSelectorString()).thenReturn(selectorString);
         
when(requestPathInfo.getSelectors()).thenReturn(StringUtils.defaultString(selectorString).split("\\."));
     }
+
+    private void withSuffixString(String suffixString) {
+        when(requestPathInfo.getSuffix()).thenReturn(suffixString);
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to