/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egiz.pdfas.web.i18n; import java.io.IOException; import java.util.Locale; import java.util.StringTokenizer; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang.LocaleUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; public class LocaleParamFilter implements Filter { private static final Logger LOG = Logger.getLogger(LocaleParamFilter.class); public static final String LOCALE_SESSION_KEY = "at.gv.egiz.pdfas.web.i18n:currentLocale"; public static final String LOCALE_PARAM_KEY = "locale"; public void destroy() { } public static String normalizeLocaleString(String localeString) { if (localeString == null) { return null; } int jsessionIdIndex = localeString.toLowerCase().indexOf(";jsessionid"); if (jsessionIdIndex != -1) { localeString = localeString.substring(0, jsessionIdIndex); } StringTokenizer tokenizer = new StringTokenizer(localeString, "_", false); StringBuffer buffer = new StringBuffer(); int index = 0; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); switch (++index) { case 1: buffer.append(token.toLowerCase()); break; case 2: buffer.append(token.toUpperCase()); break; default: buffer.append(token); break; } if (tokenizer.hasMoreTokens()) { buffer.append("_"); } } return buffer.toString(); } public static Locale getLocale(HttpServletRequest request) { Locale locale = null; HttpSession session = request.getSession(); LOG.trace("Looking for locale parameter \"" + LOCALE_PARAM_KEY + "\"."); String language = request.getParameter(LOCALE_PARAM_KEY); if (!StringUtils.isEmpty(language)) { LOG.debug("Locale parameter \"" + language + "\" found."); String code = normalizeLocaleString(language.trim()); LOG.debug("Normalizing locale -> " + code); try { locale = LocaleUtils.toLocale(code); LOG.info("Setting locale flag in session (ID=" + session.getId() + ") to \"" + locale.toString() + "\"."); session.setAttribute(LOCALE_SESSION_KEY, locale); } catch (IllegalArgumentException e) { LOG.error("Locale \"" + code + "\" is not valid. Flag will not be set."); } } else { LOG.trace("No locale parameter found."); } return locale; } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; LOG.trace("LocaleParamFilter invoked."); getLocale(request); filterChain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig filterConfig) throws ServletException { } }