Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Page Replication

Avatar

Level 5

Hello Community - Need your suggestion to implement the solution for replicating the page. We have a admin page in author instance and it is only internal to the authors. In our admin page, we will be having a drop-down selection to select the page and also there will be a button next to it. Once the page is selected and click the button, the respective should be replicated to publish instance. Please provide your suggestion on this.

1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi @v1101

We have Replicator Service methods to activate the provided page path. please find below code for the same.

 

Javascript

Pass the page path(ex: with attribute name "pagePath") which author has selected to Servlet in AJAX request.

 

Servlet:

Create new servlet & service user with required permission.

 

public class ReplicationServlet extends SlingAllMethodsServlet {
@Reference
private Replicator replicator;

@Reference
private ResourceResolverFactory resourceResolverFactory;

private static final String PAGE_PATH = "pagePath";

private static final String SERVICE_USER = "replication-user"; //create new service user with required permission
private static final Map<String, Object> SERVICE_USER_DETAILS = ImmutableMap
.of(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_USER);

private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationServlet.class);

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String pagePath = request.getParameter(PAGE_PATH); //page path without .html extension

if(StringUtils.isNotEmpty(pagePath)) {
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(SERVICE_USER_DETAILS)) {
if (resourceResolver != null) {
Session session = request.getResourceResolver().adaptTo(Session.class);

if (session != null && replicator != null) {
replicator.replicate(session, ReplicationActionType.ACTIVATE, pagePath);
}
}
} catch (LoginException e) {
LOGGER.error("LoginException occurred", e);
} catch (ReplicationException e) {
LOGGER.error("ReplicationException occurred", e);
}
}
}
}

 

- Manjunath

View solution in original post

3 Replies

Avatar

Correct answer by
Level 8

Hi @v1101

We have Replicator Service methods to activate the provided page path. please find below code for the same.

 

Javascript

Pass the page path(ex: with attribute name "pagePath") which author has selected to Servlet in AJAX request.

 

Servlet:

Create new servlet & service user with required permission.

 

public class ReplicationServlet extends SlingAllMethodsServlet {
@Reference
private Replicator replicator;

@Reference
private ResourceResolverFactory resourceResolverFactory;

private static final String PAGE_PATH = "pagePath";

private static final String SERVICE_USER = "replication-user"; //create new service user with required permission
private static final Map<String, Object> SERVICE_USER_DETAILS = ImmutableMap
.of(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_USER);

private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationServlet.class);

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String pagePath = request.getParameter(PAGE_PATH); //page path without .html extension

if(StringUtils.isNotEmpty(pagePath)) {
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(SERVICE_USER_DETAILS)) {
if (resourceResolver != null) {
Session session = request.getResourceResolver().adaptTo(Session.class);

if (session != null && replicator != null) {
replicator.replicate(session, ReplicationActionType.ACTIVATE, pagePath);
}
}
} catch (LoginException e) {
LOGGER.error("LoginException occurred", e);
} catch (ReplicationException e) {
LOGGER.error("ReplicationException occurred", e);
}
}
}
}

 

- Manjunath