01: /* CharsetFilter.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Mon Apr 18 16:40:37 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.web.servlet;
20:
21: import java.io.IOException;
22:
23: import javax.servlet.ServletRequest;
24: import javax.servlet.ServletResponse;
25: import javax.servlet.FilterChain;
26: import javax.servlet.Filter;
27: import javax.servlet.FilterConfig;
28: import javax.servlet.ServletException;
29:
30: import org.zkoss.util.logging.Log;
31:
32: /**
33: * The filter to correct character encoding and to prepare Locale for
34: * the current request.
35: *
36: * <p>Initial parameters:
37: * <dl>
38: * <dt>charset</dt>
39: * <dd>The default character set if it is not specified in the DSP page.
40: * Default: UTF-8.</dd>
41: * </dl>
42: *
43: * <p>It is actually based on {@link Charsets#setup}.
44: *
45: * @author tomyeh
46: */
47: public class CharsetFilter implements Filter {
48: //private static final Log log = Log.lookup(CharsetFilter.class);
49: private String _charset = "UTF-8";
50:
51: //-- Filter --//
52: public void doFilter(ServletRequest request,
53: ServletResponse response, FilterChain chain)
54: throws IOException, ServletException {
55: final Object old = Charsets.setup(request, response, _charset);
56: try {
57: chain.doFilter(request, response);
58: } finally {
59: Charsets.cleanup(request, old);
60: }
61: }
62:
63: public void destroy() {
64: }
65:
66: public final void init(FilterConfig config) throws ServletException {
67: final String cs = config.getInitParameter("charset");
68: if (cs != null)
69: _charset = cs.length() > 0 ? cs : null;
70: }
71: }
|