KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > SetCharacterEncodingFilter


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

35
36 public class SetCharacterEncodingFilter
37     implements Filter {
38
39 // ----------------------------------------------------- Instance Variables
40

41   /**
42    * The default character encoding to set for requests that pass through
43    * this filter.
44    */

45   protected String JavaDoc 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 // --------------------------------------------------------- Public Methods
59

60   /**
61    * Take this filter out of service.
62    */

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

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

101   public void init(FilterConfig filterConfig) throws ServletException {
102
103     this.filterConfig = filterConfig;
104     this.encoding = filterConfig.getInitParameter("encoding");
105     PropsUtil.ENCODING = this.encoding;
106     String JavaDoc value = filterConfig.getInitParameter("ignore");
107     if (value == null)
108       this.ignore = true;
109     else if (value.equalsIgnoreCase("true"))
110       this.ignore = true;
111     else if (value.equalsIgnoreCase("yes"))
112       this.ignore = true;
113     else
114       this.ignore = false;
115
116   }
117
118 // ------------------------------------------------------ Protected Methods
119

120   /**
121    * Select an appropriate character encoding to be used, based on the
122    * characteristics of the current request and/or filter initialization
123    * parameters. If no character encoding should be set, return
124    * <code>null</code>.
125    * <p>
126    * The default implementation unconditionally returns the value configured
127    * by the <strong>encoding</strong> initialization parameter for this
128    * filter.
129    *
130    * @param request The servlet request we are processing
131    */

132   protected String JavaDoc selectEncoding(ServletRequest request) {
133
134     return (this.encoding);
135
136   }
137
138 }
139
Popular Tags