AEM 6 - Filter (Skip) Specific Nodes and Properties from Activation (Replication)

Goal


Filter or skip any renditions other than thumbnail.48.48.png from Activation. In addition to filtering renditions, restrict activation of properties with specific namespace eaem. A sample Replication Content Filter implementation for restrictive activation

Thank you Ravi Kiran for the suggestion

Demo | Package Install | Source Code


Image Node in Author CRX




Image Node in Publish CRX

                            Nodes renditions/cq5dam.thumbnail.319.319.png, renditions/cq5dam.thumbnail.140.100.png, renditions/original NOT activated
                            Property eaem:department NOT activated




Solution


1) Create a sample namespace eaem in Author CRX using the namespace editor (http://localhost:4502/crx/explorer/ui/namespace_editor.jsp). Check this post for more information on creating namespaces

2) Code and install a OSGI servlet apps.experienceaem.repfilter.NodePropertyReplicationFilterFactory (check this on post creating OSGI services)

package apps.experienceaem.repfilter;

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContentFilter;
import com.day.cq.replication.ReplicationContentFilterFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import java.util.List;

@Component
@Service
public class NodePropertyReplicationFilterFactory implements ReplicationContentFilterFactory {

    private static final Logger log = LoggerFactory.getLogger(NodePropertyReplicationFilterFactory.class);

    private static final ReplicationContentFilter FILTER = new NodePropertyReplicationContentFilter();

    /**
     * Filter executes on content activation
     *
     * @param action The {@link ReplicationAction} to consider.
     *
     * @return
     */
    public ReplicationContentFilter createFilter(final ReplicationAction action) {
        return action.getType() == ReplicationActionType.ACTIVATE ? FILTER : null;
    }

    /**
     * Filters out some renditions and properties from activation
     */
    private static class NodePropertyReplicationContentFilter implements ReplicationContentFilter {
        private static String ALLOWED_RENDITION = "thumbnail.48.48.png";
        private static String RESTRICTED_PREFIX = "eaem:";

        /**
         * Filter any renditions other than thumbnail.48.48.png while Activation
         *
         * @param node The {@link Node} to check.
         * @return
         */
        public boolean accepts(final Node node) {
            try {
                String parentName = node.getParent().getName();

                if (!parentName.equals("renditions")) {
                    return true;
                }

                String nodeName = node.getName();

                return nodeName.endsWith(ALLOWED_RENDITION);
            } catch (RepositoryException e) {
                log.error("Error with filtering ", e);
            }

            return true;
        }

        /**
         * Any property with prefix eaem: is not activated
         *
         * @param property The {@link Property} to check.
         * @return
         */
        public boolean accepts(final Property property) {
            try {
                String name = property.getName();

                return !name.startsWith(RESTRICTED_PREFIX);
            } catch (RepositoryException e) {
                log.error("Error with filtering ", e);
            }

            return true;
        }

        public boolean allowsDescent(final Node node) {
            return true;
        }

        public List<String> getFilteredPaths() {
            return null;
        }
    }
}

1 comment:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Adobe Experience Manager
    , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Adobe Experience Manager . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete