KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > filters > SetCharacterEncodingFilter


1 /*
2  * $Header: /cvsroot/jgossipforum/release_01_01/src/org/jresearch/gossip/filters/SetCharacterEncodingFilter.java,v 1.3 2005/06/07 12:32:26 bel70 Exp $
3  * $Revision: 1.3 $
4  * $Date: 2005/06/07 12:32:26 $
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 package org.jresearch.gossip.filters;
65
66 import java.io.IOException JavaDoc;
67 import java.util.Locale JavaDoc;
68
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.http.HttpServletRequest JavaDoc;
76 import javax.servlet.http.HttpSession JavaDoc;
77 import javax.servlet.jsp.jstl.core.Config;
78
79 import org.apache.struts.Globals;
80
81 /**
82  * <p>
83  * Example filter that sets the character encoding to be used in parsing the
84  * incoming request, either unconditionally or only if the client did not
85  * specify a character encoding. Configuration of this filter is based on the
86  * following initialization parameters:
87  * </p>
88  * <ul>
89  * <li><strong>encoding</strong> - The character encoding to be configured for
90  * this request, either conditionally or unconditionally based on the
91  * <code>ignore</code> initialization parameter. This parameter is required,
92  * so there is no default.</li>
93  * <li><strong>ignore</strong> - If set to "true", any character encoding
94  * specified by the client is ignored, and the value returned by the
95  * <code>selectEncoding()</code> method is set. If set to "false,
96  * <code>selectEncoding()</code> is called <strong>only</strong> if the
97  * client has not already specified an encoding. By default, this parameter is
98  * set to "true".</li>
99  * </ul>
100  *
101  * <p>
102  * Although this filter can be used unchanged, it is also easy to subclass it
103  * and make the <code>selectEncoding()</code> method more intelligent about
104  * what encoding to choose, based on characteristics of the incoming request
105  * (such as the values of the <code>Accept-Language</code> and
106  * <code>User-Agent</code> headers, or a value stashed in the current user's
107  * session.
108  * </p>
109  *
110  * @author Craig McClanahan
111  * @version $Revision: 1.3 $ $Date: 2005/06/07 12:32:26 $
112  */

113
114 public class SetCharacterEncodingFilter implements Filter JavaDoc {
115
116     // ----------------------------------------------------- Instance Variables
117

118     /**
119      * The default character encoding to set for requests that pass through this
120      * filter.
121      */

122     protected String JavaDoc encoding = null;
123
124     /**
125      * The filter configuration object we are associated with. If this value is
126      * null, this filter instance is not currently configured.
127      */

128     protected FilterConfig JavaDoc filterConfig = null;
129
130     /**
131      * Should a character encoding specified by the client be ignored?
132      */

133     protected boolean ignore = true;
134
135     // --------------------------------------------------------- Public Methods
136

137     /**
138      * Take this filter out of service.
139      */

140     public void destroy() {
141
142         this.encoding = null;
143         this.filterConfig = null;
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
152      * The servlet request we are processing
153      * @param result
154      * The servlet response we are creating
155      * @param chain
156      * The filter chain we are processing
157      *
158      * @exception IOException
159      * if an input/output error occurs
160      * @exception ServletException
161      * if a servlet error occurs
162      */

163     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
164             FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
165         // Conditionally select and set the character encoding to be used
166
if (ignore || (request.getCharacterEncoding() == null)) {
167             String JavaDoc encoding = selectEncoding(request);
168             if (encoding != null) {
169                 request.setCharacterEncoding(encoding);
170             }
171         }
172         // Syncronize JSTL and struts locale settings
173
HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) request).getSession(true);
174         Locale JavaDoc locale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
175         if (locale == null) {
176             locale = request.getLocale();
177         }
178         Config.set(session, Config.FMT_LOCALE, locale);
179         // Pass control on to the next filter
180
chain.doFilter(request, response);
181
182     }
183
184     /**
185      * Place this filter into service.
186      *
187      * @param filterConfig
188      * The filter configuration object
189      */

190     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
191
192         this.filterConfig = filterConfig;
193         this.encoding = filterConfig.getInitParameter("encoding");
194         String JavaDoc value = filterConfig.getInitParameter("ignore");
195         if (value == null)
196             this.ignore = true;
197         else if (value.equalsIgnoreCase("true"))
198             this.ignore = true;
199         else if (value.equalsIgnoreCase("yes"))
200             this.ignore = true;
201         else
202             this.ignore = false;
203
204     }
205
206     // ------------------------------------------------------ Protected Methods
207

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

221     protected String JavaDoc selectEncoding(ServletRequest JavaDoc request) {
222
223         return (this.encoding);
224
225     }
226
227 }
228
Popular Tags