-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

André,

On 3/16/2009 11:53 AM, André Warnier wrote:
> As far as I understand the HTTP specs, something like
> request.setCharacterEncoding() should only be used (with a charset
> different from iso-8859-1) when a request comes in without a charset
> specification (which also indicates a broken client).

This is my interpretation of the spec.

This is my implementation of a fix:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * A filter to ensure that a valid character encoding is available to the
 * request.<p>
 *
 * @author Chris Schultz
 * @version $Revision: 1.2 $ $Date: 2006-07-14 14:23:43 $
 */
public class EncodingFilter
    implements Filter
{
    public static final String DEFAULT_ENCODING = "UTF-8";

    private String _encoding;

    /**
     * Called by the servlet container to indicate to a filter that it is
     * being put into service.<p>
     *
     * @param config The Filter configuration.
     */
    public void init(FilterConfig config)
    {
        _encoding = config.getInitParameter("encoding");
        if(null == _encoding)
            _encoding = DEFAULT_ENCODING;
    }

    protected String getDefaultEncoding()
    {
        return _encoding;
    }

    /**
     * Performs the filtering operation provided by this filter.<p>
     *
     * This filter performs the following:
     *
     * Sets the character encoding on the request to that specified in the
     * init parameters, but only if the request does not already have
     * a specified encoding.
     *
     * @param request The request being made to the server.
     * @param response The response object prepared for the client.
     * @param chain The chain of filters providing request services.
     */
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
        throws IOException, ServletException
    {
        if(null == request.getCharacterEncoding())
            request.setCharacterEncoding(this.getDefaultEncoding());

        chain.doFilter(request, response);
    }

    /**
     * Called by the servlet container to indicate that a filter is being
     * taken out of service.<p>
     */
    public void destroy()
    {
    }
}

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkm+xZkACgkQ9CaO5/Lv0PCTvwCeL5ppLbBLpFyF+FZKYtEumfhE
t1wAn2V0bm8OCkQo6EyHJ9WQXhgvCoWf
=XR+P
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to