1 package cn.home1.oss.lib.common;
2
3 import static org.apache.commons.lang3.StringUtils.isBlank;
4
5 import java.nio.charset.Charset;
6 import java.nio.charset.StandardCharsets;
7
8 import javax.servlet.ServletRequest;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletRequestWrapper;
11
12
13
14
15 public abstract class RequestUtlis {
16
17 private RequestUtlis() {
18 }
19
20 public static Charset findCharset(final HttpServletRequest request) {
21 final String encoding = request.getCharacterEncoding();
22 final Charset result;
23 if (isBlank(encoding)) {
24 result = StandardCharsets.UTF_8;
25 } else {
26 result = Charset.forName(encoding);
27 }
28 return result;
29 }
30
31 @SuppressWarnings("unchecked")
32 public static <T extends HttpServletRequestWrapper> T findWrapper(
33 final ServletRequest request, final Class<T> type
34 ) {
35 final T result;
36 if (request != null) {
37 if (type.isAssignableFrom(request.getClass())) {
38 result = (T) request;
39 } else {
40 if (HttpServletRequestWrapper.class.isAssignableFrom(request.getClass())) {
41 return findWrapper(((HttpServletRequestWrapper) request).getRequest(), type);
42 } else {
43 result = null;
44 }
45 }
46 } else {
47 result = null;
48 }
49 return result;
50 }
51 }