KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > servlet > filter > SetCharacterEncodingFilter


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/servlet/filter/SetCharacterEncodingFilter.java,v 1.2 2004/06/27 01:20:34 skoehler Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/06/27 01:20:34 $
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 net.myvietnam.mvncore.servlet.filter;
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
76
77 /**
78  * <p>Example filter that sets the character encoding to be used in parsing the
79  * incoming request, either unconditionally or only if the client did not
80  * specify a character encoding. Configuration of this filter is based on
81  * the following initialization parameters:</p>
82  * <ul>
83  * <li><strong>encoding</strong> - The character encoding to be configured
84  * for this request, either conditionally or unconditionally based on
85  * the <code>ignore</code> initialization parameter. This parameter
86  * is required, so there is no default.</li>
87  * <li><strong>ignore</strong> - If set to "true", any character encoding
88  * specified by the client is ignored, and the value returned by the
89  * <code>selectEncoding()</code> method is set. If set to "false,
90  * <code>selectEncoding()</code> is called <strong>only</strong> if the
91  * client has not already specified an encoding. By default, this
92  * parameter is set to "true".</li>
93  * </ul>
94  *
95  * <p>Although this filter can be used unchanged, it is also easy to
96  * subclass it and make the <code>selectEncoding()</code> method more
97  * intelligent about what encoding to choose, based on characteristics of
98  * the incoming request (such as the values of the <code>Accept-Language</code>
99  * and <code>User-Agent</code> headers, or a value stashed in the current
100  * user's session.</p>
101  *
102  * @author Craig McClanahan
103  * @version $Revision: 1.2 $ $Date: 2004/06/27 01:20:34 $
104  */

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

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

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

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

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

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

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

157     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
158                          FilterChain JavaDoc chain)
159     throws IOException JavaDoc, ServletException JavaDoc {
160
161         // Conditionally select and set the character encoding to be used
162
if (ignore || (request.getCharacterEncoding() == null)) {
163             String JavaDoc encoding = selectEncoding(request);
164             if (encoding != null) {
165                 request.setCharacterEncoding(encoding);
166             }
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
186         // minhnn begin
187
if (this.encoding != null && this.encoding.equals("")) {
188             // do not allow empty encoding
189
this.encoding = null;
190         }
191         // minhnn end
192

193         if (value == null)
194             this.ignore = true;
195
196         // minhnn begin
197
else if (value.equals(""))
198             this.ignore = true;
199         // minhnn end
200

201         else if (value.equalsIgnoreCase("true"))
202             this.ignore = true;
203         else if (value.equalsIgnoreCase("yes"))
204             this.ignore = true;
205         else
206             this.ignore = false;
207
208     }
209
210
211     // ------------------------------------------------------ Protected Methods
212

213
214     /**
215      * Select an appropriate character encoding to be used, based on the
216      * characteristics of the current request and/or filter initialization
217      * parameters. If no character encoding should be set, return
218      * <code>null</code>.
219      * <p>
220      * The default implementation unconditionally returns the value configured
221      * by the <strong>encoding</strong> initialization parameter for this
222      * filter.
223      *
224      * @param request The servlet request we are processing
225      */

226     protected String JavaDoc selectEncoding(ServletRequest JavaDoc request) {
227
228         return (this.encoding);
229
230     }
231
232
233 }
234
Popular Tags