@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest requestLocal = request; // Determine if this is the first request or not. We only want to wrap the request to log on the first request. boolean isFirstRequest = !isAsyncDispatch(requestLocal); if (isFirstRequest) { requestLocal = new RequestLoggingFilterWrapper(requestLocal); ((RequestLoggingFilterWrapper) requestLocal).logRequest(request); } // Move onto the next filter while wrapping the request with our own custom logging class. try { filterChain.doFilter(requestLocal, response); } finally { // Log the request after it is processed. We only log the first request. if (!isAsyncStarted(requestLocal) && requestLocal instanceof RequestLoggingFilterWrapper) { ((RequestLoggingFilterWrapper) requestLocal).logAfterRequest(request, response); } } }
private RequestLoggingFilter createFilter() { RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(); requestLoggingFilter.setIncludeClientInfo(true); requestLoggingFilter.setIncludePayload(true); requestLoggingFilter.setIncludeQueryString(true); requestLoggingFilter.setLogMessagePrefix("Log Message: ["); requestLoggingFilter.setLogMessageSuffix("]"); requestLoggingFilter.setMaxPayloadLength(null); return requestLoggingFilter; } }
@Test public void testDoFilterAllOptionsFalse() throws Exception { RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(); requestLoggingFilter.setIncludeClientInfo(false); requestLoggingFilter.setIncludePayload(false); requestLoggingFilter.setIncludeQueryString(false); // Run the filter. requestLoggingFilter.doFilter(createServletRequest(), createServletResponse(), createFilterChain()); }
@Test public void testDoFilter() throws Exception { // Run the filter. createFilter().doFilter(createServletRequest(), createServletResponse(), createFilterChain()); }
/** * Test to get the coverage for unused implemented methods of RequestLoggingFilterWrapper. */ @Test public void testRequestLoggingFilterWrapper() throws Exception { HttpServletRequest servletRequest = createServletRequest(); servletRequest.setCharacterEncoding(WebUtils.DEFAULT_CHARACTER_ENCODING); RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(); RequestLoggingFilter.RequestLoggingFilterWrapper wrapper = requestLoggingFilter.new RequestLoggingFilterWrapper(servletRequest); wrapper.logRequest(servletRequest); wrapper.getContentLength(); wrapper.getCharacterEncoding(); wrapper.getReader(); wrapper.getReader(); }
@Test public void testDoFilterMaxPayloadLength() throws Exception { RequestLoggingFilter requestLoggingFilter = new RequestLoggingFilter(); requestLoggingFilter.setMaxPayloadLength(4); // Run the filter. requestLoggingFilter.doFilter(createServletRequest(), createServletResponse(), createFilterChain()); }
@Override public BufferedReader getReader() throws IOException { if (payload == null) { return super.getReader(); } else { if (reader == null) { this.reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(payload), getCharacterEncoding())); } return reader; } }
@Before public void setup() throws Exception { // Turn on debug logging which is what enables the logging by the filter. setLogLevel(RequestLoggingFilter.class, LogLevel.DEBUG); super.setup(); }
/** * Initializes the request logging filter that logs all incoming REST requests. * * @param servletContext the servlet context. */ protected void initRequestLoggingFilter(ServletContext servletContext) { // Add a filter that logs incoming HTTP request and configure flags to enable more detailed logging. FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("requestLoggingFilter", new RequestLoggingFilter()); filterRegistration.addMappingForUrlPatterns(null, true, "/rest/*"); }
@Test public void testHandleException() throws Exception { executeWithoutLogging(HerdUiControllerAdvice.class, new Command() { @Override public void execute() { // Non-user exceptions should only contain the view and no message in the model. // Calling handleException will log a stack trace which is normal so don't be concerned if you see it in the logs. ModelAndView modelAndView = controllerAdvice.handleException(new Exception(MESSAGE)); assertTrue(UiConstants.DISPLAY_ERROR_MESSAGE_PAGE.equals(modelAndView.getViewName())); assertNull(modelAndView.getModel().get(UiConstants.MODEL_KEY_MESSAGE)); } }); }
/** * Log the request message after the request is processed. * * @param request the http request * @param response the http response */ public void logAfterRequest(HttpServletRequest request, HttpServletResponse response) { StringBuilder message = new StringBuilder(); // Append the log message prefix. message.append(AFTER_REQUEST_LOG_MESSAGE_PREFIX); // Append the URI. message.append("uri=").append(request.getRequestURI()); // Append the query string if present. if (isIncludeQueryString() && StringUtils.hasText(request.getQueryString())) { message.append('?').append(request.getQueryString()); } // Append the HTTP method. message.append(" method=").append(request.getMethod()); // Append the HTTP response status code message.append(" status=").append(response.getStatus()); // Append response time message.append(" responseTimeMillis=").append(System.currentTimeMillis() - requestBeginTimeMillis); // Log the actual message. LOGGER.debug(message.toString()); } }
@Test public void testDoFilterNoDebug() throws Exception { // Turn on info logging which will disable the core functionality of the filter (i.e. no logging). setLogLevel(RequestLoggingFilter.class, LogLevel.INFO); // Run the filter. createFilter().doFilter(createServletRequest(), createServletResponse(), createFilterChain()); }
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest requestLocal = request; // Determine if this is the first request or not. We only want to wrap the request to log on the first request. boolean isFirstRequest = !isAsyncDispatch(requestLocal); if (isFirstRequest) { requestLocal = new RequestLoggingFilterWrapper(requestLocal); ((RequestLoggingFilterWrapper) requestLocal).logRequest(request); } // Move onto the next filter while wrapping the request with our own custom logging class. try { filterChain.doFilter(requestLocal, response); } finally { // Log the request after it is processed. We only log the first request. if (!isAsyncStarted(requestLocal) && requestLocal instanceof RequestLoggingFilterWrapper) { ((RequestLoggingFilterWrapper) requestLocal).logAfterRequest(request, response); } } }
@Override public BufferedReader getReader() throws IOException { if (payload == null) { return super.getReader(); } else { if (reader == null) { this.reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(payload), getCharacterEncoding())); } return reader; } }
@Test public void testDoFilterNoPayload() throws Exception { MockHttpServletRequest request = createServletRequest(); request.setContent(null); // Run the filter. createFilter().doFilter(request, createServletResponse(), createFilterChain()); }
@Test public void testDoFilterNoClient() throws Exception { MockHttpServletRequest request = createServletRequest(); request.setRemoteAddr(null); // Run the filter. createFilter().doFilter(request, createServletResponse(), createFilterChain()); }
@Test public void testDoFilterReadInputStreamFromFilterChainWithPayload() throws Exception { FilterChain filterChain = new MockFilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { String payload = IOUtils.toString(request.getInputStream()); assertEquals(payload, PAYLOAD_CONTENT); } }; // Run the filter. createFilter().doFilter(createServletRequest(), createServletResponse(), filterChain); }
@Test public void testDoFilterNullInputStream() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest() { public ServletInputStream getInputStream() { return null; } }; // Run the filter. createFilter().doFilter(request, createServletResponse(), createFilterChain()); }
@Test public void testDoFilterReadInputStreamFromFilterChainWithNoPayload() throws Exception { FilterChain filterChain = new MockFilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { String payload = IOUtils.toString(request.getInputStream()); assertEquals("", payload); } }; MockHttpServletRequest request = createServletRequest(); request.setContent(null); // Run the filter. createFilter().doFilter(request, createServletResponse(), filterChain); }
@Test public void testDoFilterReadInputStreamFromFilterChainWithNoPayloadNoDebugLevel() throws Exception { // Turn on info logging which will disable the core functionality of the filter (i.e. no logging). setLogLevel(RequestLoggingFilter.class, LogLevel.INFO); FilterChain filterChain = new MockFilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { String payload = IOUtils.toString(request.getInputStream()); assertEquals("", payload); } }; MockHttpServletRequest request = createServletRequest(); request.setContent(null); // Run the filter. createFilter().doFilter(request, createServletResponse(), filterChain); }