I am working on a project where we allow end users (anonymous users) to submit requests and we capture and moderate the content. Business as usual, I have my HTML form, my servlet, etc... I store the submitted content using JcrUtil with admin session (see Code Fragment). Everything works as expected, except: reverse replication. The issue is that reverse replication agent merely doesn't pick up the newly created pages (based on the log file) while the pages have all the required properties for reverse replication. So the pages never get reverse replicated to author instance.

 

I have been digging around to find solution and thanks for the comments left by @TomaszRekawek on this Stackoverflow's article: http://stackoverflow.com/questions/20162289/cq-5-6-reverse-replication-replication-triggered-but-no-agent-found-or-selecte. So the idea is that you need to save the session twice. First, after the node is created, and second is after setting the node's properties for reverse replication.

Believe it or not this was the only way that solved my problem.

Code Fragment

...
import org.apache.sling.jcr.api.SlingRepository;
...
...
...
@Reference
protected SlingRepository repository;
...
...
...
Session session = this.repository.loginAdministrative(null);
....
Node node = JcrUtil.createPath(path, true, "sling:Folder", "cq:Page", session, false);
Node jcrContentNode = node.addNode(JcrConstants.JCR_CONTENT);
...
session.save();
....
jcrContentNode.setProperty("cq:distribute", true);
jcrContentNode.setProperty("cq:lastModified", Calendar.getInstance()); jcrContentNode.setProperty("cq:lastModifiedBy", session.getUserID());
...
session.save();