1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package filters; 19 20 21 import java.io.IOException; 22 23 import javax.servlet.Filter; 24 import javax.servlet.FilterChain; 25 import javax.servlet.FilterConfig; 26 import javax.servlet.ServletException; 27 import javax.servlet.ServletRequest; 28 import javax.servlet.ServletResponse; 29 30 31 /** 32 * <p>Example filter that sets the character encoding to be used in parsing the 33 * incoming request, either unconditionally or only if the client did not 34 * specify a character encoding. Configuration of this filter is based on 35 * the following initialization parameters:</p> 36 * <ul> 37 * <li><strong>encoding</strong> - The character encoding to be configured 38 * for this request, either conditionally or unconditionally based on 39 * the <code>ignore</code> initialization parameter. This parameter 40 * is required, so there is no default.</li> 41 * <li><strong>ignore</strong> - If set to "true", any character encoding 42 * specified by the client is ignored, and the value returned by the 43 * <code>selectEncoding()</code> method is set. If set to "false, 44 * <code>selectEncoding()</code> is called <strong>only</strong> if the 45 * client has not already specified an encoding. By default, this 46 * parameter is set to "true".</li> 47 * </ul> 48 * <p/> 49 * <p>Although this filter can be used unchanged, it is also easy to 50 * subclass it and make the <code>selectEncoding()</code> method more 51 * intelligent about what encoding to choose, based on characteristics of 52 * the incoming request (such as the values of the <code>Accept-Language</code> 53 * and <code>User-Agent</code> headers, or a value stashed in the current 54 * user's session.</p> 55 * 56 * @author Craig McClanahan 57 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 58 */ 59 60 public class SetCharacterEncodingFilter implements Filter { 61 62 // ----------------------------------------------------- Instance Variables 63 64 65 /** 66 * The default character encoding to set for requests that pass through 67 * this filter. 68 */ 69 protected String encoding = null; 70 71 72 /** 73 * The filter configuration object we are associated with. If this value 74 * is null, this filter instance is not currently configured. 75 */ 76 protected FilterConfig filterConfig = null; 77 78 79 /** 80 * Should a character encoding specified by the client be ignored? 81 */ 82 protected boolean ignore = true; 83 84 // --------------------------------------------------------- Public Methods 85 86 87 /** 88 * Take this filter out of service. 89 */ 90 public void destroy() { 91 92 this.encoding = null; 93 this.filterConfig = null; 94 95 } 96 97 98 /** 99 * Select and set (if specified) the character encoding to be used to 100 * interpret request parameters for this request. 101 * 102 * @param request The servlet request we are processing 103 * @param result The servlet response we are creating 104 * @param chain The filter chain we are processing 105 * @throws IOException if an input/output error occurs 106 * @throws ServletException if a servlet error occurs 107 */ 108 public void doFilter(ServletRequest request, ServletResponse response, 109 FilterChain chain) 110 throws IOException, ServletException { 111 112 // Conditionally select and set the character encoding to be used 113 if (ignore || (request.getCharacterEncoding() == null)) { 114 String encoding = selectEncoding(request); 115 if (encoding != null) 116 request.setCharacterEncoding(encoding); 117 } 118 119 // Pass control on to the next filter 120 chain.doFilter(request, response); 121 122 } 123 124 125 /** 126 * Place this filter into service. 127 * 128 * @param filterConfig The filter configuration object 129 */ 130 public void init(FilterConfig filterConfig) throws ServletException { 131 132 this.filterConfig = filterConfig; 133 this.encoding = filterConfig.getInitParameter("encoding"); 134 String value = filterConfig.getInitParameter("ignore"); 135 if (value == null) 136 this.ignore = true; 137 else if (value.equalsIgnoreCase("true")) 138 this.ignore = true; 139 else if (value.equalsIgnoreCase("yes")) 140 this.ignore = true; 141 else 142 this.ignore = false; 143 144 } 145 146 // ------------------------------------------------------ Protected Methods 147 148 149 /** 150 * Select an appropriate character encoding to be used, based on the 151 * characteristics of the current request and/or filter initialization 152 * parameters. If no character encoding should be set, return 153 * <code>null</code>. 154 * <p/> 155 * The default implementation unconditionally returns the value configured 156 * by the <strong>encoding</strong> initialization parameter for this 157 * filter. 158 * 159 * @param request The servlet request we are processing 160 */ 161 protected String selectEncoding(ServletRequest request) { 162 163 return (this.encoding); 164 165 } 166 167 168 }