From a4475ef340a2dfa5aa123a86662f5d80ee36a7aa Mon Sep 17 00:00:00 2001 From: d044677 Date: Tue, 9 Jul 2024 08:10:39 +0200 Subject: [PATCH] AddHttpHeaderToLogContextFilter: Filter immediately for getField presence * Previously the present of a field name was checked after fetchin the header value and then dropped. This can be done once and during initialization. --- .../filter/AddHttpHeadersToLogContextFilter.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/AddHttpHeadersToLogContextFilter.java b/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/AddHttpHeadersToLogContextFilter.java index 66c4f994..cbf4ccd6 100644 --- a/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/AddHttpHeadersToLogContextFilter.java +++ b/cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/AddHttpHeadersToLogContextFilter.java @@ -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; @@ -23,8 +22,8 @@ */ public class AddHttpHeadersToLogContextFilter extends AbstractLoggingFilter { - private List headers; - private List fields; + private final List headers; + private final List fields; /** * The default constructor uses {@link HttpHeaders#propagated()} to define @@ -53,16 +52,16 @@ public AddHttpHeadersToLogContextFilter(HttpHeader... headers) { */ public AddHttpHeadersToLogContextFilter(List list, HttpHeader... custom) { Stream 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); } }