Accessing Final rendered HTML of component through service | Community
Skip to main content
October 16, 2015
Solved

Accessing Final rendered HTML of component through service

  • October 16, 2015
  • 8 replies
  • 7074 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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

8 replies

smacdonald2008
Level 10
October 16, 2015

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

edubey
Level 10
October 16, 2015

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....

Kunal_Gaba_
October 16, 2015

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. 

October 16, 2015

@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.

edubey
Level 10
October 16, 2015

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?

October 16, 2015

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. 

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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

Kunal_Gaba_
October 16, 2015

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/RequestResponseFactory.html

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());