001    /*
002    * Copyright 2004 The Apache Software Foundation
003    *
004    * Licensed under the Apache License, Version 2.0 (the "License");
005    * you may not use this file except in compliance with the License.
006    * You may obtain a copy of the License at
007    *
008    *     http://www.apache.org/licenses/LICENSE-2.0
009    *
010    * Unless required by applicable law or agreed to in writing, software
011    * distributed under the License is distributed on an "AS IS" BASIS,
012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    * See the License for the specific language governing permissions and
014    * limitations under the License.
015    */
016    
017    package filters;
018    
019    
020    import java.io.IOException;
021    import javax.servlet.Filter;
022    import javax.servlet.FilterChain;
023    import javax.servlet.FilterConfig;
024    import javax.servlet.ServletException;
025    import javax.servlet.ServletRequest;
026    import javax.servlet.ServletResponse;
027    import javax.servlet.UnavailableException;
028    
029    
030    /**
031     * <p>Example filter that sets the character encoding to be used in parsing the
032     * incoming request, either unconditionally or only if the client did not
033     * specify a character encoding.  Configuration of this filter is based on
034     * the following initialization parameters:</p>
035     * <ul>
036     * <li><strong>encoding</strong> - The character encoding to be configured
037     *     for this request, either conditionally or unconditionally based on
038     *     the <code>ignore</code> initialization parameter.  This parameter
039     *     is required, so there is no default.</li>
040     * <li><strong>ignore</strong> - If set to "true", any character encoding
041     *     specified by the client is ignored, and the value returned by the
042     *     <code>selectEncoding()</code> method is set.  If set to "false,
043     *     <code>selectEncoding()</code> is called <strong>only</strong> if the
044     *     client has not already specified an encoding.  By default, this
045     *     parameter is set to "true".</li>
046     * </ul>
047     *
048     * <p>Although this filter can be used unchanged, it is also easy to
049     * subclass it and make the <code>selectEncoding()</code> method more
050     * intelligent about what encoding to choose, based on characteristics of
051     * the incoming request (such as the values of the <code>Accept-Language</code>
052     * and <code>User-Agent</code> headers, or a value stashed in the current
053     * user's session.</p>
054     *
055     * @author Craig McClanahan
056     * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
057     */
058    
059    public class SetCharacterEncodingFilter implements Filter {
060    
061    
062        // ----------------------------------------------------- Instance Variables
063    
064    
065        /**
066         * The default character encoding to set for requests that pass through
067         * this filter.
068         */
069        protected String encoding = null;
070    
071    
072        /**
073         * The filter configuration object we are associated with.  If this value
074         * is null, this filter instance is not currently configured.
075         */
076        protected FilterConfig filterConfig = null;
077    
078    
079        /**
080         * Should a character encoding specified by the client be ignored?
081         */
082        protected boolean ignore = true;
083    
084    
085        // --------------------------------------------------------- Public Methods
086    
087    
088        /**
089         * Take this filter out of service.
090         */
091        public void destroy() {
092    
093            this.encoding = null;
094            this.filterConfig = null;
095    
096        }
097    
098    
099        /**
100         * Select and set (if specified) the character encoding to be used to
101         * interpret request parameters for this request.
102         *
103         * @param request The servlet request we are processing
104         * @param result The servlet response we are creating
105         * @param chain The filter chain we are processing
106         *
107         * @exception IOException if an input/output error occurs
108         * @exception ServletException if a servlet error occurs
109         */
110        public void doFilter(ServletRequest request, ServletResponse response,
111                             FilterChain chain)
112            throws IOException, ServletException {
113    
114            // Conditionally select and set the character encoding to be used
115            if (ignore || (request.getCharacterEncoding() == null)) {
116                String encoding = selectEncoding(request);
117                if (encoding != null)
118                    request.setCharacterEncoding(encoding);
119            }
120    
121            // Pass control on to the next filter
122            chain.doFilter(request, response);
123    
124        }
125    
126    
127        /**
128         * Place this filter into service.
129         *
130         * @param filterConfig The filter configuration object
131         */
132        public void init(FilterConfig filterConfig) throws ServletException {
133    
134            this.filterConfig = filterConfig;
135            this.encoding = filterConfig.getInitParameter("encoding");
136            String value = filterConfig.getInitParameter("ignore");
137            if (value == null)
138                this.ignore = true;
139            else if (value.equalsIgnoreCase("true"))
140                this.ignore = true;
141            else if (value.equalsIgnoreCase("yes"))
142                this.ignore = true;
143            else
144                this.ignore = false;
145    
146        }
147    
148    
149        // ------------------------------------------------------ Protected Methods
150    
151    
152        /**
153         * Select an appropriate character encoding to be used, based on the
154         * characteristics of the current request and/or filter initialization
155         * parameters.  If no character encoding should be set, return
156         * <code>null</code>.
157         * <p>
158         * The default implementation unconditionally returns the value configured
159         * by the <strong>encoding</strong> initialization parameter for this
160         * filter.
161         *
162         * @param request The servlet request we are processing
163         */
164        protected String selectEncoding(ServletRequest request) {
165    
166            return (this.encoding);
167    
168        }
169    
170    
171    }