|
@@ -7,7 +7,6 @@ import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
|
-import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ReadListener;
|
|
import javax.servlet.ReadListener;
|
|
@@ -17,27 +16,24 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletRequestWrapper;
|
|
import javax.servlet.http.HttpServletRequestWrapper;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
-import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
|
|
-import com.chelvc.framework.base.context.LoggingContextHolder;
|
|
|
|
|
-import com.chelvc.framework.base.context.Result;
|
|
|
|
|
-import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
|
import com.chelvc.framework.common.util.CodecUtils;
|
|
import com.chelvc.framework.common.util.CodecUtils;
|
|
|
import com.chelvc.framework.common.util.FileUtils;
|
|
import com.chelvc.framework.common.util.FileUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
|
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 请求调用拦截器
|
|
|
|
|
|
|
+ * 请求缓存拦截器
|
|
|
*
|
|
*
|
|
|
* @author Woody
|
|
* @author Woody
|
|
|
* @date 2024/4/3
|
|
* @date 2024/4/3
|
|
|
*/
|
|
*/
|
|
|
-@Slf4j
|
|
|
|
|
-public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
|
|
|
|
|
+@Order
|
|
|
|
|
+@Component
|
|
|
|
|
+public class RequestCachingInterceptor extends OncePerRequestFilter {
|
|
|
/**
|
|
/**
|
|
|
* 缓存数据流对象
|
|
* 缓存数据流对象
|
|
|
*/
|
|
*/
|
|
@@ -205,18 +201,10 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Enumeration<String> getParameterNames() {
|
|
public Enumeration<String> getParameterNames() {
|
|
|
- Iterator<String> iterator = this.parameters.keySet().iterator();
|
|
|
|
|
- return new Enumeration<String>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean hasMoreElements() {
|
|
|
|
|
- return iterator.hasNext();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public String nextElement() {
|
|
|
|
|
- return iterator.next();
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ if (ObjectUtils.isEmpty(this.parameters)) {
|
|
|
|
|
+ return Collections.emptyEnumeration();
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.enumeration(this.parameters.keySet());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -225,12 +213,12 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public BufferedReader getReader() throws IOException {
|
|
|
|
|
|
|
+ public BufferedReader getReader() {
|
|
|
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
|
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public ServletInputStream getInputStream() throws IOException {
|
|
|
|
|
|
|
+ public ServletInputStream getInputStream() {
|
|
|
return new CachingInputStream(this.bytes);
|
|
return new CachingInputStream(this.bytes);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -238,19 +226,11 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
|
@Override
|
|
@Override
|
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
|
throws ServletException, IOException {
|
|
throws ServletException, IOException {
|
|
|
- // 如果不是文件上传请求则对原生请求进行缓存包装处理
|
|
|
|
|
- if (!HttpUtils.isStreamBody(request)) {
|
|
|
|
|
- request = new CachingRequestWrapper(request);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 执行请求处理链
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ if (HttpUtils.isStreamBody(request)) {
|
|
|
chain.doFilter(request, response);
|
|
chain.doFilter(request, response);
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- LoggingContextHolder.warn(log, request, e);
|
|
|
|
|
- String message = ApplicationContextHolder.getMessage("Request.Invalid");
|
|
|
|
|
- Result<?> result = Result.of(HttpStatus.BAD_REQUEST.name(), null, message);
|
|
|
|
|
- SessionContextHolder.response(response, result);
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果不是文件上传请求则对原生请求进行缓存包装处理
|
|
|
|
|
+ chain.doFilter(new CachingRequestWrapper(request), response);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|