1 package gboat2.base.plugin.servlet.filter;
2
3 import static gboat2.base.bridge.GboatAppConstants.ENCODING_ISO88591;
4 import static gboat2.base.bridge.GboatAppConstants.ENCODING_UTF8;
5
6 import java.io.IOException;
7 import java.net.URLDecoder;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Iterator;
11
12 import javax.servlet.FilterChain;
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.springframework.web.filter.OncePerRequestFilter;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class GboatEncodingFilter extends OncePerRequestFilter {
40
41 private Logger logger = LoggerFactory.getLogger(GboatEncodingFilter.class.getName());
42
43 @Override
44 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
45 throws ServletException, IOException {
46 HttpServletRequest httpServletRequest = (HttpServletRequest) request;
47 request.setCharacterEncoding(ENCODING_UTF8);
48
49 ArrayList<String> queryValues = new ArrayList<String>();
50 String query = httpServletRequest.getQueryString();
51 if (query != null) {
52 String[] expressions = query.split("&");
53 for (String expression : expressions) {
54 String[] parts = expression.split("=");
55 if (parts.length == 2) {
56 String str = URLDecoder.decode(parts[1], ENCODING_UTF8);
57 queryValues.add(new String(str.getBytes(ENCODING_UTF8), ENCODING_ISO88591));
58 logger.debug(" request get parameter query key : {},value : {}", parts[0], str);
59 }
60 }
61 }
62
63 Collection<?> values = request.getParameterMap().values();
64 for (Iterator<?> iterator = values.iterator(); iterator.hasNext();) {
65 String[] strs = (String[]) iterator.next();
66 for (int i = 0; i < strs.length; i++) {
67 for (String qStr : queryValues) {
68 if (qStr.equals(strs[i])) {
69 strs[i] = new String(qStr.getBytes(ENCODING_ISO88591), ENCODING_UTF8);
70 break;
71 }
72 }
73 }
74 }
75
76 filterChain.doFilter(request, response);
77 }
78 }