Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Accessing Final rendered HTML of component through service

Avatar

Former Community Member

Is there any approach or possibility for accessing specific component html using custom bulit service. We can access data by querying but we need to access full html how it is going to be when it is rendered.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

use the SlingRequestProcessor service to get a rendered page. The point in the referenced cqdump.wordpress.com blog article is, that you should not use a httpclient to access the AEM instance because it requires you to deal with hostnames, ports and account data. If you use the SlingRequestProcessor you don't need it at all.

kind regards,
Jörg

View solution in original post

8 Replies

Avatar

Level 10

Are you talking about writing an AEM service that can read rendered HTML? 

Avatar

Level 10

It looks like you want to access the html generated only by component, you can:-

As html of components resides in .jsp, .html file, you can retrieve by JS and Java Service as well.

Add a id to surround you component html

<div id="text-image-html"> Here is your component html </div>

The page in which you have used text image component you can access like this....

Via JS

data = document.getElementById('text-image-html').innerHTML;

Via Service

Integrate very useful JSOUP library in your project

Make GET request your page path, this will give you complete HTML of that page.

Now using above suggested jsoup you can search for "text-image-html" id.

Let me know if this is not what you are looking for....

Avatar

Employee Advisor

You can use https://sling.apache.org/apidocs/sling6/org/apache/sling/engine/SlingRequestProcessor.html. You can reference this service in your code and invoke the processRequest() method to get the final generated HTML response. You can create synthetic/mock request and response objects and pass them to this method. The final HTML will be returned in the response object you pass. The request URI should point to the resource for which you want the rendered HTML. 

Avatar

Former Community Member

@edubey - Thank you. Yah I am also thinking in similar lines. Did you come across any performance problems when you are parsing full html with the jsoup library.

@Kunal23 - Than you. If possible can you give few steps how to create synthetic request/response and how to use the API you mentioned.

Avatar

Level 10

We did used for some purpose, it works perfect for our case. But again it depends how many pages you have, what exactly you are doing in that service. I don't think getting html will make any performance impact. Whats your use case/ requirements?

Avatar

Former Community Member

Thanks edubey.

I was just going through this blog https://cqdump.wordpress.com/2012/08/01/cq5-requesting-itself/ . Quoting few lines from this blog.

" One way would be to reconstruct the way sling request processing works. The other and easiest way is to just do a HTTP request and do everything like it is supposed to work. But then you need to provide username/password and the port". Here we might need to configure credentials and port. I am just thinking about the Kunal point but I am not getting how to make use of that service like creating syntehtic request /response etc. 

Avatar

Correct answer by
Employee Advisor

Hi,

use the SlingRequestProcessor service to get a rendered page. The point in the referenced cqdump.wordpress.com blog article is, that you should not use a httpclient to access the AEM instance because it requires you to deal with hostnames, ports and account data. If you use the SlingRequestProcessor you don't need it at all.

kind regards,
Jörg

Avatar

Employee Advisor

You can use AEM RequestResponseFactory API to create the dummy request, response objects or else you can write custom classes which implement HttpServletRequest and HttpServletResponse interfaces.  

http://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/contentsync/handler/util/RequestResponseFa...

Example- 

RequestResponseFactory reqFactory; HttpServletRequest req = reqFactory.createRequest("GET", "/content/sites/test.html"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); HttpServletResponse res = reqFactory.createResponse(baos); slingRequestProcessor.processRequest(req,res,resourceResolver); //Get the final HTML output in String String html = new String(baos.toByteArray());