sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [sling-org-apache-sling-servlets-resolver] 01/01: SLING-8170 - Allow Sling servlets to declare a resource super type
Date Tue, 11 Dec 2018 13:50:22 GMT
This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch issue/SLING-8170
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git

commit 23663221bdfeb1a39dcc131cc30ef9403c09c4fc
Author: Radu Cotescu <radu@apache.org>
AuthorDate: Tue Dec 11 14:49:59 2018 +0100

    SLING-8170 - Allow Sling servlets to declare a resource super type
---
 pom.xml                                            |  8 +++---
 .../internal/resource/ServletResource.java         | 15 +++++++---
 .../internal/resource/ServletResourceProvider.java |  6 ++--
 .../resource/ServletResourceProviderFactory.java   | 32 ++++++++++++++--------
 .../ServletResourceProviderCreateTest.java         | 26 ++++++++++++++++++
 5 files changed, 66 insertions(+), 21 deletions(-)

diff --git a/pom.xml b/pom.xml
index e40c224..60f32cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,9 +7,9 @@
     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
@@ -64,7 +64,7 @@
                         </Import-Package>
                         <Embed-Dependency>
                             org.apache.sling.engine;inline="org/apache/sling/engine/impl/request/SlingRequestPathInfo*",
-                        </Embed-Dependency>                
+                        </Embed-Dependency>
                         <Provide-Capability>
                             osgi.extender;osgi.extender="org.apache.sling.servlets.resolver";version:Version="1.0"
                         </Provide-Capability>
@@ -96,7 +96,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.18.0</version>
+            <version>2.18.5-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <!-- for ServiceUserMapped (SLING-4312) -->
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResource.java
b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResource.java
index 63704f2..41375a5 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResource.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResource.java
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import javax.servlet.Servlet;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.AbstractResource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -38,17 +39,23 @@ class ServletResource extends AbstractResource {
     private final String path;
 
     private final String resourceType;
+    private final String resourceSuperType;
 
     private final ResourceMetadata metadata;
 
+    public ServletResource(ResourceResolver resourceResolver, Servlet servlet, String path)
{
+        this(resourceResolver, servlet, path, null);
+    }
+
     ServletResource(final ResourceResolver resourceResolver,
-            final Servlet servlet,
-            final String path) {
+                    final Servlet servlet,
+                    final String path,
+                    final String resourceSuperType) {
         this.resourceResolver = resourceResolver;
         this.servlet = servlet;
         this.path = path;
         this.resourceType = ServletResourceProviderFactory.ensureServletNameExtension(path);
-
+        this.resourceSuperType = StringUtils.isEmpty(resourceSuperType) ? "sling/bundle/resource"
: resourceSuperType;
         this.metadata = new ResourceMetadata();
     }
 
@@ -71,7 +78,7 @@ class ServletResource extends AbstractResource {
      * the super type. */
     @Override
     public String getResourceSuperType() {
-        return "sling/bundle/resource";
+        return resourceSuperType;
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProvider.java
b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProvider.java
index f0d6bb9..fd284ce 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProvider.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProvider.java
@@ -33,10 +33,12 @@ public class ServletResourceProvider extends ResourceProvider<Object>
{
     private final Servlet servlet;
 
     private final Set<String> resourcePaths;
+    private final String resourceSuperType;
 
-    ServletResourceProvider(final Servlet servlet, final Set<String> resourcePaths)
{
+    ServletResourceProvider(final Servlet servlet, final Set<String> resourcePaths,
final String resourceSuperType) {
         this.servlet = servlet;
         this.resourcePaths = resourcePaths;
+        this.resourceSuperType = resourceSuperType;
     }
 
     @SuppressWarnings("unchecked")
@@ -47,7 +49,7 @@ public class ServletResourceProvider extends ResourceProvider<Object>
{
             final Resource parent) {
         // only return a resource if the servlet has been assigned
         if (resourcePaths.contains(path)) {
-            return new ServletResource(ctx.getResourceResolver(), servlet, path);
+            return new ServletResource(ctx.getResourceResolver(), servlet, path, resourceSuperType);
         }
 
         @SuppressWarnings("rawtypes")
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
index 128695c..8b0e44c 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java
@@ -18,21 +18,13 @@
  */
 package org.apache.sling.servlets.resolver.internal.resource;
 
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_EXTENSIONS;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_METHODS;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_NAME;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PREFIX;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES;
-import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_SELECTORS;
-import static org.osgi.service.component.ComponentConstants.COMPONENT_NAME;
-
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
 import javax.servlet.Servlet;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.commons.osgi.PropertiesUtil;
@@ -42,6 +34,16 @@ import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_EXTENSIONS;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_METHODS;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_NAME;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PREFIX;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_RESOURCE_SUPER_TYPE;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES;
+import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_SELECTORS;
+import static org.osgi.service.component.ComponentConstants.COMPONENT_NAME;
+
 public class ServletResourceProviderFactory {
 
     /**
@@ -144,8 +146,16 @@ public class ServletResourceProviderFactory {
             log.debug("create({}): Registering servlet for paths {}",
                     getServiceReferenceInfo(ref), pathSet);
         }
-
-        return new ServletResourceProvider(servlet, pathSet);
+        String resourceSuperType = PropertiesUtil.toString(ref.getProperty(SLING_SERVLET_RESOURCE_SUPER_TYPE),
"");
+        if (StringUtils.isNotEmpty(resourceSuperType)) {
+            for (String rt : PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES)))
{
+                if (!rt.startsWith("/")) {
+                    rt = getPrefix(ref).concat(ResourceUtil.resourceTypeToPath(rt));
+                }
+                pathSet.add(rt);
+            }
+        }
+        return new ServletResourceProvider(servlet, pathSet, resourceSuperType);
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderCreateTest.java
b/src/test/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderCreateTest.java
index 7f508ed..9f09fbc 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderCreateTest.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderCreateTest.java
@@ -19,6 +19,7 @@
 package org.apache.sling.servlets.resolver.internal.resource;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
@@ -29,10 +30,15 @@ import javax.servlet.Servlet;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.ServletResolverConstants;
+import org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext;
+import org.apache.sling.spi.resource.provider.ResolveContext;
+import org.apache.sling.spi.resource.provider.ResourceContext;
 import org.junit.Test;
+import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
@@ -179,4 +185,24 @@ public class ServletResourceProviderCreateTest {
             + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION));
     }
 
+    @Test
+    public void testCreateWithResourceSuperType() {
+        final ServiceReference<Servlet> msr = Mockito.mock(ServiceReference.class);
+        Mockito.when(msr.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
+        Mockito.when(msr.getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES)).thenReturn(RES_TYPE);
+        Mockito.when(msr.getProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS)).thenReturn(new
String[] {"html"});
+        Mockito.when(msr.getProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_SUPER_TYPE)).thenReturn(new
String[] {"this/is/a" +
+                "/test", "resource/two"});
+        final ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
+        final Set<String> paths = srp.getServletPaths();
+        assertEquals(2, paths.size());
+        assertTrue(paths.contains(ROOT + RES_TYPE_PATH));
+        assertTrue(paths.contains(ROOT + RES_TYPE_PATH + "/html" + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION));
+        Resource servletResource = srp.getResource(Mockito.mock(ResolveContext.class), "/apps/sling/sample",
+                Mockito.mock(ResourceContext.class), Mockito.mock(Resource.class));
+        assertNotNull(servletResource);
+        assertEquals("this/is/a/test", servletResource.getResourceSuperType());
+
+    }
+
 }


Mime
View raw message