Skip to content

Instantly share code, notes, and snippets.

@sdmcraft
Created May 27, 2015 05:32
Show Gist options
  • Save sdmcraft/94fdfc5e031a5ffdc3dd to your computer and use it in GitHub Desktop.
Save sdmcraft/94fdfc5e031a5ffdc3dd to your computer and use it in GitHub Desktop.
SlingPostProcessor implementation to support configuration based post processing
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Service
@Reference(name = "resourceTypePostProcessors",referenceInterface = ResourceTypePostProcessor.class, policy = ReferencePolicy.DYNAMIC, bind = "bindResourceTypePostProcessor",
unbind = "unbindResourceTypePostProcessor", cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE)
public class ConfigurablePostProcessor
implements SlingPostProcessor {
private static final Logger log = LoggerFactory.getLogger(ConfigurablePostProcessor.class);
Map<String, ResourceTypePostProcessor> resourceTypePostProcessorMap = new HashMap<String, ResourceTypePostProcessor>();
@Override
public void process(
SlingHttpServletRequest slingHttpServletRequest,
List<Modification> list) throws Exception {
String requestResourceType = slingHttpServletRequest.getResource().getResourceType();
if(resourceTypePostProcessorMap.containsKey(requestResourceType)) {
resourceTypePostProcessorMap.get(requestResourceType).process(slingHttpServletRequest, list);
}
}
public void bindResourceTypePostProcessor(ResourceTypePostProcessor resourceTypePostProcessor) {
resourceTypePostProcessorMap.put(resourceTypePostProcessor.getResourceType(), resourceTypePostProcessor);
log.info("Bounded post processor for resource type:" + resourceTypePostProcessor.getResourceType());
}
public void unbindResourceTypePostProcessor(ResourceTypePostProcessor resourceTypePostProcessor) {
resourceTypePostProcessorMap.remove(resourceTypePostProcessor.getResourceType());
log.info("Unbounded post processor for resource type:" + resourceTypePostProcessor.getResourceType());
}
}
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.servlets.post.Modification;
import java.util.List;
public interface ResourceTypePostProcessor {
public void process(
SlingHttpServletRequest request,
List<Modification> list) throws Exception;
public String getResourceType();
}
import org.apache.felix.scr.annotations.Activate;
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.SlingHttpServletRequest;
import org.apache.sling.servlets.post.Modification;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
@Component(metatype = true)
@Service
@Properties({@Property(name = "sling.servlet.resourceTypes",value = "slingdemo")})
public class ResourceTypePostProcessorImpl implements ResourceTypePostProcessor {
private static final Logger log = LoggerFactory.getLogger(ResourceTypePostProcessorImpl.class);
private String resourceType;
@Activate
public void activate(ComponentContext ctx) {
resourceType = (String)ctx.getProperties().get("sling.servlet.resourceTypes");
}
@Override public void process(SlingHttpServletRequest request,
List<Modification> list) throws Exception {
log.info(">>>>>>>>>Inside ResourceTypePostProcessorImpl");
}
public String getResourceType() {
return resourceType;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment