KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filters > SetCharacterEncoding


1 package jodd.servlet.filters;
2
3 import java.io.IOException;
4
5 import javax.servlet.Filter;
6 import javax.servlet.FilterChain;
7 import javax.servlet.FilterConfig;
8 import javax.servlet.ServletException;
9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11
12
13 /**
14  * <p>Example filter that sets the character encoding to be used in parsing the
15  * incoming request, either unconditionally or only if the client did not
16  * specify a character encoding. Configuration of this filter is based on
17  * the following initialization parameters:</p>
18  * <ul>
19  * <li><strong>encoding</strong> - The character encoding to be configured
20  * for this request, either conditionally or unconditionally based on
21  * the <code>ignore</code> initialization parameter. This parameter
22  * is required, so there is no default.</li>
23  * <li><strong>ignore</strong> - If set to "true", any character encoding
24  * specified by the client is ignored, and the value returned by the
25  * <code>selectEncoding()</code> method is set. If set to "false,
26  * <code>selectEncoding()</code> is called <strong>only</strong> if the
27  * client has not already specified an encoding. By default, this
28  * parameter is set to "true".</li>
29  * </ul>
30  *
31  * <p>Although this filter can be used unchanged, it is also easy to
32  * subclass it and make the <code>selectEncoding()</code> method more
33  * intelligent about what encoding to choose, based on characteristics of
34  * the incoming request (such as the values of the <code>Accept-Language</code>
35  * and <code>User-Agent</code> headers, or a value stashed in the current
36  * user's session.</p>
37  */

38
39 public class SetCharacterEncoding implements Filter {
40
41     /**
42      * The default character encoding to set for requests that pass through
43      * this filter.
44      */

45     protected String encoding = null;
46
47     /**
48      * The filter configuration object we are associated with. If this value
49      * is null, this filter instance is not currently configured.
50      */

51     protected FilterConfig filterConfig = null;
52
53     /**
54      * Should a character encoding specified by the client be ignored?
55      */

56     protected boolean ignore = true;
57
58
59     /**
60      * Take this filter out of service.
61      */

62     public void destroy() {
63         this.encoding = null;
64         this.filterConfig = null;
65     }
66
67
68     /**
69      * Select and set (if specified) the character encoding to be used to
70      * interpret request parameters for this request.
71      *
72      * @param request servlet request we are processing
73      * @param response servlet response we are creating
74      * @param chain filter chain we are processing
75      *
76      * @exception IOException
77      * if an input/output error occurs
78      * @exception ServletException
79      * if a servlet error occurs
80      */

81     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
82
83         // Conditionally select and set the character encoding to be used
84
if (ignore || (request.getCharacterEncoding() == null)) {
85             String encoding = selectEncoding(request);
86             if (encoding != null) {
87                 request.setCharacterEncoding(encoding);
88             }
89         }
90         chain.doFilter(request, response);
91     }
92
93     /**
94      * Place this filter into service.
95      *
96      * @param filterConfig The filter configuration object
97      */

98     public void init(FilterConfig filterConfig) throws ServletException {
99
100         this.filterConfig = filterConfig;
101         this.encoding = filterConfig.getInitParameter("encoding");
102         String value = filterConfig.getInitParameter("ignore");
103         if (value == null) {
104             this.ignore = true;
105         } else if (value.equalsIgnoreCase("true")) {
106             this.ignore = true;
107         } else {
108             this.ignore = false;
109         }
110     }
111
112     /**
113      * Select an appropriate character encoding to be used, based on the
114      * characteristics of the current request and/or filter initialization
115      * parameters. If no character encoding should be set, return
116      * <code>null</code>.
117      * <p>
118      * The default implementation unconditionally returns the value configured
119      * by the <strong>encoding</strong> initialization parameter for this
120      * filter.
121      *
122      * @param request The servlet request we are processing
123      */

124     protected String selectEncoding(ServletRequest request) {
125         return(this.encoding);
126     }
127 }
128
129
Popular Tags