|
@@ -6,6 +6,7 @@ import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
|
+import java.util.Date;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
@@ -18,6 +19,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
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.DateUtils;
|
|
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;
|
|
@@ -146,6 +148,7 @@ public class RequestCachingInterceptor extends OncePerRequestFilter {
|
|
public static class CachingRequestWrapper extends HttpServletRequestWrapper {
|
|
public static class CachingRequestWrapper extends HttpServletRequestWrapper {
|
|
private String body;
|
|
private String body;
|
|
private final byte[] bytes;
|
|
private final byte[] bytes;
|
|
|
|
+ private final Map<String, String[]> headers;
|
|
private final Map<String, String[]> parameters;
|
|
private final Map<String, String[]> parameters;
|
|
|
|
|
|
private CachingRequestWrapper(HttpServletRequest request) throws IOException {
|
|
private CachingRequestWrapper(HttpServletRequest request) throws IOException {
|
|
@@ -153,11 +156,15 @@ public class RequestCachingInterceptor extends OncePerRequestFilter {
|
|
if (request instanceof CachingRequestWrapper) {
|
|
if (request instanceof CachingRequestWrapper) {
|
|
this.body = ((CachingRequestWrapper) request).body;
|
|
this.body = ((CachingRequestWrapper) request).body;
|
|
this.bytes = ((CachingRequestWrapper) request).bytes;
|
|
this.bytes = ((CachingRequestWrapper) request).bytes;
|
|
|
|
+ this.headers = ((CachingRequestWrapper) request).headers;
|
|
this.parameters = ((CachingRequestWrapper) request).parameters;
|
|
this.parameters = ((CachingRequestWrapper) request).parameters;
|
|
} else {
|
|
} else {
|
|
|
|
+ // 缓存请求头
|
|
|
|
+ this.headers = HttpUtils.getHeaders(request);
|
|
|
|
+
|
|
// Servlet 规范规定如果调用了getInputStream()或getReader()方法,则getParameter*()方法的行为是未定义的
|
|
// Servlet 规范规定如果调用了getInputStream()或getReader()方法,则getParameter*()方法的行为是未定义的
|
|
// Tomcat、Jetty、Undertow容器均支持在调用getParameter*()方法后能正常调用getInputStream()方法
|
|
// Tomcat、Jetty、Undertow容器均支持在调用getParameter*()方法后能正常调用getInputStream()方法
|
|
- this.parameters = request.getParameterMap();
|
|
|
|
|
|
+ this.parameters = HttpUtils.getParameters(request);
|
|
if (HttpUtils.isBodyType(request)) {
|
|
if (HttpUtils.isBodyType(request)) {
|
|
try (InputStream input = request.getInputStream()) {
|
|
try (InputStream input = request.getInputStream()) {
|
|
if (input instanceof CachingInputStream) {
|
|
if (input instanceof CachingInputStream) {
|
|
@@ -188,6 +195,47 @@ public class RequestCachingInterceptor extends OncePerRequestFilter {
|
|
return this.body;
|
|
return this.body;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取请求头
|
|
|
|
+ *
|
|
|
|
+ * @return 请求头键/值映射表
|
|
|
|
+ */
|
|
|
|
+ public Map<String, String[]> getHeaders() {
|
|
|
|
+ return this.headers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getHeader(String name) {
|
|
|
|
+ String[] values = this.headers.get(name);
|
|
|
|
+ return ObjectUtils.isEmpty(values) ? null : values[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int getIntHeader(String name) {
|
|
|
|
+ String value = this.getHeader(name);
|
|
|
|
+ return StringUtils.isEmpty(value) ? -1 : Integer.parseInt(value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public long getDateHeader(String name) {
|
|
|
|
+ String value = this.getHeader(name);
|
|
|
|
+ Date date = DateUtils.parse(value);
|
|
|
|
+ return date == null ? -1 : date.getTime();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Enumeration<String> getHeaders(String name) {
|
|
|
|
+ return ObjectUtils.enumeration(this.headers.get(name));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Enumeration<String> getHeaderNames() {
|
|
|
|
+ if (ObjectUtils.isEmpty(this.headers)) {
|
|
|
|
+ return Collections.emptyEnumeration();
|
|
|
|
+ }
|
|
|
|
+ return Collections.enumeration(this.headers.keySet());
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public String getParameter(String name) {
|
|
public String getParameter(String name) {
|
|
String[] values = this.parameters.get(name);
|
|
String[] values = this.parameters.get(name);
|
|
@@ -196,7 +244,7 @@ public class RequestCachingInterceptor extends OncePerRequestFilter {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Map<String, String[]> getParameterMap() {
|
|
public Map<String, String[]> getParameterMap() {
|
|
- return Collections.unmodifiableMap(this.parameters);
|
|
|
|
|
|
+ return this.parameters;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|