Skip to content

Commit

Permalink
Merge pull request #184 from j-denner/minor-corrections
Browse files Browse the repository at this point in the history
AddHttpHeaderToLogContextFilter: Filter immediately for getField presence
  • Loading branch information
KarstenSchnitter authored Jul 19, 2024
2 parents 9732fe3 + a4475ef commit 08385c2
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -23,8 +22,8 @@
*/
public class AddHttpHeadersToLogContextFilter extends AbstractLoggingFilter {

private List<HttpHeader> headers;
private List<String> fields;
private final List<HttpHeader> headers;
private final List<String> fields;

/**
* The default constructor uses {@link HttpHeaders#propagated()} to define
Expand Down Expand Up @@ -53,16 +52,16 @@ public AddHttpHeadersToLogContextFilter(HttpHeader... headers) {
*/
public AddHttpHeadersToLogContextFilter(List<? extends HttpHeader> list, HttpHeader... custom) {
Stream<HttpHeader> allHeaders = Stream.concat(list.stream(), Arrays.stream(custom));
this.headers = unmodifiableList(allHeaders.filter(HttpHeader::isPropagated).collect(toList()));
this.fields = unmodifiableList(headers.stream().map(HttpHeader::getField).filter(Objects::nonNull).collect(
toList()));
this.headers = unmodifiableList(allHeaders
.filter(HttpHeader::isPropagated).filter(h -> h.getField() != null).collect(toList()));
this.fields = unmodifiableList(headers.stream().map(HttpHeader::getField).collect(toList()));
}

@Override
protected void beforeFilter(HttpServletRequest request, HttpServletResponse response) {
for (HttpHeader header: headers) {
String headerValue = HttpHeaderUtilities.getHeaderValue(request, header);
if (header.getField() != null && headerValue != null) {
if (headerValue != null) {
LogContext.add(header.getField(), headerValue);
}
}
Expand Down

0 comments on commit 08385c2

Please sign in to comment.