Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Get the XF Page source code in Java

Avatar

Level 2

Hi All,

anyone know how to get the source code of XF 

below i tried but its giving an null pointer error  .

private String getExperienceFragmentHtml(SlingHttpServletRequest request) throws ServletException, IOException {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Resource xfResource = request.getResource();
        String xfPath = xfResource.getPath();
        Page xfPage = resourceResolver.getResource(xfPath).adaptTo(Page.class);
        String html = "";

        if (xfPage != null) {
            try {
                // Create a StringWriter to capture the HTML source code
                StringWriter writer = new StringWriter();
                // Create a PrintWriter with the StringWriter
                PrintWriter printWriter = new PrintWriter(writer);
                // Create a new HtmlLibraryManager instance
                HtmlLibraryManager htmlLibraryManager = resourceResolver.adaptTo(HtmlLibraryManager.class);
                
             HtmlLibrary xfHtmlLibrary = htmlLibraryManager.getLibrary(xfPage, request);
                
                 xfHtmlLibrary.include(request, printWriter, true);
                // Get the HTML source code from the StringWriter
                html = writer.toString();
            } catch (Exception e) {
                // Handle any exceptions
                e.printStackTrace();
            }

        }
        LOG.info("HTML" + html);
        return html;

    }

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @test566 ,

You can use service user to get the session so that resource can able access path, we had the same issue.

Thank,

Sanjay 

 

View solution in original post

3 Replies

Avatar

Community Advisor

@test566 You can try like below

 

public class AemResourceResolutionServlet extends SlingSafeMethodsServlet {

    /** Service to create HTTP Servlet requests and responses */
    @Reference
    private RequestResponseFactory requestResponseFactory;

    /** Service to process requests through Sling */
    @Reference
    private SlingRequestProcessor requestProcessor;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        /* The resource path to resolve. Use any selectors or extension. */
        String requestPath = "<XF path>";

        /* Setup request */
        HttpServletRequest req = requestResponseFactory.createRequest("GET", requestPath);
        WCMMode.DISABLED.toRequest(req);

        /* Setup response */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpServletResponse resp = requestResponseFactory.createResponse(out);

        /* Process request through Sling */
        requestProcessor.processRequest(req, resp, request.getResourceResolver());
        String html = out.toString();
    }
}

Avatar

Level 2

Hi @Saravanan_Dharmaraj ,

Thanks for your input.

 I tried but it is not working gives below WARN and nothing in the response

org.apache.sling.engine.impl.request.RequestData SlingRequestProgressTracker not found in request attributes

code snippet 

import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.wcm.api.NameConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;

import org.apache.commons.io.output.ByteArrayOutputStream;
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.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.engine.SlingRequestProcessor;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.propertytypes.ServiceDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

@Component(service = {Servlet.class}, name = "MFE Servlet", immediate = true)
@SlingServletResourceTypes(
resourceTypes = "poc/components/structure/xfpage",
methods = {HttpConstants.METHOD_GET},
extensions = "js",
selectors = "public.mfe")

public class MFEServlet extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(MFEServlet.class);
@Reference
private RequestResponseFactory requestResponseFactory;
@Reference
private SlingRequestProcessor requestProcessor;

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {

// Get the HTML source code of the Experience Fragment
String html = getExperienceFragmentHtml(request);
LOG.info("HTML in do get method" + html);
// Set the response content type to JavaScript
response.setContentType("application/javascript");
// Write the HTML source code to the response output stream
PrintWriter out = response.getWriter();
out.write(html);
out.flush();
out.close();
}

private String getExperienceFragmentHtml(SlingHttpServletRequest request) throws ServletException, IOException {
ResourceResolver resourceResolver = request.getResourceResolver();
Resource xfResource = request.getResource();
String xfPath = xfResource.getPath();
Page xfPage = resourceResolver.getResource(xfPath).adaptTo(Page.class);
String html = "";
HttpServletRequest req = requestResponseFactory.createRequest("GET", xfPath);
WCMMode.DISABLED.toRequest(req);
/* Setup response */
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse resp = requestResponseFactory.createResponse(out);

/* Process request through Sling */
requestProcessor.processRequest(req, resp, request.getResourceResolver());
html = out.toString();
LOG.info("HTML" + html);
return html;

}
}

 

Avatar

Correct answer by
Community Advisor

Hi @test566 ,

You can use service user to get the session so that resource can able access path, we had the same issue.

Thank,

Sanjay