Skip to content

Commit baa566c

Browse files
authored
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
1 parent 3cf85e6 commit baa566c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/main/java/org/apache/sling/engine/impl/SlingHttpServletResponseImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private String getCurrentStackTrace() {
325325

326326
@Override
327327
public void setContentType(final String type) {
328-
if (!isInclude()) {
328+
if (super.getResponse().isCommitted() || !isInclude()) {
329329
super.setContentType(type);
330330
} else {
331331
Optional<String> message = checkContentTypeOverride(type);
@@ -358,7 +358,7 @@ public void setContentType(final String type) {
358358
* @param contentType the 'Content-Type' value that is being set
359359
* @return an optional message to log
360360
*/
361-
private Optional<String> checkContentTypeOverride(@Nullable String contentType) {
361+
protected Optional<String> checkContentTypeOverride(@Nullable String contentType) {
362362
if (requestData.getSlingRequestProcessor().getContentTypeHeaderState() == ContentTypeHeaderState.VIOLATED) {
363363
// return immediatly as the content type header has already been violated
364364
// prevoiously, no more checks needed

src/test/java/org/apache/sling/engine/impl/SlingHttpServletResponseImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ public class SlingHttpServletResponseImplTest {
8787
"4749 LOG Adding bindings took 4 microseconds"
8888
};
8989

90+
@Test
91+
public void testNoViolationChecksOnCommitedResponse() {
92+
final SlingHttpServletResponse orig = Mockito.mock(SlingHttpServletResponse.class);
93+
Mockito.when(orig.isCommitted()).thenReturn(true);
94+
95+
final RequestData requestData = mock(RequestData.class);
96+
final DispatchingInfo info = new DispatchingInfo(DispatcherType.INCLUDE);
97+
when(requestData.getDispatchingInfo()).thenReturn(info);
98+
info.setProtectHeadersOnInclude(true);
99+
100+
final SlingHttpServletResponseImpl include = new SlingHttpServletResponseImpl(requestData, orig);
101+
SlingHttpServletResponseImpl spyInclude = Mockito.spy(include);
102+
103+
spyInclude.setContentType("someOtherType");
104+
Mockito.verify(orig, times(1)).setContentType(Mockito.any());
105+
Mockito.verify(spyInclude, never()).checkContentTypeOverride(Mockito.any());
106+
}
107+
90108
@Test
91109
public void testReset() {
92110
final SlingHttpServletResponse orig = mock(SlingHttpServletResponse.class);

0 commit comments

Comments
 (0)