package com.sino.publicclass.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* Creation date: (2002-07-02 12:03:06)
* @author: redbeans
*/

public class SetCharacterEncodingFilter implements Filter {
	/**
	* The default character encoding
	*/
	private final static java.lang.String DEFAULT_ENCODING = "utf-8";
	/**
	* The default character encoding to set for requests that pass through
	* this filter.
	*/
	protected String encoding = null;
	/**
	* The filter configuration object we are associated with. If this value
	* is null, this filter instance is not currently configured.
	*/
	protected FilterConfig filterConfig = null;
	/**
	* Should a character encoding specified by the client be ignored?
	*/
	protected boolean ignore = true;

	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
		throws IOException, ServletException {
		try {
			// Conditionally select and set the character encoding to be used
			if (ignore || (request.getCharacterEncoding() == null)) {
				if (encoding != null) {
					request.setCharacterEncoding(encoding);
				} else {
					request.setCharacterEncoding(DEFAULT_ENCODING);
				}
				
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		chain.doFilter(request, response);
	}

	public FilterConfig getFilterConfig() {
		return filterConfig;
	}

	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		this.filterConfig = fConfig;
		this.encoding = fConfig.getInitParameter("CharacterEncoding");
		String value = fConfig.getInitParameter("ignore");
		if (value == null) {
			this.ignore = true;
		} else if (value.equalsIgnoreCase("true")) {
			this.ignore = true;
		} else if (value.equalsIgnoreCase("yes")) {
			this.ignore = true;
		} else {
			this.ignore = false;
		}

	}
	public void setFilterConfig(FilterConfig fConfig) {
		this.filterConfig = fConfig;
	}

}
