1 package org.openkilda.security.filter;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
6 import org.springframework.web.filter.OncePerRequestFilter;
8 import com.fasterxml.jackson.core.JsonParseException;
9 import com.fasterxml.jackson.databind.JsonMappingException;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.exc.MismatchedInputException;
13 import java.io.IOException;
14 import java.io.UnsupportedEncodingException;
15 import java.util.Arrays;
16 import java.util.List;
18 import java.util.UUID;
20 import javax.servlet.FilterChain;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
33 private static final Logger LOGGER = LoggerFactory.getLogger(
LoggingFilter.class);
36 private static final String REQUEST_PREFIX =
"Request: ";
39 private static final String RESPONSE_PREFIX =
"Response: ";
49 final HttpServletResponse response,
final FilterChain filterChain)
50 throws ServletException, IOException {
51 if(LOGGER.isDebugEnabled()) {
52 List<String> apis = Arrays.asList(
"stats/",
"switch/");
54 final long startTime = System.currentTimeMillis();
55 UUID requestId = UUID.randomUUID();
56 request.setAttribute(
"Id", requestId);
57 String fullRequestPath = request.getRequestURL().toString();
58 String[] contextVar = request.getRequestURL().toString().split(
"/");
59 String apiName = contextVar[1];
60 boolean isMatch =
false;
62 for (String api : apis) {
63 if (apiName.equalsIgnoreCase(api) || fullRequestPath.toLowerCase().contains(api)) {
73 filterChain.doFilter(request, responseWrapper);
77 logResponse(responseWrapper);
79 }
catch (Exception e) {
80 LOGGER.error(
"[doFilterInternal] Exception: " + e.getMessage(), e);
83 long elapsedTime = System.currentTimeMillis() - startTime;
84 if (60000 - elapsedTime < 0) {
85 LOGGER.debug(
"[DelayedRequestDetail] - Time Taken: '{}', URL: '{}'", elapsedTime,
86 request.getRequestURL());
89 filterChain.doFilter(request, response);
99 private void logRequest(
final HttpServletRequest request) {
100 StringBuilder msg =
new StringBuilder();
101 msg.append(REQUEST_PREFIX).append(
"\n\tid: '").append(request.getAttribute(
"Id"))
102 .append(
"', ").append(
"\n\tcontent type: '").append(request.getContentType())
103 .append(
"', ").append(
"\n\turl: '").append(request.getRequestURL());
104 if (request.getQueryString() != null) {
105 msg.append(
'?').append(request.getQueryString());
108 Map<String, String[]> parameters = request.getParameterMap();
110 parameters.keySet().forEach((key) -> {
111 msg.append(
"', \n\tparams: '").append(key +
" : " + parameters.get(key));
114 LOGGER.debug(
"[logRequest] Request: " + msg.toString());
126 private void logResponse(
final ResponseWrapper response)
throws JsonParseException,
127 JsonMappingException, IOException {
128 StringBuilder msg =
new StringBuilder();
129 msg.append(RESPONSE_PREFIX);
130 msg.append(
"\nid: '").append((response.getId())).append(
"' ");
131 String content = null;
134 ObjectMapper mapper =
new ObjectMapper();
135 content =
new String(response.getData(), response.getCharacterEncoding());
136 Object json = mapper.readValue(content, Object.class);
138 msg.append(
"\nResponse: \n").append(
139 mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
140 }
catch (UnsupportedEncodingException e) {
141 LOGGER.error(
"[logResponse] Exception: " + e.getMessage(), e);
142 }
catch (MismatchedInputException e) {
143 msg.append(
"\nResponse: \n").append(content);
145 LOGGER.debug(
"[logResponse] Response: " + msg.toString());
void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)