1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 package org.dbforms.util.external;
87
88 import java.io.IOException;
89
90 import javax.servlet.Filter;
91 import javax.servlet.FilterChain;
92 import javax.servlet.FilterConfig;
93 import javax.servlet.ServletException;
94 import javax.servlet.ServletRequest;
95 import javax.servlet.ServletResponse;
96
97
98
99 /***
100 * <p>
101 * Example filter that sets the character encoding to be used in parsing the
102 * incoming request, either unconditionally or only if the client did not
103 * specify a character encoding. Configuration of this filter is based on the
104 * following initialization parameters:
105 * </p>
106 *
107 * <ul>
108 * <li>
109 * <strong>encoding</strong> - The character encoding to be configured for this
110 * request, either conditionally or unconditionally based on the
111 * <code>ignore</code> initialization parameter. This parameter is required,
112 * so there is no default.
113 * </li>
114 * <li>
115 * <strong>ignore</strong> - If set to "true", any character encoding specified
116 * by the client is ignored, and the value returned by the
117 * <code>selectEncoding()</code> method is set. If set to "false,
118 * <code>selectEncoding()</code> is called <strong>only</strong> if the client
119 * has not already specified an encoding. By default, this parameter is set
120 * to "true".
121 * </li>
122 * </ul>
123 *
124 * <p>
125 * Although this filter can be used unchanged, it is also easy to subclass it
126 * and make the <code>selectEncoding()</code> method more intelligent about
127 * what encoding to choose, based on characteristics of the incoming request
128 * (such as the values of the <code>Accept-Language</code> and
129 * <code>User-Agent</code> headers, or a value stashed in the current user's
130 * session.
131 * </p>
132 *
133 * @author Craig McClanahan
134 * @version $Revision: 1.4 $ $Date: 2005/02/19 21:26:32 $
135 */
136 public class SetCharacterEncodingFilter implements Filter {
137 /***
138 * The filter configuration object we are associated with. If this value is
139 * null, this filter instance is not currently configured.
140 */
141 protected FilterConfig filterConfig = null;
142
143
144
145 /***
146 * The default character encoding to set for requests that pass through this
147 * filter.
148 */
149 protected String encoding = null;
150
151 /*** Should a character encoding specified by the client be ignored? */
152 protected boolean ignore = true;
153
154
155
156 /***
157 * Take this filter out of service.
158 */
159 public void destroy() {
160 this.encoding = null;
161 this.filterConfig = null;
162 }
163
164
165 /***
166 * Select and set (if specified) the character encoding to be used to
167 * interpret request parameters for this request.
168 *
169 * @param request The servlet request we are processing
170 * @param response The servlet response we are creating
171 * @param chain The filter chain we are processing
172 *
173 * @exception IOException if an input/output error occurs
174 * @exception ServletException if a servlet error occurs
175 */
176 public void doFilter(ServletRequest request,
177 ServletResponse response,
178 FilterChain chain)
179 throws IOException, ServletException {
180
181 if (ignore || (request.getCharacterEncoding() == null)) {
182 String pencoding = selectEncoding(request);
183
184 if (pencoding != null) {
185 request.setCharacterEncoding(pencoding);
186 }
187 }
188
189
190 chain.doFilter(request, response);
191 }
192
193
194 /***
195 * Place this filter into service.
196 *
197 * @param filterConfig The filter configuration object
198 */
199 public void init(FilterConfig afilterConfig) throws ServletException {
200 this.filterConfig = afilterConfig;
201 this.encoding = filterConfig.getInitParameter("encoding");
202
203 String value = filterConfig.getInitParameter("ignore");
204
205 if (value == null) {
206 this.ignore = true;
207 } else if (value.equalsIgnoreCase("true")) {
208 this.ignore = true;
209 } else if (value.equalsIgnoreCase("yes")) {
210 this.ignore = true;
211 } else {
212 this.ignore = false;
213 }
214 }
215
216
217
218
219 /***
220 * Select an appropriate character encoding to be used, based on the
221 * characteristics of the current request and/or filter initialization
222 * parameters. If no character encoding should be set, return
223 * <code>null</code>.
224 *
225 * <p>
226 * The default implementation unconditionally returns the value configured
227 * by the <strong>encoding</strong> initialization parameter for this
228 * filter.
229 * </p>
230 *
231 * @param request The servlet request we are processing
232 *
233 * @return DOCUMENT ME!
234 */
235 protected String selectEncoding(ServletRequest request) {
236 return (this.encoding);
237 }
238 }