1 18 19 package org.apache.roller.ui.core.filters; 20 21 import java.io.IOException ; 22 import java.io.UnsupportedEncodingException ; 23 import java.util.Locale ; 24 import javax.servlet.Filter ; 25 import javax.servlet.FilterChain ; 26 import javax.servlet.FilterConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.ServletResponse ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpSession ; 32 import javax.servlet.jsp.jstl.core.Config; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.struts.Globals; 37 38 46 47 public class CharEncodingFilter implements Filter 48 { 49 private FilterConfig mFilterConfig = null; 50 private static Log mLogger = 51 LogFactory.getFactory().getInstance(CharEncodingFilter.class); 52 53 56 public void init(FilterConfig filterConfig) throws ServletException 57 { 58 mFilterConfig = filterConfig; 59 } 60 61 64 public void destroy() 65 { 66 } 67 68 72 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 73 throws IOException , ServletException 74 { 75 if (mLogger.isDebugEnabled()) mLogger.debug("Processing CharEncodingFilter"); 76 try 77 { 78 req.setCharacterEncoding("UTF-8"); 79 if (mLogger.isDebugEnabled()) mLogger.debug("Set request character encoding to UTF-8"); 80 } 81 catch (UnsupportedEncodingException e) 82 { 83 throw new ServletException ("Can't set incoming encoding to UTF-8"); 85 } 86 87 HttpSession session = ((HttpServletRequest ) req).getSession(); 91 if (mLogger.isDebugEnabled()) mLogger.debug("Synchronizing JSTL and Struts locales"); 92 Locale locale = (Locale ) session.getAttribute(Globals.LOCALE_KEY); 93 if (locale == null) 94 { 95 locale = req.getLocale(); 96 } 97 if (req.getParameter("locale") != null) 98 { 99 locale = new Locale (req.getParameter("locale")); 100 } 101 session.setAttribute(Globals.LOCALE_KEY, locale); 102 Config.set(session, Config.FMT_LOCALE, locale); 103 104 chain.doFilter(req, res); 105 } 106 107 } 108 | Popular Tags |