sling-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (SLING-7321) ChildResourceViaProvider should be able to deal with SlingHttpServletRequest
Date Thu, 21 Dec 2017 17:16:00 GMT

    [ https://issues.apache.org/jira/browse/SLING-7321?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16300301#comment-16300301
] 

ASF GitHub Bot commented on SLING-7321:
---------------------------------------

justinedelson closed pull request #3: SLING-7321 ChildResourceViaProvider support wrapping
request
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/3
 
 
   

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/src/main/java/org/apache/sling/models/impl/via/ChildResourceViaProvider.java
b/src/main/java/org/apache/sling/models/impl/via/ChildResourceViaProvider.java
index b35b9e0..f80a1a0 100644
--- a/src/main/java/org/apache/sling/models/impl/via/ChildResourceViaProvider.java
+++ b/src/main/java/org/apache/sling/models/impl/via/ChildResourceViaProvider.java
@@ -19,15 +19,21 @@
 import org.apache.commons.lang3.StringUtils;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
 import org.apache.sling.models.annotations.ViaProviderType;
 import org.apache.sling.models.annotations.via.ChildResource;
 import org.apache.sling.models.spi.ViaProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Component
 @Service
 public class ChildResourceViaProvider implements ViaProvider {
 
+    private static final Logger log = LoggerFactory.getLogger(ChildResourceViaProvider.class);
+
     @Override
     public Class<? extends ViaProviderType> getType() {
         return ChildResource.class;
@@ -40,8 +46,36 @@ public Object getAdaptable(Object original, String value) {
         }
         if (original instanceof Resource) {
             return ((Resource) original).getChild(value);
+        } else if (original instanceof SlingHttpServletRequest) {
+            final SlingHttpServletRequest request = (SlingHttpServletRequest) original;
+            final Resource resource = request.getResource();
+            if (resource == null) {
+                return null;
+            }
+            Resource child = resource.getChild(value);
+            if (child == null) {
+                log.debug("Could not obtain child {} of resource {}", value, resource.getPath());
+                return null;
+            }
+            return new ChildResourceRequestWrapper(request, child);
         } else {
+            log.warn("Received unexpected adaptable of type {}.", original.getClass().getName());
             return null;
         }
     }
+
+    private class ChildResourceRequestWrapper extends SlingHttpServletRequestWrapper {
+
+        private final Resource resource;
+
+        private ChildResourceRequestWrapper(SlingHttpServletRequest request, Resource resource)
{
+            super(request);
+            this.resource = resource;
+        }
+
+        @Override
+        public Resource getResource() {
+            return resource;
+        }
+    }
 }
diff --git a/src/test/java/org/apache/sling/models/impl/via/ChildResourceViaProviderTest.java
b/src/test/java/org/apache/sling/models/impl/via/ChildResourceViaProviderTest.java
new file mode 100644
index 0000000..7f32b6e
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/impl/via/ChildResourceViaProviderTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.impl.via;
+
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ChildResourceViaProviderTest {
+
+    private ChildResourceViaProvider provider = new ChildResourceViaProvider();
+
+    @Mock
+    private Resource resource;
+
+    @Mock
+    private Resource childResource;
+
+    @Mock
+    private SlingHttpServletRequest request;
+
+    @Before
+    public void init() {
+        when(resource.getChild("child")).thenReturn(childResource);
+        when(request.getResource()).thenReturn(resource);
+    }
+
+    @Test
+    public void testResource() {
+        Object adaptable = provider.getAdaptable(resource, "child");
+        Assert.assertEquals(adaptable, childResource);
+    }
+
+    @Test
+    public void testRequest() {
+        Object adaptable = provider.getAdaptable(request, "child");
+        Resource adaptableResource = ((SlingHttpServletRequest) adaptable).getResource();
+        Assert.assertEquals(adaptableResource, childResource);
+    }
+
+}


 

----------------------------------------------------------------
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:
users@infra.apache.org


> ChildResourceViaProvider should be able to deal with SlingHttpServletRequest
> ----------------------------------------------------------------------------
>
>                 Key: SLING-7321
>                 URL: https://issues.apache.org/jira/browse/SLING-7321
>             Project: Sling
>          Issue Type: Improvement
>          Components: Extensions
>    Affects Versions: Sling Models Impl 1.4.6
>            Reporter: Santiago GarcĂ­a Pimentel
>            Priority: Minor
>             Fix For: Sling Models Impl 1.4.8
>
>
> The ForcedResourceType and ResourceSuperType are able to deal with Sling requests by
creating a wrapper request that returns a wrapped Request.
> Still, the ChildResourceViaProvider does not offer such possibility. I think that for
sake of consistency it should.
> I propose that if the adaptable is a SlingHttpServletRequest the ChildResourceViaProvider
 should create a request wrapper that returns the child as given in 'value'



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message