Expand my Community achievements bar.

SOLVED

Publishing Content through API requests (Specifically Workfront Fusion)

Avatar

Level 2

Hello,

 

I would like to know if there is a way to publish content (ie: content and/or experience fragments and assets) to the publisher instance via API calls. Our goal is to only allow publishing content via Workfront Fusion. If anyone could point me in the right direction, that would be highly appreciated.

 

Thank you! 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @bruce_fernandes 

 

You can write a servlet that exposes the end-point in AEM author that takes the payload and/or references to publish to publish instance. So that allows you to call the end-point from Workfront Fusion. 

 

For reference, check the below example: 

package com.example.core.servlets;

import com.day.cq.replication.Agent;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.ReplicationStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Map;

@Component(
    service = Servlet.class,
    property = {
        ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/publishcontent",
        ServletResolverConstants.SLING_SERVLET_METHODS + "=POST"
    }
)
public class PublishContentServlet extends SlingAllMethodsServlet {

    @Reference
    private Replicator replicator;

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String contentPath = request.getParameter("contentPath");

        if (contentPath == null || contentPath.isEmpty()) {
            response.setStatus(SlingHttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("Error: Missing contentPath parameter.");
            return;
        }

        try {
            // Publish content
            replicator.replicate(request.getResourceResolver().adaptTo(Session.class),
                    ReplicationActionType.ACTIVATE, contentPath);

            response.setStatus(SlingHttpServletResponse.SC_OK);
            response.getWriter().write("Content published successfully: " + contentPath);

        } catch (Exception e) {
            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("Error publishing content: " + e.getMessage());
        }
    }
}

 

Thanks,

Lokesh

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @bruce_fernandes 

 

You can write a servlet that exposes the end-point in AEM author that takes the payload and/or references to publish to publish instance. So that allows you to call the end-point from Workfront Fusion. 

 

For reference, check the below example: 

package com.example.core.servlets;

import com.day.cq.replication.Agent;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.ReplicationStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Map;

@Component(
    service = Servlet.class,
    property = {
        ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/publishcontent",
        ServletResolverConstants.SLING_SERVLET_METHODS + "=POST"
    }
)
public class PublishContentServlet extends SlingAllMethodsServlet {

    @Reference
    private Replicator replicator;

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String contentPath = request.getParameter("contentPath");

        if (contentPath == null || contentPath.isEmpty()) {
            response.setStatus(SlingHttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("Error: Missing contentPath parameter.");
            return;
        }

        try {
            // Publish content
            replicator.replicate(request.getResourceResolver().adaptTo(Session.class),
                    ReplicationActionType.ACTIVATE, contentPath);

            response.setStatus(SlingHttpServletResponse.SC_OK);
            response.getWriter().write("Content published successfully: " + contentPath);

        } catch (Exception e) {
            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("Error publishing content: " + e.getMessage());
        }
    }
}

 

Thanks,

Lokesh

Avatar

Level 6

Hi @bruce_fernandes ,

 

We need to write a servlet as @Lokesh_Vajrala  suggested. Since it should run only on the Author instance, we’ll need to expose this endpoint from Author to the third-party system.

There’s a similar thread on the AEM Community Portal about exposing a servlet from AEM Author to a third party. Sharing the link below—hope it helps!

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/right-way-to-expose-an-end...

 

Regards,

Ayush