|
6 | 6 | import java.util.Arrays; |
7 | 7 | import java.util.Collections; |
8 | 8 | import java.util.List; |
9 | | -import java.util.Objects; |
10 | 9 | import java.util.stream.Stream; |
11 | 10 |
|
12 | 11 | import javax.servlet.http.HttpServletRequest; |
|
23 | 22 | */ |
24 | 23 | public class AddHttpHeadersToLogContextFilter extends AbstractLoggingFilter { |
25 | 24 |
|
26 | | - private List<HttpHeader> headers; |
27 | | - private List<String> fields; |
| 25 | + private final List<HttpHeader> headers; |
| 26 | + private final List<String> fields; |
28 | 27 |
|
29 | 28 | /** |
30 | 29 | * The default constructor uses {@link HttpHeaders#propagated()} to define |
@@ -53,16 +52,16 @@ public AddHttpHeadersToLogContextFilter(HttpHeader... headers) { |
53 | 52 | */ |
54 | 53 | public AddHttpHeadersToLogContextFilter(List<? extends HttpHeader> list, HttpHeader... custom) { |
55 | 54 | Stream<HttpHeader> allHeaders = Stream.concat(list.stream(), Arrays.stream(custom)); |
56 | | - this.headers = unmodifiableList(allHeaders.filter(HttpHeader::isPropagated).collect(toList())); |
57 | | - this.fields = unmodifiableList(headers.stream().map(HttpHeader::getField).filter(Objects::nonNull).collect( |
58 | | - toList())); |
| 55 | + this.headers = unmodifiableList(allHeaders |
| 56 | + .filter(HttpHeader::isPropagated).filter(h -> h.getField() != null).collect(toList())); |
| 57 | + this.fields = unmodifiableList(headers.stream().map(HttpHeader::getField).collect(toList())); |
59 | 58 | } |
60 | 59 |
|
61 | 60 | @Override |
62 | 61 | protected void beforeFilter(HttpServletRequest request, HttpServletResponse response) { |
63 | 62 | for (HttpHeader header: headers) { |
64 | 63 | String headerValue = HttpHeaderUtilities.getHeaderValue(request, header); |
65 | | - if (header.getField() != null && headerValue != null) { |
| 64 | + if (headerValue != null) { |
66 | 65 | LogContext.add(header.getField(), headerValue); |
67 | 66 | } |
68 | 67 | } |
|
0 commit comments