KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > filters > SetCharacterEncodingFilter


1 /*
2  * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java,v 1.2 2001/10/17 22:53:19 craigmcc Exp $
3  * $Revision: 1.2 $
4  * $Date: 2001/10/17 22:53:19 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  * [Additional notices, if required by prior licensing conditions]
61  *
62  */

63
64
65 package filters;
66
67
68 import java.io.IOException JavaDoc;
69 import javax.servlet.Filter JavaDoc;
70 import javax.servlet.FilterChain JavaDoc;
71 import javax.servlet.FilterConfig JavaDoc;
72 import javax.servlet.ServletException JavaDoc;
73 import javax.servlet.ServletRequest JavaDoc;
74 import javax.servlet.ServletResponse JavaDoc;
75 import javax.servlet.UnavailableException JavaDoc;
76
77
78 /**
79  * <p>Example filter that sets the character encoding to be used in parsing the
80  * incoming request, either unconditionally or only if the client did not
81  * specify a character encoding. Configuration of this filter is based on
82  * the following initialization parameters:</p>
83  * <ul>
84  * <li><strong>encoding</strong> - The character encoding to be configured
85  * for this request, either conditionally or unconditionally based on
86  * the <code>ignore</code> initialization parameter. This parameter
87  * is required, so there is no default.</li>
88  * <li><strong>ignore</strong> - If set to "true", any character encoding
89  * specified by the client is ignored, and the value returned by the
90  * <code>selectEncoding()</code> method is set. If set to "false,
91  * <code>selectEncoding()</code> is called <strong>only</strong> if the
92  * client has not already specified an encoding. By default, this
93  * parameter is set to "true".</li>
94  * </ul>
95  *
96  * <p>Although this filter can be used unchanged, it is also easy to
97  * subclass it and make the <code>selectEncoding()</code> method more
98  * intelligent about what encoding to choose, based on characteristics of
99  * the incoming request (such as the values of the <code>Accept-Language</code>
100  * and <code>User-Agent</code> headers, or a value stashed in the current
101  * user's session.</p>
102  *
103  * @author Craig McClanahan
104  * @version $Revision: 1.2 $ $Date: 2001/10/17 22:53:19 $
105  */

106
107 public class SetCharacterEncodingFilter implements Filter JavaDoc {
108
109
110     // ----------------------------------------------------- Instance Variables
111

112
113     /**
114      * The default character encoding to set for requests that pass through
115      * this filter.
116      */

117     protected String JavaDoc encoding = null;
118
119
120     /**
121      * The filter configuration object we are associated with. If this value
122      * is null, this filter instance is not currently configured.
123      */

124     protected FilterConfig JavaDoc filterConfig = null;
125
126
127     /**
128      * Should a character encoding specified by the client be ignored?
129      */

130     protected boolean ignore = true;
131
132
133     // --------------------------------------------------------- Public Methods
134

135
136     /**
137      * Take this filter out of service.
138      */

139     public void destroy() {
140
141         this.encoding = null;
142         this.filterConfig = null;
143
144     }
145
146
147     /**
148      * Select and set (if specified) the character encoding to be used to
149      * interpret request parameters for this request.
150      *
151      * @param request The servlet request we are processing
152      * @param result The servlet response we are creating
153      * @param chain The filter chain we are processing
154      *
155      * @exception IOException if an input/output error occurs
156      * @exception ServletException if a servlet error occurs
157      */

158     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
159                          FilterChain JavaDoc chain)
160     throws IOException JavaDoc, ServletException JavaDoc {
161
162         // Conditionally select and set the character encoding to be used
163
if (ignore || (request.getCharacterEncoding() == null)) {
164             String JavaDoc encoding = selectEncoding(request);
165             if (encoding != null)
166                 request.setCharacterEncoding(encoding);
167         }
168
169     // Pass control on to the next filter
170
chain.doFilter(request, response);
171
172     }
173
174
175     /**
176      * Place this filter into service.
177      *
178      * @param filterConfig The filter configuration object
179      */

180     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
181
182     this.filterConfig = filterConfig;
183         this.encoding = filterConfig.getInitParameter("encoding");
184         String JavaDoc value = filterConfig.getInitParameter("ignore");
185         if (value == null)
186             this.ignore = true;
187         else if (value.equalsIgnoreCase("true"))
188             this.ignore = true;
189         else if (value.equalsIgnoreCase("yes"))
190             this.ignore = true;
191         else
192             this.ignore = false;
193
194     }
195
196
197     // ------------------------------------------------------ Protected Methods
198

199
200     /**
201      * Select an appropriate character encoding to be used, based on the
202      * characteristics of the current request and/or filter initialization
203      * parameters. If no character encoding should be set, return
204      * <code>null</code>.
205      * <p>
206      * The default implementation unconditionally returns the value configured
207      * by the <strong>encoding</strong> initialization parameter for this
208      * filter.
209      *
210      * @param request The servlet request we are processing
211      */

212     protected String JavaDoc selectEncoding(ServletRequest JavaDoc request) {
213
214         return (this.encoding);
215
216     }
217
218
219 }
220
Popular Tags