1 /***
2 * @(#)EncodingFilter.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.util.filter;
26
27 import java.io.IOException;
28
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.HttpServletRequest;
35
36 /***
37 * <p>
38 * Filter that sets the character encoding to be used in parsing the incoming
39 * request, either unconditionally or only if the client did not specify a
40 * character encoding. Resource of this filter is based on the following
41 * initialization parameters:
42 * </p>
43 *
44 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
45 * @version $Revision: 1.6 $ $Date: 2005/05/22 06:51:24 $
46 * @version Revision: 1.0
47 */
48
49 public class EncodingFilter {
50
51 /***
52 * The filter configuration object we are associated with. If this value is
53 * null, this filter instance is not currently configured.
54 */
55 private FilterConfig config = null;
56
57 /***
58 * The default character encoding to set for requests that pass through this
59 * filter.
60 */
61 private String targetEncoding = "utf-8";
62
63 /***
64 * Place this filter into service.
65 *
66 * @param config The filter configuration object
67 * @throws javax.servlet.ServletException
68 */
69 public void init(FilterConfig config) throws ServletException {
70 this.config = config;
71 String encodingConfig = config.getInitParameter("encoding");
72 if (encodingConfig != null) {
73 this.targetEncoding = encodingConfig;
74 }
75 }
76
77 /***
78 * Take this filter out of service.
79 */
80 public void destroy() {
81 config = null;
82 targetEncoding = null;
83 }
84
85 /***
86 * Select and set (if specified) the character encoding to be used to
87 * interpret request parameters for this request.
88 *
89 * @param servletRequest
90 * @param servletResponse
91 * @param chain
92 * @throws IOException
93 * @throws ServletException
94 */
95 public void doFilter(ServletRequest servletRequest,
96 ServletResponse servletResponse, FilterChain chain)
97 throws IOException, ServletException {
98 HttpServletRequest request = (HttpServletRequest) servletRequest;
99 request.setCharacterEncoding(targetEncoding);
100 chain.doFilter(servletRequest, servletResponse);
101 }
102 }