Created
February 21, 2018 17:43
-
-
Save ani03sha/6191609d02465ca429e80adece849907 to your computer and use it in GitHub Desktop.
Adobe Experience Manager event handler that responds to JCR events, such as when a node is created.
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
package org.anirudh.redquark.eventing; | |
import java.util.Arrays; | |
import javax.jcr.Node; | |
import javax.jcr.RepositoryException; | |
import javax.jcr.Session; | |
import javax.jcr.observation.Event; | |
import javax.jcr.observation.EventIterator; | |
import javax.jcr.observation.EventListener; | |
import javax.jcr.observation.ObservationManager; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.osgi.service.component.ComponentContext; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.day.cq.tagging.JcrTagManagerFactory; | |
import com.day.cq.tagging.Tag; | |
import com.day.cq.tagging.TagManager; | |
/** | |
* This is simple event listener which tags a page as soon as it is created | |
*/ | |
@Component(metatype = true) | |
@Service | |
public class TagEventListener implements EventListener, Runnable { | |
/** | |
* Logger | |
*/ | |
private static final Logger log = LoggerFactory.getLogger(TagEventListener.class); | |
/** | |
* Injecting Sling Resource Resolver Factory | |
*/ | |
@Reference | |
private ResourceResolverFactory resolverFactory; | |
/** | |
* Custom namespace | |
*/ | |
private static final String NAMESPACE = "/etc/tags/forum"; | |
/** | |
* Injecting JCR Tag Manager Factory | |
*/ | |
@Reference | |
private JcrTagManagerFactory tagManagerFactory; | |
/** | |
* Session instance | |
*/ | |
private Session session; | |
/** | |
* Observation Manager | |
*/ | |
private ObservationManager observationManager; | |
/** | |
* Default run method of a thread | |
*/ | |
public void run() { | |
log.info("Running..."); | |
} | |
/** | |
* Placing the application's logic here to define custom event handler | |
*/ | |
protected void activate(ComponentContext context) { | |
context.getBundleContext(); | |
try { | |
/** | |
* Creating a session | |
*/ | |
ResourceResolver resolver = resolverFactory.getAdministrativeResourceResolver(null); | |
session = resolver.adaptTo(Session.class); | |
/** | |
* Setup the event handler | |
*/ | |
observationManager = session.getWorkspace().getObservationManager(); | |
/** | |
* Types of nodes where the event listening needs to take place | |
*/ | |
final String[] types = { "cq:Page", "nt:unstructured" }; | |
/** | |
* Path under which event listening should take place | |
*/ | |
final String path = "/"; | |
/** | |
* Adding the event listener in the Observation Manager | |
*/ | |
observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, null, false); | |
/** | |
* Logging the information | |
*/ | |
log.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path); | |
} catch (Exception e) { | |
log.error(e.getMessage(), e); | |
} | |
} | |
/** | |
* Clean up activities | |
*/ | |
protected void deactivate(ComponentContext componentContext) throws RepositoryException { | |
if (observationManager != null) { | |
observationManager.removeEventListener(this); | |
} | |
if (session != null) { | |
session.logout(); | |
session = null; | |
} | |
} | |
/** | |
* Default method that gets called once the event takes place | |
*/ | |
public void onEvent(EventIterator events) { | |
log.info("In onEvent()"); | |
try { | |
while (events.hasNext()) { | |
Event event = events.nextEvent(); | |
log.info("AutoTagListener - new add event: ", event.getPath()); | |
Node pageContentNode = session.getNode(event.getPath()); | |
if ((pageContentNode == null) || !pageContentNode.getPrimaryNodeType().isNodeType("cq:PageContent")) { | |
log.debug("Skip processing node: " + event.getPath()); | |
return; | |
} | |
Node pageNode = pageContentNode.getParent(); | |
if ((pageNode == null) || !pageNode.getPrimaryNodeType().isNodeType("cq:Page")) { | |
log.debug("Skip processing node: " + pageNode); | |
return; | |
} | |
TagManager tagManager = tagManagerFactory.getTagManager(session); | |
Tag superTag = tagManager.resolve(NAMESPACE); | |
Tag tag = null; | |
if (superTag == null) { | |
tag = tagManager.createTag(NAMESPACE, "My Sample", "My Sample tags", true); | |
log.info("Tag Name Space created : ", tag.getPath()); | |
tag = tagManager.createTag(NAMESPACE + "/" + pageNode.getName(), pageNode.getName(), | |
"Auto tag : " + pageNode.getName(), true); | |
String tagArray[] = new String[1]; | |
tagArray[0] = tag.getNamespace().getName() + ":" | |
+ tag.getPath().substring(tag.getPath().indexOf(NAMESPACE) + NAMESPACE.length() + 1); | |
pageContentNode.setProperty("cq:tags", tagArray); | |
session.save(); | |
log.info("End ONEVENT!"); | |
} | |
} | |
} catch (Exception e) { | |
log.error(e.getMessage(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment