SLING-5726 reverting r1743648
authorKonrad Windszus <kwin@apache.org>
Fri, 13 May 2016 11:23:07 +0000 (11:23 +0000)
committerKonrad Windszus <kwin@apache.org>
Fri, 13 May 2016 11:23:07 +0000 (11:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1743654 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/injectors/AbstractInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java
src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java
src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathAllOptionalModel.java

index cce4938..8ef3797 100644 (file)
@@ -44,30 +44,12 @@ abstract class AbstractInjector {
         return resolver;
     }
 
-    /** 
-     * Retrieve the ValueMap from the given adaptable. This succeeds, if the adaptable is either
-     * <ul>
-     * <li>a {@link ValueMap},</li>
-     * <li>a {@link SlingHttpServletRequest}, in which case the returned {@link ValueMap} is the one derived from the request's resource or</li>
-     * <li>adaptable to a {@link ValueMap}.</li>
-     * </ul>
-     * Otherwise {@code null} is returned.
-     * @param adaptable
-     * @return a ValueMap or {@code null}.
-     */
     protected ValueMap getValueMap(Object adaptable) {
         if (adaptable instanceof ValueMap) {
             return (ValueMap) adaptable;
-        } else if (adaptable instanceof SlingHttpServletRequest) {
-            final Resource resource = ((SlingHttpServletRequest)adaptable).getResource();
-            // resource may be null for mocked adaptables, therefore do a check here
-            if (resource != null) {
-                return resource.adaptTo(ValueMap.class);
-            } else {
-                return null;
-            }
         } else if (adaptable instanceof Adaptable) {
-            return ((Adaptable) adaptable).adaptTo(ValueMap.class);
+            ValueMap map = ((Adaptable) adaptable).adaptTo(ValueMap.class);
+            return map;
         } else {
             return null;
         }
index ebc7db0..330ba5b 100644 (file)
@@ -84,7 +84,6 @@ public class ResourcePathInjector extends AbstractInjector implements Injector,
 
         ResourceResolver resolver = getResourceResolver(adaptable);
         if (resolver == null) {
-            LOG.debug("Could not get resolver from adaptable {}", adaptable);
             return null;
         }
         List<Resource> resources = getResources(resolver, resourcePaths, name);
index bbb1e07..c76a553 100644 (file)
@@ -169,7 +169,12 @@ public class ValueMapInjector extends AbstractInjector implements Injector, Inje
             if (StringUtils.isNotBlank(annotation.via())) {
                 return annotation.via();
             }
-            return null;
+            // automatically go via resource, if this is the httprequest
+            if (adaptable instanceof SlingHttpServletRequest) {
+                return "resource";
+            } else {
+                return null;
+            }
         }
         
         @Override
index 681ccd5..6a3e6ab 100644 (file)
@@ -57,10 +57,10 @@ public class ResourcePathInjectionTest {
     private ModelAdapterFactory factory;
 
     @Mock
-    private Resource adaptableResource;
+    private Resource adaptable;
     
     @Mock
-    SlingHttpServletRequest adaptableRequest;
+    SlingHttpServletRequest nonResourceAdaptable;
 
     @Mock
     private Resource byPathResource;
@@ -98,16 +98,13 @@ public class ResourcePathInjectionTest {
         when(componentCtx.getBundleContext()).thenReturn(bundleContext);
         when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
 
-        when(adaptableResource.getResourceResolver()).thenReturn(resourceResolver);
-        when(adaptableResource.adaptTo(ValueMap.class)).thenReturn(properties);
+        when(adaptable.getResourceResolver()).thenReturn(resourceResolver);
+        when(adaptable.adaptTo(ValueMap.class)).thenReturn(properties);
 
         when(resourceResolver.getResource("/some/path")).thenReturn(byPathResource);
         when(resourceResolver.getResource("/some/path2")).thenReturn(byPathResource2);
         when(resourceResolver.getResource("/some/other/path")).thenReturn(byPropertyValueResource);
         when(resourceResolver.getResource("/some/other/path2")).thenReturn(byPropertyValueResource2);
-        
-        when(adaptableRequest.getResource()).thenReturn(byPathResource);
-        when(adaptableRequest.getResourceResolver()).thenReturn(resourceResolver);
 
         factory = new ModelAdapterFactory();
         factory.activate(componentCtx);
@@ -119,8 +116,8 @@ public class ResourcePathInjectionTest {
     }
 
     @Test
-    public void testPathInjectionFromResource() {
-        ResourcePathModel model = factory.getAdapter(adaptableResource, ResourcePathModel.class);
+    public void testPathInjection() {
+        ResourcePathModel model = factory.getAdapter(adaptable, ResourcePathModel.class);
         assertNotNull(model);
         assertEquals(byPathResource, model.getFromPath());
         assertEquals(byPropertyValueResource, model.getByDerefProperty());
@@ -129,20 +126,15 @@ public class ResourcePathInjectionTest {
     }
 
     @Test
-    public void testPathInjectionFromRequest() {
-        // return the same properties through this request's resource, as through adaptableResource
-        doReturn(adaptableResource.adaptTo(ValueMap.class)).when(byPathResource).adaptTo(ValueMap.class);
-        ResourcePathModel model = factory.getAdapter(adaptableRequest, ResourcePathModel.class);
-        assertNotNull(model);
-        assertEquals(byPathResource, model.getFromPath());
-        assertEquals(byPropertyValueResource, model.getByDerefProperty());
-        assertEquals(byPathResource2, model.getFromPath2());
-        assertEquals(byPropertyValueResource2, model.getByDerefProperty2());
+    public void testPathInjectionWithNonResourceAdaptable() {
+        ResourcePathModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathModel.class);
+        // should be null because mandatory fields could not be injected
+        assertNull(model);
     }
 
     @Test
     public void testOptionalPathInjectionWithNonResourceAdaptable() {
-        ResourcePathAllOptionalModel model = factory.getAdapter(adaptableRequest, ResourcePathAllOptionalModel.class);
+        ResourcePathAllOptionalModel model = factory.getAdapter(nonResourceAdaptable, ResourcePathAllOptionalModel.class);
         // should not be null because resource paths fields are optional
         assertNotNull(model);
         // but the field itself are null
@@ -154,7 +146,7 @@ public class ResourcePathInjectionTest {
 
     @Test
     public void testMultiplePathInjection() {
-        ResourcePathModel model = factory.getAdapter(adaptableResource, ResourcePathModel.class);
+        ResourcePathModel model = factory.getAdapter(adaptable, ResourcePathModel.class);
         assertNotNull(model);
         List<Resource> resources=model.getMultipleResources();
         assertNotNull(resources);
@@ -180,16 +172,16 @@ public class ResourcePathInjectionTest {
     public void testPartialInjectionFailure1() {
         when(resourceResolver.getResource("/some/other/path")).thenReturn(null);
         
-        ResourcePathPartialModel model = factory.getAdapter(adaptableResource, ResourcePathPartialModel.class);
+        ResourcePathPartialModel model = factory.getAdapter(adaptable, ResourcePathPartialModel.class);
         assertNull(model);
     }
 
     @Test
-    public void testPartialInjectionFailure2() {
+    public void testPartialInjectionFailure2() {       
         when(resourceResolver.getResource("/some/other/path")).thenReturn(null);
         when(resourceResolver.getResource("/some/other/path2")).thenReturn(null);
         
-        ResourcePathPartialModel model = factory.getAdapter(adaptableResource, ResourcePathPartialModel.class);
+        ResourcePathPartialModel model = factory.getAdapter(adaptable, ResourcePathPartialModel.class);
         assertNull(model);
     }
 
index 38fc7f9..a7dcb62 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.sling.models.annotations.injectorspecific.ResourcePath;
 public class ResourcePathAllOptionalModel {
 
     @Inject
-    @Path("/some/invalidpath")
+    @Path("/some/path")
     @Optional
     private Resource fromPath;
 
@@ -42,11 +42,11 @@ public class ResourcePathAllOptionalModel {
     private Resource derefProperty;
     
     @Inject
-    @Path(paths={"/some/invalidpath", "/some/invalidpath2"})
+    @Path(paths={"/some/path", "/some/path2"})
     @Optional
     private Resource manyFromPathNonList;
 
-    @ResourcePath(path = "/some/invalidpath2", optional=true)
+    @ResourcePath(path = "/some/path2", optional=true)
     private Resource fromPath2;
 
     @ResourcePath(name = "anotherPropertyContainingAPath", optional=true)