You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:00:21 UTC

[sling-org-apache-sling-resourceresolver] 03/10: SLING-2665 - allow the default redirect status code to be configured

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

rombert pushed a commit to annotated tag org.apache.sling.resourceresolver-1.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git

commit 62fe67a08fb2950671168526e36d59fcf2ca32a5
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Wed Nov 14 21:42:35 2012 +0000

    SLING-2665 - allow the default redirect status code to be configured
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/resourceresolver@1409439 13f79535-47bb-0310-9956-ffa450edef68
---
 .../impl/ResourceResolverFactoryActivator.java     | 11 ++++
 .../impl/ResourceResolverFactoryImpl.java          |  4 ++
 .../impl/mapping/MapConfigurationProvider.java     |  2 +
 .../resourceresolver/impl/mapping/MapEntries.java  |  8 ++-
 .../OSGI-INF/metatype/metatype.properties          |  7 ++-
 .../impl/mapping/MapEntriesTest.java               | 73 ++++++++++++++++++++--
 6 files changed, 96 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
index 6581290..cf019a8 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
@@ -119,6 +119,9 @@ public class ResourceResolverFactoryActivator {
     @Property(value = MapEntries.DEFAULT_MAP_ROOT)
     private static final String PROP_MAP_LOCATION = "resource.resolver.map.location";
 
+    @Property(intValue = MapEntries.DEFAULT_DEFAULT_VANITY_PATH_REDIRECT_STATUS)
+    private static final String PROP_DEFAULT_VANITY_PATH_REDIRECT_STATUS = "resource.resolver.default.vanity.redirect.status";
+
     /** Tracker for the resource decorators. */
     private final ResourceDecoratorTracker resourceDecoratorTracker = new ResourceDecoratorTracker();
 
@@ -156,6 +159,8 @@ public class ResourceResolverFactoryActivator {
     /** ComponentContext */
     private ComponentContext componentContext;
 
+    private int defaultVanityPathRedirectStatus;
+
     private final FactoryPreconditions preconds = new FactoryPreconditions();
 
     /**
@@ -203,6 +208,10 @@ public class ResourceResolverFactoryActivator {
         return mapRoot;
     }
 
+    public int getDefaultVanityPathRedirectStatus() {
+        return defaultVanityPathRedirectStatus;
+    }
+
     // ---------- SCR Integration ---------------------------------------------
 
     /** Activates this component, called by SCR before registering as a service */
@@ -263,6 +272,8 @@ public class ResourceResolverFactoryActivator {
         // the root of the resolver mappings
         mapRoot = PropertiesUtil.toString(properties.get(PROP_MAP_LOCATION), MapEntries.DEFAULT_MAP_ROOT);
 
+        defaultVanityPathRedirectStatus = PropertiesUtil.toInteger(properties.get(PROP_DEFAULT_VANITY_PATH_REDIRECT_STATUS), MapEntries.DEFAULT_DEFAULT_VANITY_PATH_REDIRECT_STATUS);
+
         final BundleContext bc = componentContext.getBundleContext();
 
         // check for required property
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java
index 5565d72..7d1f2f8 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java
@@ -161,4 +161,8 @@ public class ResourceResolverFactoryImpl implements ResourceResolverFactory, Map
     public RootResourceProviderEntry getRootProviderEntry() {
         return this.activator.getRootProviderEntry();
     }
+
+    public int getDefaultVanityPathRedirectStatus() {
+        return this.activator.getDefaultVanityPathRedirectStatus();
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
index 28767e2..3990bf0 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
@@ -35,4 +35,6 @@ public interface MapConfigurationProvider extends ResourceResolverFactory {
 
     Mapping[] getMappings();
 
+    int getDefaultVanityPathRedirectStatus();
+
 }
diff --git a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 098a2b5..1c22135 100644
--- a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -77,6 +77,8 @@ public class MapEntries implements EventHandler {
 
     public static final String DEFAULT_MAP_ROOT = "/etc/map";
 
+    public static final int DEFAULT_DEFAULT_VANITY_PATH_REDIRECT_STATUS = HttpServletResponse.SC_FOUND;
+
     private static final String JCR_SYSTEM_PREFIX = "/jcr:system/";
 
     static final String ANY_SCHEME_HOST = "[^/]+/[^/]+";
@@ -569,11 +571,11 @@ public class MapEntries implements EventHandler {
                         final String redirect = redirectTarget.getPath();
                         final String redirectName = redirectTarget.getName();
 
-                        // whether the target is attained by a 302/FOUND or by an
-                        // internal redirect is defined by the sling:redirect
+                        // whether the target is attained by a external redirect or
+                        // by an internal redirect is defined by the sling:redirect
                         // property
                         final int status = props.get("sling:redirect", false) ? props.get(
-                                        PROP_REDIRECT_EXTERNAL_REDIRECT_STATUS, HttpServletResponse.SC_FOUND)
+                                        PROP_REDIRECT_EXTERNAL_REDIRECT_STATUS, factory.getDefaultVanityPathRedirectStatus())
                                         : -1;
 
                         final String checkPath = result[1];
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
index 6512548..1b820a5 100644
--- a/src/main/resources/OSGI-INF/metatype/metatype.properties
+++ b/src/main/resources/OSGI-INF/metatype/metatype.properties
@@ -76,4 +76,9 @@ resource.resolver.required.providers.name = Required Providers
 resource.resolver.required.providers.description = A resource resolver factory is only \
  available (registered) if all resource providers mentioned in this configuration \
  are available. Each entry is either a service PID or a filter expression. \
- Invalid filters are ignored.
\ No newline at end of file
+ Invalid filters are ignored.
+
+resource.resolver.default.vanity.redirect.status.name = Default Vanity Path Redirect Status
+resource.resolver.default.vanity.redirect.status.description = The default status code \
+ used when a sling:vanityPath is configured to redirect and does not have a specific \
+ status code associated with it (via a sling:redirectStatus property)
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
index a3677ef..8f9540a 100644
--- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
+++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
@@ -20,9 +20,12 @@ import static org.junit.Assert.*;
 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.sling.api.resource.Resource;
@@ -75,7 +78,7 @@ public class MapEntriesTest {
         when(result.getParent()).thenReturn(parent);
         when(result.getPath()).thenReturn("/parent/child");
         when(result.getName()).thenReturn("child");
-        when(result.adaptTo(ValueMap.class)).thenReturn(singletonValueMap("sling:alias", "alias"));
+        when(result.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:alias", "alias"));
 
         when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
 
@@ -105,13 +108,13 @@ public class MapEntriesTest {
         when(result.getParent()).thenReturn(parent);
         when(result.getPath()).thenReturn("/parent/child");
         when(result.getName()).thenReturn("child");
-        when(result.adaptTo(ValueMap.class)).thenReturn(singletonValueMap("sling:alias", "alias"));
+        when(result.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:alias", "alias"));
 
         final Resource secondResult = mock(Resource.class);
         when(secondResult.getParent()).thenReturn(parent);
         when(secondResult.getPath()).thenReturn("/parent/child2");
         when(secondResult.getName()).thenReturn("child2");
-        when(secondResult.adaptTo(ValueMap.class)).thenReturn(singletonValueMap("sling:alias", "alias"));
+        when(secondResult.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:alias", "alias"));
 
         when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
 
@@ -132,8 +135,68 @@ public class MapEntriesTest {
         assertEquals("child", aliasMap.get("alias"));
     }
 
-    private ValueMap singletonValueMap(String key, String value) {
-        return new ValueMapDecorator(Collections.<String, Object> singletonMap(key, value));
+    @Test
+    public void test_vanity_path_registration() {
+        // specifically making this a weird value because we want to verify that
+        // the configuration value is being used
+        int DEFAULT_VANITY_STATUS = 333333;
+
+        when(resourceResolverFactory.getDefaultVanityPathRedirectStatus()).thenReturn(DEFAULT_VANITY_STATUS);
+
+        final List<Resource> resources = new ArrayList<Resource>();
+
+        Resource justVanityPath = mock(Resource.class);
+        when(justVanityPath.getPath()).thenReturn("/justVanityPath");
+        when(justVanityPath.getName()).thenReturn("justVanityPath");
+        when(justVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath"));
+        resources.add(justVanityPath);
+
+        Resource redirectingVanityPath = mock(Resource.class);
+        when(redirectingVanityPath.getPath()).thenReturn("/redirectingVanityPath");
+        when(redirectingVanityPath.getName()).thenReturn("redirectingVanityPath");
+        when(redirectingVanityPath.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/redirectingVanityPath", "sling:redirect", true));
+        resources.add(redirectingVanityPath);
+
+        Resource redirectingVanityPath301 = mock(Resource.class);
+        when(redirectingVanityPath301.getPath()).thenReturn("/redirectingVanityPath301");
+        when(redirectingVanityPath301.getName()).thenReturn("redirectingVanityPath301");
+        when(redirectingVanityPath301.adaptTo(ValueMap.class)).thenReturn(buildValueMap("sling:vanityPath", "/target/redirectingVanityPath301", "sling:redirect", true, "sling:redirectStatus", 301));
+        resources.add(redirectingVanityPath301);
+
+        when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
+
+            public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
+                if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
+                    return resources.iterator();
+                } else {
+                    return Collections.<Resource> emptySet().iterator();
+                }
+            }
+        });
+
+        mapEntries.doInit();
+
+        List<MapEntry> entries = mapEntries.getResolveMaps();
+        assertEquals(6, entries.size());
+        for (MapEntry entry : entries) {
+            if (entry.getPattern().contains("/target/redirectingVanityPath301")) {
+                assertEquals(301, entry.getStatus());
+                assertFalse(entry.isInternal());
+            } else if (entry.getPattern().contains("/target/redirectingVanityPath")) {
+                assertEquals(DEFAULT_VANITY_STATUS, entry.getStatus());
+                assertFalse(entry.isInternal());
+            } else if (entry.getPattern().contains("/target/justVanityPath")) {
+                assertTrue(entry.isInternal());
+            }
+        }
+    }
+
+    private ValueMap buildValueMap(Object... string) {
+        final Map<String, Object> data = new HashMap<String, Object>();
+        for (int i = 0; i < string.length; i = i + 2) {
+            data.put((String) string[i], string[i+1]);
+        }
+        return new ValueMapDecorator(data);
     }
 
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.