forked from Adobe-Consulting-Services/acs-aem-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adobe-Consulting-Services#36 SlingPostProcessor Example
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
bundle/src/main/java/com/adobe/acs/samples/servlets/impl/SlingPostProcessorSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Modification> 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()); | ||
} | ||
|
||
} |