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.Filter;
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletRequest;
34 import javax.servlet.ServletResponse;
35 import javax.servlet.http.HttpServletRequest;
36
37 /***
38 * <p>
39 * Filter that sets the character encoding to be used in parsing the incoming
40 * request, either unconditionally or only if the client did not specify a
41 * character encoding. Resource of this filter is based on the following
42 * initialization parameters:
43 * </p>
44 *
45 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
46 * @version $Revision: 1.6 $ $Date: 2005/05/22 06:51:24 $
47 * @version Revision: 1.0
48 */
49
50 public class EncodingFilter implements Filter {
51
52 /***
53 * The filter configuration object we are associated with. If this value is
54 * null, this filter instance is not currently configured.
55 */
56 private FilterConfig config = null;
57
58 /***
59 * The default character encoding to set for requests that pass through this
60 * filter.
61 */
62 private String targetEncoding = "utf-8";
63
64 /***
65 * Place this filter into service.
66 *
67 * @param config The filter configuration object
68 * @throws javax.servlet.ServletException
69 */
70 public void init(FilterConfig config) throws ServletException {
71 this.config = config;
72 String encodingConfig = config.getInitParameter("encoding");
73 if (encodingConfig != null) {
74 this.targetEncoding = encodingConfig;
75 }
76 }
77
78 /***
79 * Take this filter out of service.
80 */
81 public void destroy() {
82 config = null;
83 targetEncoding = null;
84 }
85
86 /***
87 * Select and set (if specified) the character encoding to be used to
88 * interpret request parameters for this request.
89 *
90 * @param servletRequest
91 * @param servletResponse
92 * @param chain
93 * @throws IOException
94 * @throws ServletException
95 */
96 public void doFilter(ServletRequest servletRequest,
97 ServletResponse servletResponse, FilterChain chain)
98 throws IOException, ServletException {
99 HttpServletRequest request = (HttpServletRequest) servletRequest;
100 request.setCharacterEncoding(targetEncoding);
101 chain.doFilter(servletRequest, servletResponse);
102 }
103 }