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

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

Avatar

Level 2

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.

Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

4 Replies

Avatar

Community Advisor

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); }

 

 


Aanchal Sikka

Avatar

Correct answer by
Community Advisor

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.