tw.com.javaworld.CH11.SetCharacterEncodingFilter (Java2HTML)

| SetCharacterEncodingFilter.java |

    1 /*
    2  * $Header: /home/cvs/jakarta-servletapi-5/jsr154/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java,v 1.1 2002/09/02 13:14:53 remm Exp $
    3  * $Revision: 1.1 $
    4  * $Date: 2002/09/02 13:14:53 $
    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
    65package tw.com.javaworld.CH11;
    66
    67
    68import java.io.IOException;
    69import javax.servlet.Filter;
    70import javax.servlet.FilterChain;
    71import javax.servlet.FilterConfig;
    72import javax.servlet.ServletException;
    73import javax.servlet.ServletRequest;
    74import javax.servlet.ServletResponse;
    75import javax.servlet.UnavailableException;
    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>
    00 * and <code>User-Agent</code> headers, or a value stashed in the current
    01 * user's session.</p>
    02 *
    03 * @author Craig McClanahan
    04 * @version $Revision: 1.1 $ $Date: 2002/09/02 13:14:53 $
    05 */
    06
    07public class SetCharacterEncodingFilter implements Filter {
    08
    09
    10    // ----------------------------------------------------- Instance Variables
    11
    12
    13    /**
    14     * The default character encoding to set for requests that pass through
    15     * this filter.
    16     */
    17    protected String encoding = null;
    18
    19
    20    /**
    21     * The filter configuration object we are associated with.  If this value
    22     * is null, this filter instance is not currently configured.
    23     */
    24    protected FilterConfig filterConfig = null;
    25
    26
    27    /**
    28     * Should a character encoding specified by the client be ignored?
    29     */
    30    protected boolean ignore = true;
    31
    32
    33    // --------------------------------------------------------- Public Methods
    34
    35
    36    /**
    37     * Take this filter out of service.
    38     */
    39    public void destroy() {
    40
    41        this.encoding = null;
    42        this.filterConfig = null;
    43
    44    }
    45
    46
    47    /**
    48     * Select and set (if specified) the character encoding to be used to
    49     * interpret request parameters for this request.
    50     *
    51     * @param request The servlet request we are processing
    52     * @param result The servlet response we are creating
    53     * @param chain The filter chain we are processing
    54     *
    55     * @exception IOException if an input/output error occurs
    56     * @exception ServletException if a servlet error occurs
    57     */
    58    public void doFilter(ServletRequest request, ServletResponse response,
    59                         FilterChain chain)
    60    throws IOException, ServletException {
    61
    62        // Conditionally select and set the character encoding to be used
    63        if (ignore || (request.getCharacterEncoding() == null)) {
    64            String encoding = selectEncoding(request);
    65            if (encoding != null)
    66                request.setCharacterEncoding(encoding);
    67        }
    68
    69    // Pass control on to the next filter
    70        chain.doFilter(request, response);
    71
    72    }
    73
    74
    75    /**
    76     * Place this filter into service.
    77     *
    78     * @param filterConfig The filter configuration object
    79     */
    80    public void init(FilterConfig filterConfig) throws ServletException {
    81
    82    this.filterConfig = filterConfig;
    83        this.encoding = filterConfig.getInitParameter("encoding");
    84        String value = filterConfig.getInitParameter("ignore");
    85        if (value == null)
    86            this.ignore = true;
    87        else if (value.equalsIgnoreCase("true"))
    88            this.ignore = true;
    89        else if (value.equalsIgnoreCase("yes"))
    90            this.ignore = true;
    91        else
    92            this.ignore = false;
    93
    94    }
    95
    96
    97    // ------------------------------------------------------ Protected Methods
    98
    99
    00    /**
    01     * Select an appropriate character encoding to be used, based on the
    02     * characteristics of the current request and/or filter initialization
    03     * parameters.  If no character encoding should be set, return
    04     * <code>null</code>.
    05     * <p>
    06     * The default implementation unconditionally returns the value configured
    07     * by the <strong>encoding</strong> initialization parameter for this
    08     * filter.
    09     *
    10     * @param request The servlet request we are processing
    11     */
    12    protected String selectEncoding(ServletRequest request) {
    13
    14        return (this.encoding);
    15
    16    }
    17
    18
    19}
    20