private Map<String, Object> getRequestHeaders(HttpServletRequest request) { Map<String, Object> headers = new LinkedHashMap<String, Object>(); Enumeration<String> names = request.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); List<String> values = Collections.list(request.getHeaders(name)); Object value = values; if (values.size() == 1) { value = values.get(0); } else if (values.isEmpty()) { value = ""; } headers.put(name, value); } if (!isIncluded(Include.COOKIES)) { headers.remove("Cookie"); } postProcessRequestHeaders(headers); return headers; }
protected Map<String, Object> getTrace(HttpServletRequest request) { HttpSession session = request.getSession(false); Throwable exception = (Throwable) request .getAttribute("javax.servlet.error.exception"); Principal userPrincipal = request.getUserPrincipal(); Map<String, Object> trace = new LinkedHashMap<String, Object>(); Map<String, Object> headers = new LinkedHashMap<String, Object>(); trace.put("method", request.getMethod()); trace.put("path", request.getRequestURI()); trace.put("headers", headers); if (isIncluded(Include.REQUEST_HEADERS)) { headers.put("request", getRequestHeaders(request)); } add(trace, Include.PATH_INFO, "pathInfo", request.getPathInfo()); add(trace, Include.PATH_TRANSLATED, "pathTranslated", request.getPathTranslated()); add(trace, Include.CONTEXT_PATH, "contextPath", request.getContextPath()); add(trace, Include.USER_PRINCIPAL, "userPrincipal", (userPrincipal == null ? null : userPrincipal.getName())); if (isIncluded(Include.PARAMETERS)) { trace.put("parameters", request.getParameterMap()); } add(trace, Include.QUERY_STRING, "query", request.getQueryString()); add(trace, Include.AUTH_TYPE, "authType", request.getAuthType()); add(trace, Include.REMOTE_ADDRESS, "remoteAddress", request.getRemoteAddr()); add(trace, Include.SESSION_ID, "sessionId", (session == null ? null : session.getId())); add(trace, Include.REMOTE_USER, "remoteUser", request.getRemoteUser()); if (isIncluded(Include.ERRORS) && exception != null && this.errorAttributes != null) { trace.put("error", this.errorAttributes .getErrorAttributes(new ServletRequestAttributes(request), true)); } return trace; }
@SuppressWarnings("unchecked") protected void enhanceTrace(Map<String, Object> trace, HttpServletResponse response) { if (isIncluded(Include.RESPONSE_HEADERS)) { Map<String, Object> headers = (Map<String, Object>) trace.get("headers"); headers.put("response", getResponseHeaders(response)); } }
private Map<String, String> getResponseHeaders(HttpServletResponse response) { Map<String, String> headers = new LinkedHashMap<String, String>(); for (String header : response.getHeaderNames()) { String value = response.getHeader(header); headers.put(header, value); } if (!isIncluded(Include.COOKIES)) { headers.remove("Set-Cookie"); } headers.put("status", "" + response.getStatus()); return headers; }
@Test @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddRequestCookiesWithCookiesExclude() throws ServletException, IOException { this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS)); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); request.addHeader("Accept", "application/json"); request.addHeader("Cookie", "testCookie=testValue;"); Map<String, Object> map = (Map<String, Object>) this.filter.getTrace(request) .get("headers"); assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}"); }
@Test @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddResponseCookiesWithCookiesExclude() throws ServletException, IOException { this.properties.setInclude(Collections.singleton(Include.RESPONSE_HEADERS)); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); response.addHeader("Set-Cookie", "testCookie=testValue;"); Map<String, Object> trace = this.filter.getTrace(request); this.filter.enhanceTrace(trace, response); Map<String, Object> map = (Map<String, Object>) trace.get("headers"); assertThat(map.get("response").toString()) .isEqualTo("{Content-Type=application/json, status=200}"); }
protected Map<String, Object> getTrace(HttpServletRequest request) { HttpSession session = request.getSession(false); Throwable exception = (Throwable) request .getAttribute("javax.servlet.error.exception"); Principal userPrincipal = request.getUserPrincipal(); Map<String, Object> trace = new LinkedHashMap<String, Object>(); Map<String, Object> headers = new LinkedHashMap<String, Object>(); trace.put("method", request.getMethod()); trace.put("path", request.getRequestURI()); trace.put("headers", headers); if (isIncluded(Include.REQUEST_HEADERS)) { headers.put("request", getRequestHeaders(request)); } add(trace, Include.PATH_INFO, "pathInfo", request.getPathInfo()); add(trace, Include.PATH_TRANSLATED, "pathTranslated", request.getPathTranslated()); add(trace, Include.CONTEXT_PATH, "contextPath", request.getContextPath()); add(trace, Include.USER_PRINCIPAL, "userPrincipal", (userPrincipal == null ? null : userPrincipal.getName())); add(trace, Include.PARAMETERS, "parameters", request.getParameterMap()); add(trace, Include.QUERY_STRING, "query", request.getQueryString()); add(trace, Include.AUTH_TYPE, "authType", request.getAuthType()); add(trace, Include.REMOTE_ADDRESS, "remoteAddress", request.getRemoteAddr()); add(trace, Include.SESSION_ID, "sessionId", (session == null ? null : session.getId())); add(trace, Include.REMOTE_USER, "remoteUser", request.getRemoteUser()); if (isIncluded(Include.ERRORS) && exception != null && this.errorAttributes != null) { trace.put("error", this.errorAttributes .getErrorAttributes(new ServletRequestAttributes(request), true)); } return trace; }
private void add(Map<String, Object> trace, Include include, String name, Object value) { if (isIncluded(include) && value != null) { trace.put(name, value); } }
private boolean isIncluded(Include include) { return this.properties.getInclude().contains(include); }