¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.interceptor; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import com.alibaba.ttl.TransmittableThreadLocal; |
| | | import com.ruoyi.common.filter.RepeatedlyRequestWrapper; |
| | | import com.ruoyi.common.utils.JsonUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.time.StopWatch; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.BufferedReader; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * webçè°ç¨æ¶é´ç»è®¡æ¦æªå¨ |
| | | * devç¯å¢ææ |
| | | * |
| | | * @author Lion Li |
| | | * @since 3.3.0 |
| | | */ |
| | | @Slf4j |
| | | public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor { |
| | | |
| | | private final TransmittableThreadLocal<StopWatch> invokeTimeTL = new TransmittableThreadLocal<>(); |
| | | |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
| | | if (!"prod".equals(SpringUtils.getActiveProfile())) { |
| | | String url = request.getMethod() + " " + request.getRequestURI(); |
| | | |
| | | // æå°è¯·æ±åæ° |
| | | if (isJsonRequest(request)) { |
| | | String jsonParam = ""; |
| | | if (request instanceof RepeatedlyRequestWrapper) { |
| | | BufferedReader reader = request.getReader(); |
| | | jsonParam = IoUtil.read(reader); |
| | | } |
| | | log.debug("[PLUS]å¼å§è¯·æ± => URL[{}],åæ°ç±»å[json],åæ°:[{}]", url, jsonParam); |
| | | } else { |
| | | Map<String, String[]> parameterMap = request.getParameterMap(); |
| | | if (MapUtil.isNotEmpty(parameterMap)) { |
| | | String parameters = JsonUtils.toJsonString(parameterMap); |
| | | log.debug("[PLUS]å¼å§è¯·æ± => URL[{}],åæ°ç±»å[param],åæ°:[{}]", url, parameters); |
| | | } else { |
| | | log.debug("[PLUS]å¼å§è¯·æ± => URL[{}],æ åæ°", url); |
| | | } |
| | | } |
| | | |
| | | StopWatch stopWatch = new StopWatch(); |
| | | invokeTimeTL.set(stopWatch); |
| | | stopWatch.start(); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
| | | if (!"prod".equals(SpringUtils.getActiveProfile())) { |
| | | StopWatch stopWatch = invokeTimeTL.get(); |
| | | stopWatch.stop(); |
| | | log.debug("[PLUS]ç»æè¯·æ± => URL[{}],èæ¶:[{}]毫ç§", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime()); |
| | | invokeTimeTL.remove(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤ææ¬æ¬¡è¯·æ±çæ°æ®ç±»åæ¯å¦ä¸ºjson |
| | | * |
| | | * @param request request |
| | | * @return boolean |
| | | */ |
| | | private boolean isJsonRequest(HttpServletRequest request) { |
| | | String contentType = request.getContentType(); |
| | | if (contentType != null) { |
| | | return StringUtils.startsWithIgnoreCase(contentType, MediaType.APPLICATION_JSON_VALUE); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | } |