KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > external > SetCharacterEncodingFilter


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/external/SetCharacterEncodingFilter.java,v 1.4 2005/02/19 21:26:32 hkollmann Exp $
3  * $Revision: 1.4 $
4  * $Date: 2005/02/19 21:26:32 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

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

86 package org.dbforms.util.external;
87
88 import java.io.IOException JavaDoc;
89
90 import javax.servlet.Filter JavaDoc;
91 import javax.servlet.FilterChain JavaDoc;
92 import javax.servlet.FilterConfig JavaDoc;
93 import javax.servlet.ServletException JavaDoc;
94 import javax.servlet.ServletRequest JavaDoc;
95 import javax.servlet.ServletResponse JavaDoc;
96
97
98
99 /**
100  * <p>
101  * Example filter that sets the character encoding to be used in parsing the
102  * incoming request, either unconditionally or only if the client did not
103  * specify a character encoding. Configuration of this filter is based on the
104  * following initialization parameters:
105  * </p>
106  *
107  * <ul>
108  * <li>
109  * <strong>encoding</strong> - The character encoding to be configured for this
110  * request, either conditionally or unconditionally based on the
111  * <code>ignore</code> initialization parameter. This parameter is required,
112  * so there is no default.
113  * </li>
114  * <li>
115  * <strong>ignore</strong> - If set to "true", any character encoding specified
116  * by the client is ignored, and the value returned by the
117  * <code>selectEncoding()</code> method is set. If set to "false,
118  * <code>selectEncoding()</code> is called <strong>only</strong> if the client
119  * has not already specified an encoding. By default, this parameter is set
120  * to "true".
121  * </li>
122  * </ul>
123  *
124  * <p>
125  * Although this filter can be used unchanged, it is also easy to subclass it
126  * and make the <code>selectEncoding()</code> method more intelligent about
127  * what encoding to choose, based on characteristics of the incoming request
128  * (such as the values of the <code>Accept-Language</code> and
129  * <code>User-Agent</code> headers, or a value stashed in the current user's
130  * session.
131  * </p>
132  *
133  * @author Craig McClanahan
134  * @version $Revision: 1.4 $ $Date: 2005/02/19 21:26:32 $
135  */

136 public class SetCharacterEncodingFilter implements Filter JavaDoc {
137    /**
138     * The filter configuration object we are associated with. If this value is
139     * null, this filter instance is not currently configured.
140     */

141    protected FilterConfig JavaDoc filterConfig = null;
142
143    // ----------------------------------------------------- Instance Variables
144

145    /**
146     * The default character encoding to set for requests that pass through this
147     * filter.
148     */

149    protected String JavaDoc encoding = null;
150
151    /** Should a character encoding specified by the client be ignored? */
152    protected boolean ignore = true;
153
154    // --------------------------------------------------------- Public Methods
155

156    /**
157     * Take this filter out of service.
158     */

159    public void destroy() {
160       this.encoding = null;
161       this.filterConfig = null;
162    }
163
164
165    /**
166     * Select and set (if specified) the character encoding to be used to
167     * interpret request parameters for this request.
168     *
169     * @param request The servlet request we are processing
170     * @param response The servlet response we are creating
171     * @param chain The filter chain we are processing
172     *
173     * @exception IOException if an input/output error occurs
174     * @exception ServletException if a servlet error occurs
175     */

176    public void doFilter(ServletRequest JavaDoc request,
177                         ServletResponse JavaDoc response,
178                         FilterChain JavaDoc chain)
179                  throws IOException JavaDoc, ServletException JavaDoc {
180       // Conditionally select and set the character encoding to be used
181
if (ignore || (request.getCharacterEncoding() == null)) {
182          String JavaDoc pencoding = selectEncoding(request);
183
184          if (pencoding != null) {
185             request.setCharacterEncoding(pencoding);
186          }
187       }
188
189       // Pass control on to the next filter
190
chain.doFilter(request, response);
191    }
192
193
194    /**
195     * Place this filter into service.
196     *
197     * @param filterConfig The filter configuration object
198     */

199    public void init(FilterConfig JavaDoc afilterConfig) throws ServletException JavaDoc {
200       this.filterConfig = afilterConfig;
201       this.encoding = filterConfig.getInitParameter("encoding");
202
203       String JavaDoc value = filterConfig.getInitParameter("ignore");
204
205       if (value == null) {
206          this.ignore = true;
207       } else if (value.equalsIgnoreCase("true")) {
208          this.ignore = true;
209       } else if (value.equalsIgnoreCase("yes")) {
210          this.ignore = true;
211       } else {
212          this.ignore = false;
213       }
214    }
215
216
217    // ------------------------------------------------------ Protected Methods
218

219    /**
220     * Select an appropriate character encoding to be used, based on the
221     * characteristics of the current request and/or filter initialization
222     * parameters. If no character encoding should be set, return
223     * <code>null</code>.
224     *
225     * <p>
226     * The default implementation unconditionally returns the value configured
227     * by the <strong>encoding</strong> initialization parameter for this
228     * filter.
229     * </p>
230     *
231     * @param request The servlet request we are processing
232     *
233     * @return DOCUMENT ME!
234     */

235    protected String JavaDoc selectEncoding(ServletRequest JavaDoc request) {
236       return (this.encoding);
237    }
238 }
239
Popular Tags