KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > CharacterEncodingFilter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.servlet;
11
12 import java.io.IOException JavaDoc;
13
14 import javax.servlet.Filter JavaDoc;
15 import javax.servlet.FilterChain JavaDoc;
16 import javax.servlet.FilterConfig JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.ServletRequest JavaDoc;
19 import javax.servlet.ServletResponse JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.mmbase.module.Module;
23 import org.mmbase.module.core.MMBase;
24 import org.mmbase.util.logging.Logger;
25 import org.mmbase.util.logging.Logging;
26
27 /**
28  * There's large simularity in the way data travels from and to a web server.
29  * But unfortunately there is one difference: while the web server tells the browser what
30  * character encoding the page it sends is in (via the Content-Type HTTP header),
31  * the client does not send such information.
32  *
33  * Accordingly to the HTTP spec the HTTP request the browser send to the server
34  * (that contains the submitted form) may well contain the Content-Type header too.
35  * This would give the server the key to decript the form parameters.
36  * Regretfully our present internet browsers do not send it
37  *
38  * The browser generally does the following: it takes user input in national characters
39  * <ul>
40  * <li>
41  * translates it to a byte sequence using the character encoding that the web page that contains
42  * the form is encoded with
43  * </li>
44  * <li>
45  * the resulting byte secuence is encoded into the query string according to the usual rules of
46  * encoding query strings. That is all bytes that correspond to legal ascii alpha-numeric chars
47  * are encoded as those chars, all the rest are converted to the %xy representation, where xy
48  * is the hexademical code of the corresponding byte (like %C1, for example)
49  * </li>
50  * </ul>
51  * Then the encoded query (possibly containing %xy codes) is sent to the server. ascii characters,
52  * according to the procedure described above are sent to the server as they are, provided that they
53  * have the same codes both in ascii character encoding and in the national character encoding that is used.
54  *
55  *
56  * This filter sets the character encoding before parameters are handled.
57  * The filter sets the character encoding by the following information:
58  * <ul>
59  * <li>HTTP content-type header</li>
60  * <li>Parameter of filter in the WEB-INF/web.xml</li>
61  * <li>MMBase encoding set in mmbase-config/modules/mmbaseroot.xml</li>
62  * <li>No encoding defined. (default UTF-8)</li>
63  * </ul>
64  *
65  * Get it to work by incorporating the following piece of XML in your web.xml:
66  <pre><code>
67  * &lt;filter&gt;
68  * &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;
69  * &lt;filter-class&gt;org.mmbase.servlet.CharacterEncodingFilter&lt;/filter-class&gt;
70  * &lt;!-- Overrides config/module/mmbaseroot.xml#encoding --&gt;
71  * &lt;!-- &lt;init-param&gt;
72  * &lt;param-name&gt;encoding&lt;/param-name&gt;
73  * &lt;param-value&gt;UTF-8&lt;/param-value&gt;
74  * &lt;/init-param&gt;
75  * --&gt;
76  * &lt;/filter&gt;
77  *
78  * &lt;filter-mapping&gt;
79  * &lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;
80  * &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
81  * &lt;/filter-mapping&gt;
82  </code></pre>
83  *
84  *
85  * @author P.S.D.Reitsma (Finalist IT Group)
86  * @author Nico Klasens (Finalist IT Group)
87  *
88  * @since MMBase-1.6
89  * @version $Id: CharacterEncodingFilter.java,v 1.3 2003/09/01 13:29:45 pierre Exp $
90  */

91 public class CharacterEncodingFilter implements Filter JavaDoc {
92
93     /** MMBase logging system */
94     private static Logger log =
95         Logging.getLoggerInstance(CharacterEncodingFilter.class.getName());
96
97     private String JavaDoc encoding = null;
98     private FilterConfig JavaDoc filterConfig = null;
99
100     /**
101      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
102      */

103     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
104         log.info("CharacterEncodingFilter init");
105         filterConfig = config;
106         encoding = config.getInitParameter("encoding");
107         if (encoding == null) {
108            MMBase mmbase = (MMBase) Module.getModule("MMBASEROOT");
109            encoding = mmbase.getEncoding();
110            if (encoding == null) {
111               encoding = "UTF-8";
112            }
113         }
114     }
115
116     /**
117      * @see javax.servlet.Filter#destroy()
118      */

119     public void destroy() {
120         log.info("CharacterEncodingFilter destroy");
121         filterConfig = null;
122     }
123
124     /**
125      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
126      */

127     public void doFilter(
128         ServletRequest JavaDoc request,
129         ServletResponse JavaDoc response,
130         FilterChain JavaDoc chain)
131         throws IOException JavaDoc, ServletException JavaDoc {
132
133         if (request instanceof HttpServletRequest JavaDoc) {
134             HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
135             try {
136                 if (req.getCharacterEncoding() == null) {
137                     // Parameters are usually in the encoding of the jsp with the form
138
// jsp pageEncoding directive (ContentType charset)
139
req.setCharacterEncoding(encoding);
140                }
141             }
142             catch (Exception JavaDoc e) {
143                 log.warn("Error setting encoding : " + e.getMessage());
144             }
145         }
146
147         // Perform any other filters that are chained after this one.
148
// This includes calling the requested servlet!
149
chain.doFilter(request, response);
150     }
151 }
152
Popular Tags