how we can checks the replication status of the content (e.g., an asset or a page) using code, i have a usecase to implement where i need to check whether a content is in replication or published
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @NickAd1
Below is a demo servlet which you can use to get the replication status of any content passed through parameters like :
http://localhost:4502/bin/checkReplicationStatus?path=/content/dam/myProject/image.jpg
import com.day.cq.replication.ReplicationStatus; import com.google.gson.JsonObject; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.framework.Constants; import javax.servlet.Servlet; import javax.servlet.ServletException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @Component( service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Replication Status Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/checkReplicationStatus" } ) public class ReplicationStatusServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { String contentPath = request.getParameter("path"); JsonObject jsonResponse = new JsonObject(); if (contentPath == null || contentPath.isEmpty()) { jsonResponse.addProperty("error", "Content path is required"); response.setStatus(SlingHttpServletResponse.SC_BAD_REQUEST); } else { Resource resource = request.getResourceResolver().getResource(contentPath); if (resource == null) { jsonResponse.addProperty("error", "Resource not found"); response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND); } else { ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class); if (replicationStatus != null) { jsonResponse.addProperty("path", contentPath); jsonResponse.addProperty("isPublished", replicationStatus.isActivated()); jsonResponse.addProperty("lastReplicationAction", replicationStatus.getLastReplicationAction() != null ? replicationStatus.getLastReplicationAction().toString() : "N/A"); jsonResponse.addProperty("lastReplicationDate", replicationStatus.getLastReplicationDate() != null ? formatDate(replicationStatus.getLastReplicationDate().getTime()) : "N/A"); jsonResponse.addProperty("isInReplicationQueue", replicationStatus.isPending()); } else { jsonResponse.addProperty("error", "Replication status not available for this resource"); response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } response.setContentType("application/json"); response.getWriter().write(jsonResponse.toString()); } private String formatDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); return sdf.format(date); } }
This will return a json response.
Hope this helps!
you can try like this :
For Asset:
public String checkReplicationStatusForAsset(String path) {
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Asset asset = assetManager.getAsset(path);
if (asset != null) {
ReplicationStatus replicationStatus = asset.adaptTo(ReplicationStatus.class);
if (replicationStatus != null) {
boolean isActivated = replicationStatus.isActivated();
return "Asset " + path + " is " + (isActivated ? "activated" : "not activated");
} else {
return "Could not get replication status for asset " + path;
}
} else {
return "Asset " + path + " does not exist";
}
}
For Page:
Using PageManager - by passing the path of the page
public void checkReplicationStatusForPage(String path) {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(path);
if (page != null) {
ReplicationStatus replicationStatus = page.adaptTo(ReplicationStatus.class);
if (replicationStatus != null) {
boolean isActivated = replicationStatus.isActivated();
System.out.println("Page " + path + " is " + (isActivated ? "activated" : "not activated"));
} else {
System.out.println("Could not get replication status for page " + path);
}
} else {
System.out.println("Page " + path + " does not exist");
}
}
Hi @NickAd1,
You can have a ReplicationStatusCheck class to check the "cq:lastReplicated" property for a particular path
Here's a sample code -
if (resource != null) {
ValueMap properties = resource.adaptTo(ValueMap.class);
if (properties != null) {
StringreplicationStatus = properties.get("cq:lastReplicated", String.class);
return replicationStatus != null ? replicationStatus : "Not replicated yet.";
}
}
Alternatively, you can also use Replication API to hit the end point - bin/receive?path=
This returns the replication status in JSON format, which includes details like whether the page is replicated, the last replication time, etc.
Hope this helps!
This is an example of checking the status of a single page, but using your java skills, you should be able to send in a particular param and applying Java patterns like SOLID principals when creating methods utils, etc... in this example, we are checking the status of /content/wknd/en/adventure
import com.day.cq.replication.ReplicationStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import java.io.IOException;
@Component(
service = {Servlet.class},
property = {
"sling.servlet.paths=/bin/checkReplicationStatusHardcoded",
"sling.servlet.methods=GET"
}
)
public class CheckReplicationStatusHardcodedServlet extends SlingAllMethodsServlet {
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String path = "/content/wknd/en/adventure/jcr:content";
ResourceResolver resourceResolver = request.getResourceResolver();
Resource resource = resourceResolver.getResource(path);
if (resource == null) {
response.getWriter().write("Resource not found at path: " + path);
return;
}
// Get the ReplicationStatus of the resource
ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);
if (replicationStatus == null) {
response.getWriter().write("Replication status not available for this resource.");
return;
}
// Check if the resource is published
boolean isPublished = replicationStatus.isActivated();
boolean isInReplication = replicationStatus.isStale(); // Checks if the resource is out of sync with publish
// Respond with the replication status
response.getWriter().write("Path: " + path + "\n");
response.getWriter().write("Is Published: " + isPublished + "\n");
response.getWriter().write("Is In Replication (Out of Sync): " + isInReplication + "\n");
}
}
Example URL: http://localhost:4502/bin/checkReplicationStatusHardcoded
Example Output:
Path: /content/wknd/en/adventure/jcr:content
Is Published: true
Is In Replication (Out of Sync): false
Hi @NickAd1
Below is a demo servlet which you can use to get the replication status of any content passed through parameters like :
http://localhost:4502/bin/checkReplicationStatus?path=/content/dam/myProject/image.jpg
import com.day.cq.replication.ReplicationStatus; import com.google.gson.JsonObject; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.framework.Constants; import javax.servlet.Servlet; import javax.servlet.ServletException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @Component( service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Replication Status Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/checkReplicationStatus" } ) public class ReplicationStatusServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { String contentPath = request.getParameter("path"); JsonObject jsonResponse = new JsonObject(); if (contentPath == null || contentPath.isEmpty()) { jsonResponse.addProperty("error", "Content path is required"); response.setStatus(SlingHttpServletResponse.SC_BAD_REQUEST); } else { Resource resource = request.getResourceResolver().getResource(contentPath); if (resource == null) { jsonResponse.addProperty("error", "Resource not found"); response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND); } else { ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class); if (replicationStatus != null) { jsonResponse.addProperty("path", contentPath); jsonResponse.addProperty("isPublished", replicationStatus.isActivated()); jsonResponse.addProperty("lastReplicationAction", replicationStatus.getLastReplicationAction() != null ? replicationStatus.getLastReplicationAction().toString() : "N/A"); jsonResponse.addProperty("lastReplicationDate", replicationStatus.getLastReplicationDate() != null ? formatDate(replicationStatus.getLastReplicationDate().getTime()) : "N/A"); jsonResponse.addProperty("isInReplicationQueue", replicationStatus.isPending()); } else { jsonResponse.addProperty("error", "Replication status not available for this resource"); response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } response.setContentType("application/json"); response.getWriter().write(jsonResponse.toString()); } private String formatDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); return sdf.format(date); } }
This will return a json response.
Hope this helps!
@NickAd1 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies