adobe / aem-core-wcm-components

Standardized components to build websites with AEM.
https://docs.adobe.com/content/help/en/experience-manager-core-components/using/introduction.html
Apache License 2.0
730 stars 736 forks source link

SITES-16805: init rewriter processor at the right time with toc enabled #2604

Closed buuhuu closed 9 months ago

buuhuu commented 10 months ago
Q                       A
Fixed Issues?
Patch: Bug Fix? 👍
Minor: New Feature? No
Major: Breaking Change? No
Tests Added + Pass? N/A
Documentation Provided Yes
Any Dependency Changes? No
License Apache License, Version 2.0

With the TOC Filter enabled the resourceType condition evaluation for the Sling Rewriter Pipeline configuration is not applied on the usual resource anymore: https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter.html#configuring-a-processor

Usually (without the TOC Filter) the following happens:

/content/path/to/page (cq:Page)
 - jcr:content (sling:resourceType=myapp/components/page)

The /libs/cq/Page/Page.jsp includes the jcr:content child transparently without writing anything to the response, hence not calling HttpServletResponse#getWriter(). The myapp/components/pagE or any of its super types contains usually the first script that writes to the response, hence calls HttpServletResponse#getWriter() which initialises the Sling Rewriter processing as a side-effect with the jcr:content resource and it in particular its resourceType as context: https://github.com/apache/sling-org-apache-sling-rewriter/blob/master/src/main/java/org/apache/sling/rewriter/impl/RewriterResponse.java#L89-L119

With the TOC Filter enabled, the response is wrapped and its writer replaced with some in-memory buffering writer. The content of the buffer is than processed and written to the original writer after calling HttpServletResponse#getWriter(). That works functionally, however, as HttpServletResponse#getWriter() is called after the page was rendered the response's upstream getWriter() is only called now, initialising the Sling Rewriter processor in the context of the cq:Page instead of the jcr:content.

To see this in action, check the /system/console/request for the same page with the filter enabled/disabled and look for LOG Found processor for post processing ProcessorConfiguration, it should appear right after the include of the jcr:content resource's main rendering script. With the TOC Filter enabled, it appears at the end of the whole rendering.

With this PR the upstream getWriter() is called at the time the CharResponseWrapper#getWriter() is invoked. This triggers the side-effect to initialise the Sling Rewriter processor at the right time.

codecov[bot] commented 10 months ago

Codecov Report

Merging #2604 (ceff4a1) into main (aaa5fbe) will increase coverage by 0.00%. The diff coverage is 100.00%.

:exclamation: Current head ceff4a1 differs from pull request most recent head c745a00. Consider uploading reports for the commit c745a00 to get more accurate results

@@            Coverage Diff            @@
##               main    #2604   +/-   ##
=========================================
  Coverage     87.34%   87.34%           
  Complexity     2648     2648           
=========================================
  Files           232      232           
  Lines          7078     7080    +2     
  Branches       1073     1073           
=========================================
+ Hits           6182     6184    +2     
  Misses          359      359           
  Partials        537      537           
Files Coverage Δ
...ponents/internal/servlets/CharResponseWrapper.java 100.00% <100.00%> (ø)

:mega: Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

sonarcloud[bot] commented 9 months ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication