Skip to content

Commit 718641a

Browse files
ankurpathakjzheaux
authored andcommitted
Added CompositeHeaderWriter
1. Added new CompositeHeaderWriter 2. Improvement in HeaderWriterFilter using CompositeHeaderWriter. Fixes: gh-6453
1 parent ca02d8a commit 718641a

File tree

3 files changed

+135
-17
lines changed

3 files changed

+135
-17
lines changed

web/src/main/java/org/springframework/security/web/header/HeaderWriterFilter.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import javax.servlet.http.HttpServletRequestWrapper;
2828
import javax.servlet.http.HttpServletResponse;
2929

30+
import org.springframework.security.web.header.writers.CompositeHeaderWriter;
3031
import org.springframework.security.web.util.OnCommittedResponseWrapper;
3132
import org.springframework.util.Assert;
3233
import org.springframework.web.filter.OncePerRequestFilter;
@@ -38,55 +39,55 @@
3839
*
3940
* @author Marten Deinum
4041
* @author Josh Cummings
42+
* @author Ankur Pathak
4143
* @since 3.2
42-
*
4344
*/
4445
public class HeaderWriterFilter extends OncePerRequestFilter {
4546

47+
4648
/**
47-
* Collection of {@link HeaderWriter} instances to write out the headers to the
49+
* Composite {@link CompositeHeaderWriter} containing Collection of {@link HeaderWriter} instances to write out the headers to the
4850
* response.
4951
*/
50-
private final List<HeaderWriter> headerWriters;
52+
private final HeaderWriter headerWriter;
5153

5254
/**
5355
* Creates a new instance.
5456
*
5557
* @param headerWriters the {@link HeaderWriter} instances to write out headers to the
56-
* {@link HttpServletResponse}.
58+
* {@link HttpServletResponse}.
5759
*/
5860
public HeaderWriterFilter(List<HeaderWriter> headerWriters) {
5961
Assert.notEmpty(headerWriters, "headerWriters cannot be null or empty");
60-
this.headerWriters = headerWriters;
62+
this.headerWriter = new CompositeHeaderWriter(headerWriters);
6163
}
6264

6365
@Override
6466
protected void doFilterInternal(HttpServletRequest request,
65-
HttpServletResponse response, FilterChain filterChain)
66-
throws ServletException, IOException {
67+
HttpServletResponse response, FilterChain filterChain)
68+
throws ServletException, IOException {
6769

6870
HeaderWriterResponse headerWriterResponse = new HeaderWriterResponse(request,
69-
response, this.headerWriters);
71+
response, this.headerWriter);
7072
HeaderWriterRequest headerWriterRequest = new HeaderWriterRequest(request,
7173
headerWriterResponse);
7274

7375
try {
7476
filterChain.doFilter(headerWriterRequest, headerWriterResponse);
75-
}
76-
finally {
77+
} finally {
7778
headerWriterResponse.writeHeaders();
7879
}
7980
}
8081

8182
static class HeaderWriterResponse extends OnCommittedResponseWrapper {
8283
private final HttpServletRequest request;
83-
private final List<HeaderWriter> headerWriters;
84+
private final HeaderWriter headerWriter;
8485

8586
HeaderWriterResponse(HttpServletRequest request, HttpServletResponse response,
86-
List<HeaderWriter> headerWriters) {
87+
HeaderWriter headerWriter) {
8788
super(response);
8889
this.request = request;
89-
this.headerWriters = headerWriters;
90+
this.headerWriter = headerWriter;
9091
}
9192

9293
/*
@@ -105,9 +106,7 @@ protected void writeHeaders() {
105106
if (isDisableOnResponseCommitted()) {
106107
return;
107108
}
108-
for (HeaderWriter headerWriter : this.headerWriters) {
109-
headerWriter.writeHeaders(this.request, getHttpResponse());
110-
}
109+
this.headerWriter.writeHeaders(this.request, getHttpResponse());
111110
}
112111

113112
private HttpServletResponse getHttpResponse() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.web.header.writers;
17+
18+
import org.springframework.security.web.header.HeaderWriter;
19+
import org.springframework.util.Assert;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
import java.util.List;
24+
25+
/**
26+
* A {@link HeaderWriter} that delegates to several other {@link HeaderWriter}s.
27+
*
28+
* @author Ankur Pathak
29+
* @since 5.2
30+
*/
31+
public class CompositeHeaderWriter implements HeaderWriter {
32+
33+
private final List<HeaderWriter> headerWriters;
34+
35+
/**
36+
* Creates a new instance.
37+
*
38+
* @param headerWriters the {@link HeaderWriter} instances to write out headers to the
39+
* {@link HttpServletResponse}.
40+
*/
41+
public CompositeHeaderWriter(List<HeaderWriter> headerWriters) {
42+
Assert.notEmpty(headerWriters, "headerWriters cannot be empty");
43+
this.headerWriters = headerWriters;
44+
}
45+
46+
@Override
47+
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
48+
this.headerWriters.forEach(headerWriter -> headerWriter.writeHeaders(request, response));
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.web.header.writers;
17+
18+
import java.util.Arrays;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import org.springframework.mock.web.MockHttpServletRequest;
24+
import org.springframework.mock.web.MockHttpServletResponse;
25+
import org.springframework.security.web.header.HeaderWriter;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for class {@link CompositeHeaderWriter}/.
31+
*
32+
* @author Ankur Pathak
33+
* @since 5.2
34+
*/
35+
public class CompositeHeaderWriterTests {
36+
private MockHttpServletRequest request;
37+
38+
private MockHttpServletResponse response;
39+
40+
private CompositeHeaderWriter writer;
41+
42+
@Before
43+
public void setup() {
44+
this.request = new MockHttpServletRequest();
45+
this.response = new MockHttpServletResponse();
46+
HeaderWriter writerA = (request, response) -> {
47+
if (!response.containsHeader("A")) {
48+
response.setHeader("A", "a");
49+
}
50+
};
51+
52+
HeaderWriter writerB = (request, response) -> {
53+
if (!response.containsHeader("B")) {
54+
response.setHeader("B", "b");
55+
}
56+
};
57+
this.writer = new CompositeHeaderWriter(Arrays.asList(writerA, writerB));
58+
}
59+
60+
61+
@Test
62+
public void doCompositeHeaderWrite(){
63+
this.writer.writeHeaders(request, response);
64+
assertThat(this.response.containsHeader("A")).isTrue();
65+
assertThat(this.response.containsHeader("B")).isTrue();
66+
assertThat(this.response.getHeader("A")).isEqualTo("a");
67+
assertThat(this.response.getHeader("B")).isEqualTo("b");
68+
}
69+
}

0 commit comments

Comments
 (0)