This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git
The following commit(s) were added to refs/heads/master by this push:
new baa566c SLING-12697 do not check for violations when the response is
commited… (#58)
baa566c is described below
commit baa566cec022f85b46e409d65927db20d855c637
Author: Remo Liechti <[email protected]>
AuthorDate: Tue Mar 18 14:17:09 2025 +0100
SLING-12697 do not check for violations when the response is commited… (#58)
* SLING-12697 do not check for violations when the response is commited as
the content type header change will be ignored according to the servlet api
specification - avoid false positives
* SLING-12697 fix sonar issue
---
.../engine/impl/SlingHttpServletResponseImpl.java | 4 ++--
.../engine/impl/SlingHttpServletResponseImplTest.java | 18 ++++++++++++++++++
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/apache/sling/engine/impl/SlingHttpServletResponseImpl.java
b/src/main/java/org/apache/sling/engine/impl/SlingHttpServletResponseImpl.java
index 8fb5b9b..72a9554 100644
---
a/src/main/java/org/apache/sling/engine/impl/SlingHttpServletResponseImpl.java
+++
b/src/main/java/org/apache/sling/engine/impl/SlingHttpServletResponseImpl.java
@@ -325,7 +325,7 @@ public class SlingHttpServletResponseImpl extends
HttpServletResponseWrapper imp
@Override
public void setContentType(final String type) {
- if (!isInclude()) {
+ if (super.getResponse().isCommitted() || !isInclude()) {
super.setContentType(type);
} else {
Optional<String> message = checkContentTypeOverride(type);
@@ -358,7 +358,7 @@ public class SlingHttpServletResponseImpl extends
HttpServletResponseWrapper imp
* @param contentType the 'Content-Type' value that is being set
* @return an optional message to log
*/
- private Optional<String> checkContentTypeOverride(@Nullable String
contentType) {
+ protected Optional<String> checkContentTypeOverride(@Nullable String
contentType) {
if (requestData.getSlingRequestProcessor().getContentTypeHeaderState()
== ContentTypeHeaderState.VIOLATED) {
// return immediatly as the content type header has already been
violated
// prevoiously, no more checks needed
diff --git
a/src/test/java/org/apache/sling/engine/impl/SlingHttpServletResponseImplTest.java
b/src/test/java/org/apache/sling/engine/impl/SlingHttpServletResponseImplTest.java
index 4bf21ff..a76aaa7 100644
---
a/src/test/java/org/apache/sling/engine/impl/SlingHttpServletResponseImplTest.java
+++
b/src/test/java/org/apache/sling/engine/impl/SlingHttpServletResponseImplTest.java
@@ -87,6 +87,24 @@ public class SlingHttpServletResponseImplTest {
"4749 LOG Adding bindings took 4 microseconds"
};
+ @Test
+ public void testNoViolationChecksOnCommitedResponse() {
+ final SlingHttpServletResponse orig =
Mockito.mock(SlingHttpServletResponse.class);
+ Mockito.when(orig.isCommitted()).thenReturn(true);
+
+ final RequestData requestData = mock(RequestData.class);
+ final DispatchingInfo info = new
DispatchingInfo(DispatcherType.INCLUDE);
+ when(requestData.getDispatchingInfo()).thenReturn(info);
+ info.setProtectHeadersOnInclude(true);
+
+ final SlingHttpServletResponseImpl include = new
SlingHttpServletResponseImpl(requestData, orig);
+ SlingHttpServletResponseImpl spyInclude = Mockito.spy(include);
+
+ spyInclude.setContentType("someOtherType");
+ Mockito.verify(orig, times(1)).setContentType(Mockito.any());
+ Mockito.verify(spyInclude,
never()).checkContentTypeOverride(Mockito.any());
+ }
+
@Test
public void testReset() {
final SlingHttpServletResponse orig =
mock(SlingHttpServletResponse.class);