001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.core.filters;
020:
021: import java.io.IOException;
022: import java.io.UnsupportedEncodingException;
023: import java.util.Locale;
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: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032: import javax.servlet.jsp.jstl.core.Config;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.struts.Globals;
037:
038: /**
039: * This filter syncs up the Struts and JSTL locales.
040: * @web.filter name="StrutsCharEncodingFilter"
041: */
042: public class StrutsCharEncodingFilter implements Filter {
043: private FilterConfig mFilterConfig = null;
044: private static Log mLogger = LogFactory.getFactory().getInstance(
045: StrutsCharEncodingFilter.class);
046:
047: /**
048: * init
049: */
050: public void init(FilterConfig filterConfig) throws ServletException {
051: mFilterConfig = filterConfig;
052: }
053:
054: /**
055: * destroy
056: */
057: public void destroy() {
058: }
059:
060: /**
061: * Set the character encoding and sync up Struts and JSTL locales. This filter should normally be first (and last)
062: * in the chain.
063: */
064: public void doFilter(ServletRequest req, ServletResponse res,
065: FilterChain chain) throws IOException, ServletException {
066: if (mLogger.isDebugEnabled())
067: mLogger.debug("Processing CharEncodingFilter");
068: try {
069:
070: // Keep JSTL and Struts Locale's in sync
071: // NOTE: The session here will get created if it is not present. This code was taken from its
072: // earlier incarnation in RequestFilter, which also caused the session to be created.
073: HttpSession session = ((HttpServletRequest) req)
074: .getSession();
075: if (mLogger.isDebugEnabled())
076: mLogger.debug("Synchronizing JSTL and Struts locales");
077: Locale locale = (Locale) session
078: .getAttribute(Globals.LOCALE_KEY);
079: if (locale == null) {
080: locale = req.getLocale();
081: }
082: if (req.getParameter("locale") != null) {
083: locale = new Locale(req.getParameter("locale"));
084: }
085: session.setAttribute(Globals.LOCALE_KEY, locale);
086: Config.set(session, Config.FMT_LOCALE, locale);
087: if (mLogger.isDebugEnabled())
088: mLogger
089: .debug("Set request character encoding to UTF-8");
090:
091: } catch (Exception e) {
092: // This should never happen
093: throw new ServletException(
094: "Can't set JSTL Config.FMT_LOCALE");
095: }
096:
097: chain.doFilter(req, res);
098: }
099:
100: }
|