Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQ5.5 get .infinity.json of a resource in a servlet

Tags:

aem

sling

If we had component resource set up as such:

  • mycomponent
    • mycomponent.jsp
    • mycomponent.JSON.jsp

We can Assume it will work as such:

  • /path/to/mycomponent.html => see html
  • /path/to/mycomponent.json => see my json

Also in a servlet we might be able to do something like

Resource myResource = resourceResolver.getResource(request, "path/to/mycomponent");

I'm just curious how I might be able to get the .json representation in a servlet context as well.

I've done something that sort of solves this, but I was wondering if there was an alternate way, as this solution has huge limitations. Basically I load the Node at the path and do JSONDumps of the Node and it's children. This would allow me to get a larger set of JSON from the resource at the mycomponent.getPath(), but it doesn't allow me the ability to pull the custom JSON view i've created via mycomponent.JSON.jsp.

any thoughts/advice would be great, thank you.

like image 837
Brodie Avatar asked Feb 14 '23 04:02

Brodie


1 Answers

To capture the output of rendering a resource you can use the SlingRequestProcessor service, which makes a request internally without going through the network layers but still using all the same rendering mechanisms that are used to process HTTP requests.

If you just need to include such output in the rendering that you are computing you can use Request.getRequestDispatcher(somePathWithJsonExtension).include(request, response) which is what the Sling and CQ include JSP tags do.

Using resourceResolver.getResource(...) does not do any rendering, it just provides a raw Resource which is a data/content object.

like image 116
Bertrand Delacretaz Avatar answered Feb 23 '23 23:02

Bertrand Delacretaz