Skip to main content
Shahid_Siddiqui_04
Level 2
September 19, 2023
Solved

How to publish list of pages by java(Servlet or services) ?

Let's assume I have list of path like, /content/us/en/a,  /content/us/en/b,  /content/us/en/c etc. and I want to publish these content from java 

What I have ?

List<String> allPaths = allPagesPathWhichNeedToPublish();

and want to publish all pages which is in allPaths list. What java steps I need to follow to publish path/pages one by one.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
lukasz-m
lukasz-mAccepted solution
Community Advisor
September 20, 2023

Hi @shahid_siddiqui_04,

Like @aanchal-sikka already described, you should use Replicator java API.

There are two options in your case:

  1. Iterate through all elements in your list and run replicate method for each item.
    @Reference private Replicator replicator; // place for other code List<String> allPaths = allPagesPathWhichNeedToPublish(); Session session = resourceResolver.adaptTo(Session.class); for(String path : allPaths) { try { replicator.replicate(session, ReplicationActionType.ACTIVATE, path, null); } catch (ReplicationException e) { e.printStackTrace(); } }​
  2. Use dedicate replicate method, that is consuming array of paths, in this case you do not have to iterate through the collection in your code - this option looks much cleaner and better utilizes api capabilities
    @Reference private Replicator replicator; // place for other code List<String> allPaths = allPagesPathWhichNeedToPublish(); // converting to array as this is format accepted by replicator String [] paths = allPaths.toArray(new String[allPaths.size()]) try { replicator.replicate(resourceResolver.adaptTo(Session.class), ReplicationActionType.ACTIVATE, paths, null); } catch (ReplicationException e) { e.printStackTrace(); }

Make sure that resource resolver object you will be suing has a proper level of permission to replicate pages. You can consider to create proper service user and create resource resolver using this user, to have full control of permission set.

4 replies

aanchal-sikka
Community Advisor
Community Advisor
September 20, 2023

Hello @shahid_siddiqui_04 

 

We can use Replicator API to publish pages

 

  • create a Replicator instance by using @Reference annotation.
@Reference
private Replicator replicator;
  • Get a session
ResourceResolver resolver = request.getResourceResolver();
javax.jcr.Session session = resolver.adaptTo(Session.class);
  • Replicate the page by looping through the list
    try {
replicator.replicate(session, ReplicationActionType.ACTIVATE, path); log.info("Page activated: ", path); } catch (ReplicationException e) { log.info("Replication failed "+e.getMessage(), path); }

 

 

Translate
Aanchal Sikka
Shahid_Siddiqui_04
Level 2
September 21, 2023

Thankyou 

Translate
lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
September 20, 2023

Hi @shahid_siddiqui_04,

Like @aanchal-sikka already described, you should use Replicator java API.

There are two options in your case:

  1. Iterate through all elements in your list and run replicate method for each item.
    @Reference private Replicator replicator; // place for other code List<String> allPaths = allPagesPathWhichNeedToPublish(); Session session = resourceResolver.adaptTo(Session.class); for(String path : allPaths) { try { replicator.replicate(session, ReplicationActionType.ACTIVATE, path, null); } catch (ReplicationException e) { e.printStackTrace(); } }​
  2. Use dedicate replicate method, that is consuming array of paths, in this case you do not have to iterate through the collection in your code - this option looks much cleaner and better utilizes api capabilities
    @Reference private Replicator replicator; // place for other code List<String> allPaths = allPagesPathWhichNeedToPublish(); // converting to array as this is format accepted by replicator String [] paths = allPaths.toArray(new String[allPaths.size()]) try { replicator.replicate(resourceResolver.adaptTo(Session.class), ReplicationActionType.ACTIVATE, paths, null); } catch (ReplicationException e) { e.printStackTrace(); }

Make sure that resource resolver object you will be suing has a proper level of permission to replicate pages. You can consider to create proper service user and create resource resolver using this user, to have full control of permission set.

Translate
Shahid_Siddiqui_04
Level 2
September 21, 2023

Thankyou

Translate