You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2012/11/14 22:42:37 UTC

svn commit: r1409439 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/ main/java/org/apache/sling/resourceresolver/impl/mapping/ main/resources/OSGI-INF/metatype/ test/java/org/apache/sling/resourceresolv...

Author: justin
Date: Wed Nov 14 21:42:35 2012
New Revision: 1409439

URL: http://svn.apache.org/viewvc?rev=1409439&view=rev
Log:
SLING-2665 - allow the default redirect status code to be configured

Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
    sling/trunk/bundles/resourceresolver/src/main/resources/OSGI-INF/metatype/metatype.properties
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java Wed Nov 14 21:42:35 2012
@@ -119,6 +119,9 @@ public class ResourceResolverFactoryActi
     @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 ResourceResolverFactoryActi
     /** ComponentContext */
     private ComponentContext componentContext;
 
+    private int defaultVanityPathRedirectStatus;
+
     private final FactoryPreconditions preconds = new FactoryPreconditions();
 
     /**
@@ -203,6 +208,10 @@ public class ResourceResolverFactoryActi
         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 ResourceResolverFactoryActi
         // 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

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryImpl.java Wed Nov 14 21:42:35 2012
@@ -161,4 +161,8 @@ public class ResourceResolverFactoryImpl
     public RootResourceProviderEntry getRootProviderEntry() {
         return this.activator.getRootProviderEntry();
     }
+
+    public int getDefaultVanityPathRedirectStatus() {
+        return this.activator.getDefaultVanityPathRedirectStatus();
+    }
 }
\ No newline at end of file

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapConfigurationProvider.java Wed Nov 14 21:42:35 2012
@@ -35,4 +35,6 @@ public interface MapConfigurationProvide
 
     Mapping[] getMappings();
 
+    int getDefaultVanityPathRedirectStatus();
+
 }

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java Wed Nov 14 21:42:35 2012
@@ -77,6 +77,8 @@ public class MapEntries implements Event
 
     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 Event
                         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];

Modified: sling/trunk/bundles/resourceresolver/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/resources/OSGI-INF/metatype/metatype.properties (original)
+++ sling/trunk/bundles/resourceresolver/src/main/resources/OSGI-INF/metatype/metatype.properties Wed Nov 14 21:42:35 2012
@@ -76,4 +76,9 @@ resource.resolver.required.providers.nam
 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

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java?rev=1409439&r1=1409438&r2=1409439&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java Wed Nov 14 21:42:35 2012
@@ -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);
     }
 
 }