1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.seasar.tuigwaa.util.filter;
23
24 import java.io.IOException;
25 import java.util.regex.Pattern;
26
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletRequest;
34
35 /***
36 * @author nishioka
37 */
38 public class EncodingFilter implements Filter {
39
40
41
42 private String defaultPostEncoding;
43
44 private String defaultGetEncoding;
45
46 private String extraGetEncoding;
47
48 private Pattern extraGetPathPattern;
49
50 public void init(FilterConfig config) throws ServletException {
51 this.defaultPostEncoding = config
52 .getInitParameter("defaultPostEncoding");
53 this.defaultGetEncoding = config.getInitParameter("defaultGetEncoding");
54
55 String extraPath = config.getInitParameter("extraGetPath");
56 this.extraGetEncoding = config.getInitParameter("extraGetEncoding");
57 this.extraGetPathPattern = Pattern.compile(extraPath);
58 }
59
60 public void doFilter(ServletRequest request, ServletResponse response,
61 FilterChain chain) throws IOException, ServletException {
62
63 HttpServletRequest hRequest = (HttpServletRequest) request;
64 String servletPath = hRequest.getServletPath();
65
66 String encode = defaultGetEncoding;
67 if (extraGetPathPattern.matcher(servletPath).find()) {
68 encode = extraGetEncoding;
69 }
70 EncodingRequest eRequest = new EncodingRequest(hRequest, encode);
71
72 eRequest.setCharacterEncoding(this.defaultPostEncoding);
73
74
75
76
77
78
79
80 chain.doFilter(eRequest, response);
81 }
82
83 public void destroy() {
84 }
85 }