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 package filters; 019 020 021 import java.io.IOException; 022 023 import javax.servlet.Filter; 024 import javax.servlet.FilterChain; 025 import javax.servlet.FilterConfig; 026 import javax.servlet.ServletException; 027 import javax.servlet.ServletRequest; 028 import javax.servlet.ServletResponse; 029 030 031 /** 032 * <p>Example filter that sets the character encoding to be used in parsing the 033 * incoming request, either unconditionally or only if the client did not 034 * specify a character encoding. Configuration of this filter is based on 035 * the following initialization parameters:</p> 036 * <ul> 037 * <li><strong>encoding</strong> - The character encoding to be configured 038 * for this request, either conditionally or unconditionally based on 039 * the <code>ignore</code> initialization parameter. This parameter 040 * is required, so there is no default.</li> 041 * <li><strong>ignore</strong> - If set to "true", any character encoding 042 * specified by the client is ignored, and the value returned by the 043 * <code>selectEncoding()</code> method is set. If set to "false, 044 * <code>selectEncoding()</code> is called <strong>only</strong> if the 045 * client has not already specified an encoding. By default, this 046 * parameter is set to "true".</li> 047 * </ul> 048 * <p/> 049 * <p>Although this filter can be used unchanged, it is also easy to 050 * subclass it and make the <code>selectEncoding()</code> method more 051 * intelligent about what encoding to choose, based on characteristics of 052 * the incoming request (such as the values of the <code>Accept-Language</code> 053 * and <code>User-Agent</code> headers, or a value stashed in the current 054 * user's session.</p> 055 * 056 * @author Craig McClanahan 057 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 058 */ 059 060 public class SetCharacterEncodingFilter implements Filter { 061 062 // ----------------------------------------------------- Instance Variables 063 064 065 /** 066 * The default character encoding to set for requests that pass through 067 * this filter. 068 */ 069 protected String encoding = null; 070 071 072 /** 073 * The filter configuration object we are associated with. If this value 074 * is null, this filter instance is not currently configured. 075 */ 076 protected FilterConfig filterConfig = null; 077 078 079 /** 080 * Should a character encoding specified by the client be ignored? 081 */ 082 protected boolean ignore = true; 083 084 // --------------------------------------------------------- Public Methods 085 086 087 /** 088 * Take this filter out of service. 089 */ 090 public void destroy() { 091 092 this.encoding = null; 093 this.filterConfig = null; 094 095 } 096 097 098 /** 099 * 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 }