You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2014/11/21 15:55:30 UTC

svn commit: r1640927 - /sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/

Author: bdelacretaz
Date: Fri Nov 21 14:55:30 2014
New Revision: 1640927

URL: http://svn.apache.org/r1640927
Log:
SLING-4173 - switch to ResourceProvider - patch contributed by Sandro Boehme, thanks!

Added:
    sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/
    sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java
    sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java
    sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java
    sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java

Added: sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java?rev=1640927&view=auto
==============================================================================
--- sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java (added)
+++ sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResEditorResourceProvider.java Fri Nov 21 14:55:30 2014
@@ -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();
+    }
+    
+}

Added: sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java?rev=1640927&view=auto
==============================================================================
--- sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java (added)
+++ sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceIteratorWrapper.java Fri Nov 21 14:55:30 2014
@@ -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();
+	}
+
+}

Added: sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java?rev=1640927&view=auto
==============================================================================
--- sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java (added)
+++ sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceProviderBasedResourceDecorator.java Fri Nov 21 14:55:30 2014
@@ -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

Added: sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java?rev=1640927&view=auto
==============================================================================
--- sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java (added)
+++ sling/trunk/contrib/explorers/resourceeditor/src/main/java/org/apache/sling/reseditor/resource/ResourceTypeResourceWrapper.java Fri Nov 21 14:55:30 2014
@@ -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);
+	}
+}