KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filter > CharacterEncodingFilter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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

41
42 public class CharacterEncodingFilter implements Filter JavaDoc {
43
44     /**
45      * The default character encoding to set for requests that pass through
46      * this filter.
47      */

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

54     protected FilterConfig JavaDoc filterConfig = null;
55
56     /**
57      * Should a character encoding specified by the client be ignored?
58      */

59     protected boolean ignore = true;
60
61
62     /**
63      * Take this filter out of service.
64      */

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

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

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

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