summaryrefslogtreecommitdiff
path: root/eaaf-springboot-utils
diff options
context:
space:
mode:
Diffstat (limited to 'eaaf-springboot-utils')
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java9
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java87
2 files changed, 75 insertions, 21 deletions
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java
index b3d5d846..06226e6e 100644
--- a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/LoggingProperties.java
@@ -2,6 +2,7 @@ package at.gv.egiz.eaaf.utils.springboot.ajp.logging;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -47,12 +48,12 @@ public class LoggingProperties {
* Whether to use Logback's MDC.
*/
private boolean enabled = false;
+
/**
- * List of HTTP Headers to make available in Logback's MDC.
+ * A Map of [MDCKey, HttpHeaderName] tuples that are to be saved into MDC.
*/
- private List<String> headers = Collections.emptyList();
- private String headerPrefix = "";
- private String headerPostfix = "";
+ private Map<String, String> headerMap = Collections.emptyMap();
+
/**
* List of HTTP Cookies to make available in Logback's MDC.
*/
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java
index d63c47c9..a1f59f66 100644
--- a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java
@@ -1,6 +1,8 @@
package at.gv.egiz.eaaf.utils.springboot.ajp.logging;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Optional;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -12,10 +14,11 @@ import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
+import lombok.Getter;
+
@Component
@EnableConfigurationProperties(LoggingProperties.class)
public class MdcEnhancerFilter implements Filter {
@@ -23,10 +26,48 @@ public class MdcEnhancerFilter implements Filter {
/**
* Logging properties.
*/
- @Autowired
private LoggingProperties loggingProperties;
/**
+ * To reduce the overhead of building the final "headerMap".
+ *
+ * <p>
+ * The map is to be built when the object is initiated.
+ * </p>
+ */
+ private final HashMap<String, String> headerMap;
+
+ /**
+ * Create a new instance of {@link MdcEnhancerFilter}.
+ *
+ * @param loggingProperties an instance of {@link LoggingProperties}
+ */
+ public MdcEnhancerFilter(LoggingProperties loggingProperties) {
+ this.loggingProperties = loggingProperties;
+ this.headerMap = new HashMap<>(this.loggingProperties.getMdc().getHeaderMap());
+
+ // add TXID into map (no overrides)
+ for (final Txid txid : Txid.values()) {
+ this.headerMap.putIfAbsent(txid.getFieldName(), txid.getHeaderName());
+ }
+ }
+
+ /**
+ * Filter the request headers based on the configured MDC properties.
+ *
+ * @param request incoming {@link HttpServletRequest}
+ */
+ private void filterHeader(final HttpServletRequest request) {
+ this.headerMap.forEach((field, header) -> {
+ // to provide backwards compatibility we have to put the "null-value" into the
+ // MDC if it is not present
+ final String value = Optional.ofNullable(request.getHeader(header))
+ .orElse(this.loggingProperties.getMdc().getNullValue());
+ MDC.put(field, value);
+ });
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
@@ -34,24 +75,15 @@ public class MdcEnhancerFilter implements Filter {
final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
+ filterHeader(request);
+
String key;
String value;
- for (final String header : loggingProperties.getMdc().getHeaders()) {
- key = loggingProperties.getMdc().getHeaderPrefix() + header + loggingProperties.getMdc()
- .getHeaderPostfix();
- value = request.getHeader(header);
- if (!StringUtils.isEmpty(value)) {
- MDC.put(key, value);
- } else if (loggingProperties.getMdc().getNullValue() != null) {
- MDC.put(key, loggingProperties.getMdc().getNullValue());
- }
- }
-
for (final String cookie : loggingProperties.getMdc().getCookies()) {
key = loggingProperties.getMdc().getCookiePrefix() + cookie + loggingProperties.getMdc()
.getCookiePostfix();
value = getCookie(cookie, request.getCookies());
- if (!StringUtils.isEmpty(value)) {
+ if (StringUtils.isNotBlank(value)) {
MDC.put(key, value);
} else if (loggingProperties.getMdc().getNullValue() != null) {
MDC.put(key, loggingProperties.getMdc().getNullValue());
@@ -73,8 +105,8 @@ public class MdcEnhancerFilter implements Filter {
try {
filterChain.doFilter(servletRequest, servletResponse);
} finally {
- for (final String header : loggingProperties.getMdc().getHeaders()) {
- MDC.remove(header);
+ for (final String field : this.headerMap.keySet()) {
+ MDC.remove(field);
}
for (final String cookie : loggingProperties.getMdc().getCookies()) {
MDC.remove(cookie);
@@ -86,7 +118,7 @@ public class MdcEnhancerFilter implements Filter {
}
private static String getCookie(final String cookie, final Cookie[] cookies) {
- if (cookies == null || StringUtils.isEmpty(cookie)) {
+ if (cookies == null || !StringUtils.isNotBlank(cookie)) {
return null;
}
for (final Cookie c : cookies) {
@@ -96,4 +128,25 @@ public class MdcEnhancerFilter implements Filter {
}
return null;
}
+
+ /**
+ * Enumeration of default TXIDs handled by the auto-configuration.
+ */
+ @Getter
+ private enum Txid {
+ HTTP_REQUEST("AM-TXID-HTTP-Request", "amTxidHttpRequest"),
+ BROWSER("AM-TXID-Browser-Session", "amTxidBrowserSession"),
+ IDP("AM-TXID-IdP-Session", "amTxidIdpSession"),
+ SP("AM-TXID-SP-Session", "amTxidSpSession");
+
+ private final String headerName;
+ private final String fieldName;
+
+ Txid(final String headerName, final String fieldName) {
+ this.headerName = headerName;
+ this.fieldName = fieldName;
+ }
+
+ }
+
} \ No newline at end of file