AEM API to Get the status of a given content fragment? | Community
Skip to main content
New Participant
November 28, 2022
Solved

AEM API to Get the status of a given content fragment?

  • November 28, 2022
  • 2 replies
  • 690 views

Is there any AEM API which can give me the status of given content fragment when fetched by path whether it is published or not?

 

CURL https://BASEURI/.../_path

 

expected response: {"isPublished":true or false}

 

something like that

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 lukasz-m

Hi @spidey1405,

There are few options to get above information:

  1. You can use query builder, in the way @arunpatidar described in this post: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-graphql-get-the-published-content-fragments-only-in-author/m-p/560153/highlight/true#M139325
  2. If you know path to content fragment which replication status you would like to check you can try something like this: /content/dam/path-to-cf/jcr:content.json
  3. Finally you can write custom servlet that will return replication status, below example returns replication status for given path (also CF), when you add replicationStatus selector and json extension, e.g. /content/dam/path-to-cf.replicationStatus.json
    package com.mysite.core.servlets;
    
    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.SlingSafeMethodsServlet;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component(service = Servlet.class, property = {
            "sling.servlet.methods=" + HttpConstants.METHOD_GET,
            "sling.servlet.resourceTypes=sling/servlet/default",
            "sling.servlet.selectors=replicationStatus",
            "sling.servlet.extensions=json" })
    public class ReplicationStatusServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            Resource resource = request.getResource();
            ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("isPublished", replicationStatus.isActivated());
            response.setContentType("application/json");
            response.getWriter().write(jsonObject.toString());
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }
    

2 replies

lukasz-m
lukasz-mAccepted solution
New Participant
November 28, 2022

Hi @spidey1405,

There are few options to get above information:

  1. You can use query builder, in the way @arunpatidar described in this post: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-graphql-get-the-published-content-fragments-only-in-author/m-p/560153/highlight/true#M139325
  2. If you know path to content fragment which replication status you would like to check you can try something like this: /content/dam/path-to-cf/jcr:content.json
  3. Finally you can write custom servlet that will return replication status, below example returns replication status for given path (also CF), when you add replicationStatus selector and json extension, e.g. /content/dam/path-to-cf.replicationStatus.json
    package com.mysite.core.servlets;
    
    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.SlingSafeMethodsServlet;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component(service = Servlet.class, property = {
            "sling.servlet.methods=" + HttpConstants.METHOD_GET,
            "sling.servlet.resourceTypes=sling/servlet/default",
            "sling.servlet.selectors=replicationStatus",
            "sling.servlet.extensions=json" })
    public class ReplicationStatusServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            Resource resource = request.getResource();
            ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("isPublished", replicationStatus.isActivated());
            response.setContentType("application/json");
            response.getWriter().write(jsonObject.toString());
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }
    

New Participant
November 28, 2022

Hi ,

 

Content fragment supported api 

 

https://developer.adobe.com/experience-manager/reference-materials/6-5/assets-api-content-fragments/index.html#/

 

But there is no specific api for getting replication status.

 

Thanks