Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

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

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-publis...
  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
    ootb-json.jpg
  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);
        }
    }
    
    servlet-json.jpg

View solution in original post

2 Replies

Avatar

Community Advisor

Hi ,

 

Content fragment supported api 

 

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

 

But there is no specific api for getting replication status.

 

Thanks

Avatar

Correct answer by
Community Advisor

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-publis...
  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
    ootb-json.jpg
  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);
        }
    }
    
    servlet-json.jpg