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 2017/12/21 17:15:47 UTC
[sling-org-apache-sling-models-impl] branch master updated:
SLING-7321 ChildResourceViaProvider support wrapping request (#3)
This is an automated email from the ASF dual-hosted git repository.
justin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
The following commit(s) were added to refs/heads/master by this push:
new 74e4f06 SLING-7321 ChildResourceViaProvider support wrapping request (#3)
74e4f06 is described below
commit 74e4f0697d1a2e7c151a0a0c43e2c460d76d3a03
Author: Santiago GarcĂa Pimentel <sa...@gmail.com>
AuthorDate: Thu Dec 21 18:15:46 2017 +0100
SLING-7321 ChildResourceViaProvider support wrapping request (#3)
* SLING-7321 ChildResourceViaProvider support wrapping request
* SLING-7321 formatting
---
.../models/impl/via/ChildResourceViaProvider.java | 34 ++++++++++++
.../impl/via/ChildResourceViaProviderTest.java | 63 ++++++++++++++++++++++
2 files changed, 97 insertions(+)
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 @@ package org.apache.sling.models.impl.via;
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 class ChildResourceViaProvider implements ViaProvider {
}
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);
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].