Respond to a Page Create, Copy, Move, Delete, or Update Event
Introduction
Goal
Extend the page create, copy, move, delete, or update process with additional custom behavior.
Summary
CMS users can perform a number of page actions within the Experience manager, specifically they can create, copy, move, delete, and update pages via thePage Settings. Before processing such an action, the delivery tier sends an event to its internal event bus, enabling developers to extend the process with additional behavior.
Extend the Page Actions
When the delivery tier (HST) processes a page action, and before persisting any changes to the repository, aPageEvent is sent to the synchronousHST internal event bus, of a type corresponding to the action that took place. Developers can create a listener for thisPageEvent and add custom behavior to the page action (like invoking some workflow on some content, sending an event to a message bus, removing an experiment that was running on the source page, etc). Information about the page action is exposed via the event context, accessible via thePageEvent’sgetPageActionContext method. Through aPageEvent, a developer can alsoabort the entire page action.
Because the code in the HST that posts the event also handles the persistence of the session changes at the end, a developer shouldnever save the JCR session that can be accessed via either
pageEvent.getPageActionContext().getRequestContext().getSession();
or via any method of thePageActionContext that exposes a JCRNode, for example in thePageCopyEvent
copyEvent.getPageActionContext().getNewSiteMapItemNode().getSession()
Create a Custom Listener for a PageEvent
The example below is for aPageCopyEvent listener. To create listeners for other action types, use the appropriatePageEvent class (PageCreateEvent,PageCopyEvent,PageMoveEvent,PageDeleteEvent, or PageUpdateEvent). Different information is exposed per event type, via differentPageActionContext implementations. Please consult theJavaDocs API Documentation for classesPageCreateContext,PageCopyContext,PageMoveContext,PageDeleteContext, andPageUpdateContext.
package com.example.pagecopy;import org.hippoecm.hst.pagecomposer.jaxrs.api.ChannelEventListenerRegistry;import org.hippoecm.hst.pagecomposer.jaxrs.api.PageCopyEvent;import org.onehippo.cms7.services.eventbus.Subscribe;public class PageCopyEventListener { @SuppressWarnings("UnusedDeclaration") public void init() { ChannelEventListenerRegistry.get().register(this); } @SuppressWarnings("UnusedDeclaration") public void destroy() { ChannelEventListenerRegistry.get().unregister(this); } @Subscribe public void onPageEvent(PageCopyEvent event) { if (event.getException() != null) { return; } // DO YOUR STUFF BUT MAKE SURE TO NEVER // SAVE THE JCR SESSION FOR THAT IS ACCESSIBLE VIA // THE PageCopyEvent#getPageActionContext#getRequestContext }}The last step is to make sure your PageCopyEventListener is initialized as a Spring bean. For example, add a Spring XML configuration bean in site/components/src/main/resources/META-INF/hst-assembly/overrides/page-event-listeners.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean init-method="init" destroy-method="destroy"/></beans>
Now, whenever a user copies a page via the Page Settings, the abovePageCopyEventListener#onPageEvent is invoked before any changes are persisted into the repository.
Abort Page Action with the PageEvent
A listener for page events can abort a page action by setting an exception on the event object. This can be useful in case some requirement is not met or the post-processing fails. SeeAbort an Experience Manager Action for details.