KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > UnicodeFilter


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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 JavaDoc;
19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc {
38
39     String JavaDoc encoding;
40     
41     /* (non-Javadoc)
42      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
43      */

44     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
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 JavaDoc req, ServletResponse JavaDoc res,FilterChain JavaDoc chain)
57         throws IOException JavaDoc, ServletException JavaDoc
58     {
59         HttpServletRequest JavaDoc h_req = (HttpServletRequest JavaDoc)req;
60         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)res;
61         String JavaDoc accept = h_req.getHeader("accept");
62         if(accept==null || accept.indexOf("text/vnd.wap.wml")==-1)
63             res.setContentType("text/html;charset=UTF-8");
64         else{
65             response.setContentType("text/vnd.wap.wml;charset=UTF-8");
66             response.setHeader("Pragma","No-cache");
67             response.setHeader("Cache-Control","no-cache");
68             response.setDateHeader("Expires", 0);
69         }
70         HttpServletRequest JavaDoc p_req = RequestProxy.getProxy(req, encoding).getInstance();
71         HttpServletResponse JavaDoc p_res = ResponseProxy.getProxy(response).getInstance();
72         chain.doFilter(p_req, p_res);
73     }
74
75     /* (non-Javadoc)
76      * @see javax.servlet.Filter#destroy()
77      */

78     public void destroy() {
79     }
80
81 }
82
Popular Tags