diff options
author | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2020-12-23 15:47:38 +0100 |
---|---|---|
committer | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2020-12-23 15:47:38 +0100 |
commit | db3d586b9937e8a556c499faa485a2d9e4a03f81 (patch) | |
tree | d7dbc6821d2b3499e26492690fb3536cffce4ce9 /eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java | |
parent | 04cec6a9aa085bba0736cc6d55cb6c8b4d54a0a0 (diff) | |
download | EAAF-Components-db3d586b9937e8a556c499faa485a2d9e4a03f81.tar.gz EAAF-Components-db3d586b9937e8a556c499faa485a2d9e4a03f81.tar.bz2 EAAF-Components-db3d586b9937e8a556c499faa485a2d9e4a03f81.zip |
add new module that include common-code for SpringBoot applications
Diffstat (limited to 'eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java')
-rw-r--r-- | eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java | 99 |
1 files changed, 99 insertions, 0 deletions
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 new file mode 100644 index 00000000..d63c47c9 --- /dev/null +++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/ajp/logging/MdcEnhancerFilter.java @@ -0,0 +1,99 @@ +package at.gv.egiz.eaaf.utils.springboot.ajp.logging; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +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; + +@Component +@EnableConfigurationProperties(LoggingProperties.class) +public class MdcEnhancerFilter implements Filter { + + /** + * Logging properties. + */ + @Autowired + private LoggingProperties loggingProperties; + + /** + * {@inheritDoc} + */ + @Override + public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, + final FilterChain filterChain) throws IOException, ServletException { + final HttpServletRequest request = (HttpServletRequest) servletRequest; + + 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)) { + MDC.put(key, value); + } else if (loggingProperties.getMdc().getNullValue() != null) { + MDC.put(key, loggingProperties.getMdc().getNullValue()); + } + } + + Object object; + for (final String attribute : loggingProperties.getMdc().getSessionAttributes()) { + key = loggingProperties.getMdc().getSessionAttributePrefix() + attribute + loggingProperties.getMdc() + .getSessionAttributePostfix(); + object = request.getSession(true).getAttribute(attribute); + if (object != null) { + MDC.put(key, object.toString()); + } else if (loggingProperties.getMdc().getNullValue() != null) { + MDC.put(key, loggingProperties.getMdc().getNullValue()); + } + } + + try { + filterChain.doFilter(servletRequest, servletResponse); + } finally { + for (final String header : loggingProperties.getMdc().getHeaders()) { + MDC.remove(header); + } + for (final String cookie : loggingProperties.getMdc().getCookies()) { + MDC.remove(cookie); + } + for (final String attribute : loggingProperties.getMdc().getSessionAttributes()) { + MDC.remove(attribute); + } + } + } + + private static String getCookie(final String cookie, final Cookie[] cookies) { + if (cookies == null || StringUtils.isEmpty(cookie)) { + return null; + } + for (final Cookie c : cookies) { + if (c.getName().equals(cookie)) { + return c.getValue(); + } + } + return null; + } +}
\ No newline at end of file |