May 13, 2020
Estimated Post Reading Time ~

HTML of a Page in OSGi Service in AEM

In this blog, I will explain one interesting use-case of one of my projects. Here is the user-case-

“whenever we activate a page on author instance we need to get the HTML for the activated page and send this page HTML to another search engine. “

In this blog, I will explain how I got the HTML of the activated page in my CustomTransportHander. Let’s start with questions and answers.

Have you used the HTTPClient API?
No, we have the instructions not to use HTTPClient API to get the HTML. We need to use an internal AEM library for it.

Which APIs you have used?
We completed this task using threeOOTB services.
RequestResponseFactory
SlingRequestProcessor
ResourceResolverFactory

What is the use of RequestResponseFactory?
We have used this service to create HttpServletRequest and HttpServletResponse objects.

What is the use of SlingRequestProcessor?
This service is used to execute the newly created HttpServletRequest.

What is the use of ResourceResolverFactory?
This service is used to create the Resource Resolver object.

What code you have added for getting the HTML of the activated page?
Here is the code snippet-

private String getHTMLForRequestedPage(ReplicationLog replicationLog) {

HttpServletRequest req = requestResponseFactory.createRequest(“GET”, pageURL);

WCMMode.DISABLED.toRequest(req);

ByteArrayOutputStream out = new ByteArrayOutputStream();

HttpServletResponse resp = requestResponseFactory.createResponse(out);

try (ResourceResolver serviceResourceResolver = resolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, SUB_SERVICE))) {

slingRequestProcessor.processRequest(req, resp, serviceResourceResolver);

} catch (IOException | LoginException | ServletException e) {

replicationLog.error(“Exception occured : {}”, e.getMessage());

}

return out.toString();

}

Don’t forget to refer to all of the above-mentioned services.

What are the reference statements for these services?
Here are these-

@Reference
private SlingSettingsService slingSettingsService;

@Reference
private RequestResponseFactory requestResponseFactory;

@Reference
private SlingRequestProcessor slingRequestProcessor;

@Reference
private ResourceResolverFactory resolverFactory;

In the above-defined method, what is the value of pageURL?
pageURL is the page URL with .html extension of the newly activated page. For example-

If author activates a page – /content/demo/en then pageURL value should be /content/demo/en.html.

How are you managing other assets or configurations which got activated with the page?
We have written down this code in such a way that it will handle only the page activation request. It will not work for other assets.

Is this solution worked for you?
Yes, I got the expected results.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.