added request method to mdc filter

This commit is contained in:
Sebastian Sdorra
2016-06-28 12:06:28 +02:00
parent 7ef8e1ebd5
commit 7d8613b6bb
2 changed files with 8 additions and 0 deletions

View File

@@ -73,6 +73,10 @@ public class MDCFilter extends HttpFilter
@VisibleForTesting @VisibleForTesting
static final String MDC_REQUEST_URI = "request_uri"; static final String MDC_REQUEST_URI = "request_uri";
/** request method */
@VisibleForTesting
static final String MDC_REQUEST_METHOD = "request_method";
/** Field description */ /** Field description */
@VisibleForTesting @VisibleForTesting
static final String MDC_USERNAME = "username"; static final String MDC_USERNAME = "username";
@@ -98,6 +102,7 @@ public class MDCFilter extends HttpFilter
MDC.put(MDC_USERNAME, getUsername()); MDC.put(MDC_USERNAME, getUsername());
MDC.put(MDC_CLIEN_IP, request.getRemoteAddr()); MDC.put(MDC_CLIEN_IP, request.getRemoteAddr());
MDC.put(MDC_CLIEN_HOST, request.getRemoteHost()); MDC.put(MDC_CLIEN_HOST, request.getRemoteHost());
MDC.put(MDC_REQUEST_METHOD, request.getMethod());
MDC.put(MDC_REQUEST_URI, request.getRequestURI()); MDC.put(MDC_REQUEST_URI, request.getRequestURI());
try try
@@ -109,6 +114,7 @@ public class MDCFilter extends HttpFilter
MDC.remove(MDC_USERNAME); MDC.remove(MDC_USERNAME);
MDC.remove(MDC_CLIEN_IP); MDC.remove(MDC_CLIEN_IP);
MDC.remove(MDC_CLIEN_HOST); MDC.remove(MDC_CLIEN_HOST);
MDC.remove(MDC_REQUEST_METHOD);
MDC.remove(MDC_REQUEST_URI); MDC.remove(MDC_REQUEST_URI);
} }
} }

View File

@@ -87,6 +87,7 @@ public class MDCFilterTest extends AbstractTestBase {
when(request.getRequestURI()).thenReturn("api/v1/repositories"); when(request.getRequestURI()).thenReturn("api/v1/repositories");
when(request.getRemoteAddr()).thenReturn("127.0.0.1"); when(request.getRemoteAddr()).thenReturn("127.0.0.1");
when(request.getRemoteHost()).thenReturn("localhost"); when(request.getRemoteHost()).thenReturn("localhost");
when(request.getMethod()).thenReturn("GET");
MDCCapturingFilterChain chain = new MDCCapturingFilterChain(); MDCCapturingFilterChain chain = new MDCCapturingFilterChain();
filter.doFilter(request, response, chain); filter.doFilter(request, response, chain);
@@ -96,6 +97,7 @@ public class MDCFilterTest extends AbstractTestBase {
assertEquals("api/v1/repositories", chain.ctx.get(MDCFilter.MDC_REQUEST_URI)); assertEquals("api/v1/repositories", chain.ctx.get(MDCFilter.MDC_REQUEST_URI));
assertEquals("127.0.0.1", chain.ctx.get(MDCFilter.MDC_CLIEN_IP)); assertEquals("127.0.0.1", chain.ctx.get(MDCFilter.MDC_CLIEN_IP));
assertEquals("localhost", chain.ctx.get(MDCFilter.MDC_CLIEN_HOST)); assertEquals("localhost", chain.ctx.get(MDCFilter.MDC_CLIEN_HOST));
assertEquals("GET", chain.ctx.get(MDCFilter.MDC_REQUEST_METHOD));
} }
/** /**