Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » web » filter » [javadoc | source]
    1   /*
    2    * Copyright 2002-2007 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package org.springframework.web.filter;
   18   
   19   import java.io.IOException;
   20   
   21   import javax.servlet.FilterChain;
   22   import javax.servlet.ServletException;
   23   import javax.servlet.http.HttpServletRequest;
   24   import javax.servlet.http.HttpServletResponse;
   25   
   26   import org.springframework.util.ClassUtils;
   27   
   28   /**
   29    * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
   30    * requests. This is useful because current browsers typically do not set a
   31    * character encoding even if specified in the HTML page or form.
   32    *
   33    * <p>This filter can either apply its encoding if the request does not
   34    * already specify an encoding, or enforce this filter's encoding in any case
   35    * ("forceEncoding"="true"). In the latter case, the encoding will also be
   36    * applied as default response encoding on Servlet 2.4+ containers (although
   37    * this will usually be overridden by a full content type set in the view).
   38    *
   39    * @author Juergen Hoeller
   40    * @since 15.03.2004
   41    * @see #setEncoding
   42    * @see #setForceEncoding
   43    * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
   44    * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
   45    */
   46   public class CharacterEncodingFilter extends OncePerRequestFilter {
   47   
   48   	// Determine whether the Servlet 2.4 HttpServletResponse.setCharacterEncoding(String)
   49   	// method is available, for use in the "doFilterInternal" implementation.
   50   	private final static boolean responseSetCharacterEncodingAvailable = ClassUtils.hasMethod(
   51   			HttpServletResponse.class, "setCharacterEncoding", new Class[] {String.class});
   52   
   53   
   54   	private String encoding;
   55   
   56   	private boolean forceEncoding = false;
   57   
   58   
   59   	/**
   60   	 * Set the encoding to use for requests. This encoding will be passed into a
   61   	 * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
   62   	 * <p>Whether this encoding will override existing request encodings
   63   	 * (and whether it will be applied as default response encoding as well)
   64   	 * depends on the {@link #setForceEncoding "forceEncoding"} flag.
   65   	 */
   66   	public void setEncoding(String encoding) {
   67   		this.encoding = encoding;
   68   	}
   69   
   70   	/**
   71   	 * Set whether the configured {@link #setEncoding encoding} of this filter
   72   	 * is supposed to override existing request and response encodings.
   73   	 * <p>Default is "false", i.e. do not modify the encoding if
   74   	 * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
   75   	 * returns a non-null value. Switch this to "true" to enforce the specified
   76   	 * encoding in any case, applying it as default response encoding as well.
   77   	 * <p>Note that the response encoding will only be set on Servlet 2.4+
   78   	 * containers, since Servlet 2.3 did not provide a facility for setting
   79   	 * a default response encoding.
   80   	 */
   81   	public void setForceEncoding(boolean forceEncoding) {
   82   		this.forceEncoding = forceEncoding;
   83   	}
   84   
   85   
   86   	protected void doFilterInternal(
   87   			HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
   88   			throws ServletException, IOException {
   89   
   90   		if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
   91   			request.setCharacterEncoding(this.encoding);
   92   			if (this.forceEncoding && responseSetCharacterEncodingAvailable) {
   93   				response.setCharacterEncoding(this.encoding);
   94   			}
   95   		}
   96   		filterChain.doFilter(request, response);
   97   	}
   98   
   99   }

Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » web » filter » [javadoc | source]