sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-resource-editor] 05/50: SLING-4173 - switch to ResourceProvider - patch contributed by Sandro Boehme, thanks!
Date Wed, 18 Oct 2017 23:23:21 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-editor.git

commit 12b1c743fca2b0527d0eb6584564761afc7ef762
Author: Bertrand Delacretaz <bdelacretaz@apache.org>
AuthorDate: Fri Nov 21 14:55:30 2014 +0000

    SLING-4173 - switch to ResourceProvider - patch contributed by Sandro Boehme, thanks!
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1640927 13f79535-47bb-0310-9956-ffa450edef68
---
 .../resource/ResEditorResourceProvider.java        | 80 ++++++++++++++++++++
 .../resource/ResourceIteratorWrapper.java          | 30 ++++++++
 .../ResourceProviderBasedResourceDecorator.java    | 85 ++++++++++++++++++++++
 .../resource/ResourceTypeResourceWrapper.java      | 32 ++++++++
 4 files changed, 227 insertions(+)

diff --git a/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java
b/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java
new file mode 100644
index 0000000..edc4b42
--- /dev/null
+++ b/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java
@@ -0,0 +1,80 @@
+/*
+ * 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.reseditor.resource;
+
+import java.util.Iterator;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
+
+/**
+ * The Resource Provider that wraps all Resources under {@link ROOT_PATHELEMENT_NAME} with
the {@link ResourceTypeResourceWrapper}.
+ *
+ */
+@Component
+@Service
+@Properties({
+    @Property(name=ResourceProvider.ROOTS, value=ResEditorResourceProvider.ROOT_PATHELEMENT_NAME)
+})
+public class ResEditorResourceProvider implements ResourceProvider{
+    public static final String ROOT_PATHELEMENT_NAME = "reseditor";
+    public static final String ABS_ROOT = "/" + ROOT_PATHELEMENT_NAME;
+    public static final String RESOURCE_EDITOR_PROVIDER_RESOURCE = "resource-editor.RESOURCE_EDITOR_PROVIDER_RESOURCE";
+    public static final String RESEDITOR_RESOURCE_TYPE = "sling/resource-editor";
+
+    
+    /** ResourceProvider interface */
+    public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String
path) {
+        // Synthetic resource for the root, so that /reseditor works
+        if((ABS_ROOT).equals(path)) {
+            return new SyntheticResource(resolver, path, ROOT_PATHELEMENT_NAME);
+        }
+        Resource originalResource = resolver.resolve(req, path.substring(ROOT_PATHELEMENT_NAME.length()));
+        Resource newResource = new ResourceTypeResourceWrapper(originalResource);
+        return newResource;
+    }
+
+    /** ResourceProvider interface */
+    public Resource getResource(ResourceResolver resolver, String path) {
+        // Synthetic resource for the root, so that /reseditor works
+        if((ABS_ROOT).equals(path)) {
+            return new SyntheticResource(resolver, path, ROOT_PATHELEMENT_NAME);
+        }
+        Resource originalResource = resolver.resolve(path.substring(ROOT_PATHELEMENT_NAME.length()+1));
+        Resource newResource = new ResourceTypeResourceWrapper(originalResource);
+        return newResource;
+    }
+
+    /** ResourceProvider interface */
+    public Iterator<Resource> listChildren(Resource parent) {
+    	ResourceResolver resourceResolver = parent.getResourceResolver();
+    	Resource originalResource = resourceResolver.resolve("/");
+        Resource newResource = new ResourceTypeResourceWrapper(originalResource);
+    	return newResource.listChildren();
+    }
+    
+}
diff --git a/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java
b/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java
new file mode 100644
index 0000000..393a896
--- /dev/null
+++ b/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java
@@ -0,0 +1,30 @@
+package org.apache.sling.reseditor.resource;
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * It wraps the resources with the {@link ResourceTypeResourceWrapper} on 
+ * {@code next()}.
+ */
+public class ResourceIteratorWrapper implements Iterator<Resource>{
+
+	private Iterator<Resource> iterator;
+
+	public ResourceIteratorWrapper(Iterator<Resource> iterator){
+		this.iterator = iterator;
+	}
+	
+	public boolean hasNext() {
+		return iterator.hasNext();
+	}
+
+	public Resource next() {
+		return new ResourceTypeResourceWrapper(iterator.next());
+	}
+
+	public void remove() {
+		iterator.remove();
+	}
+
+}
diff --git a/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java
b/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java
new file mode 100644
index 0000000..6ca9ebf
--- /dev/null
+++ b/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java
@@ -0,0 +1,85 @@
+/*
+ * 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.reseditor.resource;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceDecorator;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceWrapper;
+
+/**
+ * Decorates resources that have been called with the {@link ResEditorResourceProvider} with
the resource-editor resource type.
+ * 
+ */
+
+@Component
+@Service(ResourceDecorator.class)
+@Properties({
+ @Property(name = "service.description", value = "Decorates resources that have been called
with the ResEditorResourceProvider with the resource-editor resource type."),
+ @Property(name = "service.vendor", value = "The Apache Software Foundation")
+})
+public class ResourceProviderBasedResourceDecorator implements ResourceDecorator {
+
+	/**
+	 * @see org.apache.sling.api.resource.ResourceDecorator#decorate(org.apache.sling.api.resource.Resource,
+	 *      javax.servlet.http.HttpServletRequest)
+	 */
+	public Resource decorate(Resource resource, HttpServletRequest request) {
+		String pathInfo = request.getPathInfo();
+		return getResourceEditorResourceWrapper(resource,
+				pathInfo);
+	}
+
+	/**
+	 * @see org.apache.sling.api.resource.ResourceDecorator#decorate(org.apache.sling.api.resource.Resource)
+	 */
+	public Resource decorate(Resource resource) {
+		Resource result = null;
+		if (resource != null) {
+			ResourceMetadata resourceMetadata = resource.getResourceMetadata();
+			if (resourceMetadata != null) {
+				String resolutionPathInfo = resourceMetadata.getResolutionPathInfo();
+				result = getResourceEditorResourceWrapper(resource,resolutionPathInfo);
+			}
+		}
+		return result;
+	}
+
+	private Resource getResourceEditorResourceWrapper(Resource resource, String resolutionPathInfo)
{
+		Resource result = null;
+		ResourceMetadata resourceMetadata = resource.getResourceMetadata();
+		boolean isResourceEditorProviderResource = resourceMetadata != null ? resourceMetadata.containsKey(ResEditorResourceProvider.RESOURCE_EDITOR_PROVIDER_RESOURCE)
: false;  
+		if (resolutionPathInfo != null && isResourceEditorProviderResource) {
+			result = new ResourceWrapper(resource) {
+				@Override
+				public String getResourceType() {
+					return ResEditorResourceProvider.RESEDITOR_RESOURCE_TYPE;
+				}
+
+			};
+		}
+		return result;
+	}
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java
b/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java
new file mode 100644
index 0000000..68c6225
--- /dev/null
+++ b/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java
@@ -0,0 +1,32 @@
+package org.apache.sling.reseditor.resource;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceWrapper;
+
+/**
+ * This ResourceWrapper adds a marker to the ResourceMetadata object that the {@link ResourceProviderBasedResourceDecorator}
+ * uses.
+ */
+public class ResourceTypeResourceWrapper extends ResourceWrapper {
+
+	public ResourceTypeResourceWrapper(Resource resource) {
+		super(resource);
+	}
+
+	@Override
+	public ResourceMetadata getResourceMetadata() {
+		ResourceMetadata newResourceMetadata = new ResourceMetadata();
+		newResourceMetadata.putAll(getResource().getResourceMetadata());
+		newResourceMetadata.put(ResEditorResourceProvider.RESOURCE_EDITOR_PROVIDER_RESOURCE, null);
+		return newResourceMetadata;
+	}
+
+	@Override
+    public Iterator<Resource> listChildren() {
+		Iterator<Resource> originalIterator = this.getResource().listChildren();
+		return new ResourceIteratorWrapper(originalIterator);
+	}
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message