001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 019 package filters; 020 021 022 import java.io.IOException; 023 024 import javax.servlet.Filter; 025 import javax.servlet.FilterChain; 026 import javax.servlet.FilterConfig; 027 import javax.servlet.ServletException; 028 import javax.servlet.ServletRequest; 029 import javax.servlet.ServletResponse; 030 031 032 /** 033 * <p>Example filter that sets the character encoding to be used in parsing the 034 * incoming request, either unconditionally or only if the client did not 035 * specify a character encoding. Configuration of this filter is based on 036 * the following initialization parameters:</p> 037 * <ul> 038 * <li><strong>encoding</strong> - The character encoding to be configured 039 * for this request, either conditionally or unconditionally based on 040 * the <code>ignore</code> initialization parameter. This parameter 041 * is required, so there is no default.</li> 042 * <li><strong>ignore</strong> - If set to "true", any character encoding 043 * specified by the client is ignored, and the value returned by the 044 * <code>selectEncoding()</code> method is set. If set to "false, 045 * <code>selectEncoding()</code> is called <strong>only</strong> if the 046 * client has not already specified an encoding. By default, this 047 * parameter is set to "true".</li> 048 * </ul> 049 * <p/> 050 * <p>Although this filter can be used unchanged, it is also easy to 051 * subclass it and make the <code>selectEncoding()</code> method more 052 * intelligent about what encoding to choose, based on characteristics of 053 * the incoming request (such as the values of the <code>Accept-Language</code> 054 * and <code>User-Agent</code> headers, or a value stashed in the current 055 * user's session.</p> 056 * 057 * @author Craig McClanahan 058 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 059 */ 060 061 public class SetCharacterEncodingFilter implements Filter { 062 063 // ----------------------------------------------------- Instance Variables 064 065 066 /** 067 * The default character encoding to set for requests that pass through 068 * this filter. 069 */ 070 protected String encoding = null; 071 072 073 /** 074 * The filter configuration object we are associated with. If this value 075 * is null, this filter instance is not currently configured. 076 */ 077 protected FilterConfig filterConfig = null; 078 079 080 /** 081 * Should a character encoding specified by the client be ignored? 082 */ 083 protected boolean ignore = true; 084 085 // --------------------------------------------------------- Public Methods 086 087 088 /** 089 * Take this filter out of service. 090 */ 091 public void destroy() { 092 093 this.encoding = null; 094 this.filterConfig = null; 095 096 } 097 098 099 /** 100 * Select and set (if specified) the character encoding to be used to 101 * interpret request parameters for this request. 102 * 103 * @param request The servlet request we are processing 104 * @param result The servlet response we are creating 105 * @param chain The filter chain we are processing 106 * @throws IOException if an input/output error occurs 107 * @throws ServletException if a servlet error occurs 108 */ 109 public void doFilter(ServletRequest request, ServletResponse response, 110 FilterChain chain) 111 throws IOException, ServletException { 112 113 // Conditionally select and set the character encoding to be used 114 if (ignore || (request.getCharacterEncoding() == null)) { 115 String encoding = selectEncoding(request); 116 if (encoding != null) 117 request.setCharacterEncoding(encoding); 118 } 119 120 // Pass control on to the next filter 121 chain.doFilter(request, response); 122 123 } 124 125 126 /** 127 * Place this filter into service. 128 * 129 * @param filterConfig The filter configuration object 130 */ 131 public void init(FilterConfig filterConfig) throws ServletException { 132 133 this.filterConfig = filterConfig; 134 this.encoding = filterConfig.getInitParameter("encoding"); 135 String value = filterConfig.getInitParameter("ignore"); 136 if (value == null) 137 this.ignore = true; 138 else if (value.equalsIgnoreCase("true")) 139 this.ignore = true; 140 else if (value.equalsIgnoreCase("yes")) 141 this.ignore = true; 142 else 143 this.ignore = false; 144 145 } 146 147 // ------------------------------------------------------ Protected Methods 148 149 150 /** 151 * Select an appropriate character encoding to be used, based on the 152 * characteristics of the current request and/or filter initialization 153 * parameters. If no character encoding should be set, return 154 * <code>null</code>. 155 * <p/> 156 * The default implementation unconditionally returns the value configured 157 * by the <strong>encoding</strong> initialization parameter for this 158 * filter. 159 * 160 * @param request The servlet request we are processing 161 */ 162 protected String selectEncoding(ServletRequest request) { 163 164 return (this.encoding); 165 166 } 167 168 169 }