From 1a5808059a9ba7ae0c17f51bc77be14459de77bc Mon Sep 17 00:00:00 2001 From: dherges Date: Wed, 28 Oct 2015 15:53:55 +0100 Subject: [PATCH] #36 SlingPostProcessor Example --- .../impl/SlingPostProcessorSample.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SlingPostProcessorSample.java diff --git a/bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SlingPostProcessorSample.java b/bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SlingPostProcessorSample.java new file mode 100644 index 0000000..3e3fb3f --- /dev/null +++ b/bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SlingPostProcessorSample.java @@ -0,0 +1,50 @@ +package com.adobe.acs.samples.servlets.impl; + +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.ModifiableValueMap; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.servlets.post.Modification; +import org.apache.sling.servlets.post.SlingPostProcessor; + +import java.util.List; +import java.util.UUID; + + +/** + * SlingPostProcessor's are called after a POST operation of the default SlingPostServlet but before changes are + * persisted to the repository. + * + * @link https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html#slingpostprocessor + * @link https://sling.apache.org/apidocs/sling7/org/apache/sling/servlets/post/SlingPostProcessor.html + */ +@Component +@Service +public class SlingPostProcessorSample implements SlingPostProcessor { + + @Override + public void process(SlingHttpServletRequest request, List modifications) throws Exception { + // First, check if our post processor implementation needs to do anything + // Note: all registered SlingPostProcessor instances are called on all invocations of the Sling POST Servlet + if (accepts(request)) { + + // Apply your custom changes: first, adapt the resource to a ModifiableValueMap + final Resource resource = request.getResource(); + final ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class); + + // Example: add an additional property to the resource + String uuid = UUID.randomUUID().toString(); + properties.put("myapp.versionHash", uuid); + + // Record a "MODIFIED" entry in the modifications list + modifications.add(Modification.onModified(resource.getPath())); + } + } + + protected boolean accepts(SlingHttpServletRequest request) { + // If you want to modify only certain resources, you can check on the resource's "sling:resourceType" + return "my-app/components/fancy-resource-type".equals(request.getResource().getResourceType()); + } + +}