01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package dlog4j;
17:
18: import java.io.IOException;
19:
20: import javax.servlet.Filter;
21: import javax.servlet.FilterChain;
22: import javax.servlet.FilterConfig;
23: import javax.servlet.ServletException;
24: import javax.servlet.ServletRequest;
25: import javax.servlet.ServletResponse;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28:
29: import dlog4j.proxy.RequestProxy;
30: import dlog4j.proxy.ResponseProxy;
31: import dlog4j.util.StringUtils;
32:
33: /**
34: * UNICODE¹ýÂËÆ÷
35: * @author Winter Lau
36: */
37: public class UnicodeFilter implements Filter {
38:
39: String encoding;
40:
41: /* (non-Javadoc)
42: * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
43: */
44: public void init(FilterConfig config) throws ServletException {
45: encoding = config.getInitParameter("encoding");
46: if (StringUtils.isEmpty(encoding))
47: encoding = "UTF-8";
48: config.getServletContext().setAttribute("encoding", encoding);
49: }
50:
51: /* (non-Javadoc)
52: * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
53: * javax.servlet.ServletResponse,
54: * javax.servlet.FilterChain)
55: */
56: public void doFilter(ServletRequest req, ServletResponse res,
57: FilterChain chain) throws IOException, ServletException {
58: HttpServletRequest h_req = (HttpServletRequest) req;
59: HttpServletResponse response = (HttpServletResponse) res;
60: String accept = h_req.getHeader("accept");
61: if (accept == null || accept.indexOf("text/vnd.wap.wml") == -1)
62: res.setContentType("text/html;charset=UTF-8");
63: else {
64: response.setContentType("text/vnd.wap.wml;charset=UTF-8");
65: response.setHeader("Pragma", "No-cache");
66: response.setHeader("Cache-Control", "no-cache");
67: response.setDateHeader("Expires", 0);
68: }
69: HttpServletRequest p_req = RequestProxy.getProxy(req, encoding)
70: .getInstance();
71: HttpServletResponse p_res = ResponseProxy.getProxy(response)
72: .getInstance();
73: chain.doFilter(p_req, p_res);
74: }
75:
76: /* (non-Javadoc)
77: * @see javax.servlet.Filter#destroy()
78: */
79: public void destroy() {
80: }
81:
82: }
|