Page Replication | Community
Skip to main content
Level 5
October 1, 2020
Solved

Page Replication

  • October 1, 2020
  • 1 reply
  • 1092 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Manjunath_K

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

1 reply

Manjunath_K
Manjunath_KAccepted solution
Level 7
October 2, 2020

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

v1101Author
Level 5
October 2, 2020
@manjunath_k - Thank you Manjunathh. for the prompt response