Expand my Community achievements bar.

SOLVED

manipulate node content when publishing in aemaacs

Avatar

Level 4

i need to manipulate node content programatically after author click quick publish button and send changed content to replication agent for activation. 

that is i need to filter put some paths from the published content and  send filtered content to replication agent for pubishing.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

I don't think that the replication API allows you to modify content (that means: changing the values of properties and/or add/remove nodes) which is then sent only to the publish, but which is not persistet on the authoring instance.

View solution in original post

9 Replies

Avatar

Community Advisor

Hi,

 

I recommend that you go through this article, which explains replication quite well: Replication Under the Hood. Beyond that, I think you should look into the Preprocessor interface. Here are some examples:

 

Hope this helps!



Esteban Bustamante

Avatar

Level 4

I have read it but I want it to work on quick publish button only and not for manage publication.

Avatar

Level 4

plz review this code and help me where am i doing it wrong.

package com.macnicagwi.core.listeners;

 

import javax.jcr.Node;

import javax.jcr.PathNotFoundException;

import javax.jcr.RepositoryException;

import javax.jcr.Session;

 

import org.apache.sling.api.resource.PersistenceException;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import com.day.cq.replication.Preprocessor;

import com.day.cq.replication.ReplicationAction;

import com.day.cq.replication.ReplicationContentFilter;

import com.day.cq.replication.ReplicationException;

import com.day.cq.replication.ReplicationOptions;

import com.day.cq.replication.Replicator;

import com.macnicagwi.core.constants.MacnicaCoreConstants;

import com.macnicagwi.core.utils.MacnicaCoreUtils;

 

@component(service = Preprocessor.class, immediate = true)

public class OnPublishReplicationEventHandler implements Preprocessor {

 

Logger logger = LoggerFactory.getLogger(getClass());

 

@reference

ResourceResolverFactory factory;

 

@Override

public void preprocess(ReplicationAction action, ReplicationOptions options) throws ReplicationException {

// TODO Auto-generated method stub

logger.info("reached");

String id = action.getUserId();

String[] paths = action.getPaths();

ReplicationFactory factory = new ReplicationFactory();

ReplicationContentFilter filter = factory.createFilter(action);

ResourceResolver resolver = MacnicaCoreUtils.getResourceResolver(this.factory,

MacnicaCoreConstants.MACNICA_SUB_SERVICE);

Session session = resolver.adaptTo(Session.class);

for (String path : paths) {

try {

Node node =session.getNode(path);

filter.accepts(node);

resolver.commit();

} catch (PathNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (RepositoryException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (PersistenceException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

 

 

@component(immediate = true,factory = "ReplicationFactory",service = ReplicationContentFilterFactory.class)

public class ReplicationFactory implements ReplicationContentFilterFactory {

 

@Override

public ReplicationContentFilter createFilter(ReplicationAction action) {

// TODO Auto-generated method stub

ReplicationFilter filter = new ReplicationFilter();

return filter;

}

 

}

 

 

@component(immediate = true)

public class ReplicationFilter implements ReplicationContentFilter {

 

@Override

public boolean accepts(Node node) {

// TODO Auto-generated method stub

return false;

}

 

@Override

public boolean accepts(Property property) {

// TODO Auto-generated method stub

return false;

}

 

@Override

public boolean allowsDescent(Node node) {

// TODO Auto-generated method stub

return false;

}

 

@Override

public List String getFilteredPaths() {

// TODO Auto-generated method stub

return null;

}

 

}

Avatar

Community Advisor

Hi @AliSyed1 


What you can do for this is create a Sling Event Handler and bind it to replication event on publish.


This will get called every time a page is replicated to the publisher.
Here you can write the logic to modify the page replicated as you please:

Here is sample code of how you can register such a Handler on AEM Publish instance.

 

 

@Component(service = EventHandler.class,
        immediate = true,
        property = {
                Constants.SERVICE_DESCRIPTION + "=Listener on page replication",
                EventConstants.EVENT_TOPIC + "=" + ReplicationEvent.EVENT_TOPIC
        })
public class OnPublishReplicationEventHandler implements EventHandler {
@Override
    public void handleEvent(final Event event) {
      //write your logic here
    }
}

 

 

 

Avatar

Level 4

is n't it triggers when page is published?i want to change content when publish is clicked.

Avatar

Correct answer by
Employee Advisor

I don't think that the replication API allows you to modify content (that means: changing the values of properties and/or add/remove nodes) which is then sent only to the publish, but which is not persistet on the authoring instance.

Avatar

Level 4

yes but we can prevent some nodes or properties from activating using replication content filter.

Avatar

Administrator

@AliSyed1 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni