1 63 64 package org.jresearch.gossip.filters; 65 66 import java.io.IOException ; 67 import java.util.Locale ; 68 69 import javax.servlet.Filter ; 70 import javax.servlet.FilterChain ; 71 import javax.servlet.FilterConfig ; 72 import javax.servlet.ServletException ; 73 import javax.servlet.ServletRequest ; 74 import javax.servlet.ServletResponse ; 75 import javax.servlet.http.HttpServletRequest ; 76 import javax.servlet.http.HttpSession ; 77 import javax.servlet.jsp.jstl.core.Config; 78 79 import org.apache.struts.Globals; 80 81 113 114 public class SetCharacterEncodingFilter implements Filter { 115 116 118 122 protected String encoding = null; 123 124 128 protected FilterConfig filterConfig = null; 129 130 133 protected boolean ignore = true; 134 135 137 140 public void destroy() { 141 142 this.encoding = null; 143 this.filterConfig = null; 144 145 } 146 147 163 public void doFilter(ServletRequest request, ServletResponse response, 164 FilterChain chain) throws IOException , ServletException { 165 if (ignore || (request.getCharacterEncoding() == null)) { 167 String encoding = selectEncoding(request); 168 if (encoding != null) { 169 request.setCharacterEncoding(encoding); 170 } 171 } 172 HttpSession session = ((HttpServletRequest ) request).getSession(true); 174 Locale locale = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 175 if (locale == null) { 176 locale = request.getLocale(); 177 } 178 Config.set(session, Config.FMT_LOCALE, locale); 179 chain.doFilter(request, response); 181 182 } 183 184 190 public void init(FilterConfig filterConfig) throws ServletException { 191 192 this.filterConfig = filterConfig; 193 this.encoding = filterConfig.getInitParameter("encoding"); 194 String value = filterConfig.getInitParameter("ignore"); 195 if (value == null) 196 this.ignore = true; 197 else if (value.equalsIgnoreCase("true")) 198 this.ignore = true; 199 else if (value.equalsIgnoreCase("yes")) 200 this.ignore = true; 201 else 202 this.ignore = false; 203 204 } 205 206 208 221 protected String selectEncoding(ServletRequest request) { 222 223 return (this.encoding); 224 225 } 226 227 } 228 | Popular Tags |