|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.support.spring.security; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Locale; |
| 22 | + |
| 23 | +import javax.servlet.Filter; |
| 24 | +import javax.servlet.FilterChain; |
| 25 | +import javax.servlet.FilterConfig; |
| 26 | +import javax.servlet.ServletException; |
| 27 | +import javax.servlet.ServletRequest; |
| 28 | +import javax.servlet.ServletResponse; |
| 29 | +import javax.servlet.http.HttpServletRequest; |
| 30 | + |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import org.springframework.context.i18n.LocaleContextHolder; |
| 35 | +import org.springframework.web.servlet.i18n.SessionLocaleResolver; |
| 36 | +import org.springframework.web.util.WebUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Filter sets locale by looking up in the session or uses English as a fallback. |
| 40 | + * |
| 41 | + * Unfortunately RequestContextFilter doesn't respect locale that may be set by |
| 42 | + * SessionLocaleResolver. This leads to improperly localized error messages from Spring Security. |
| 43 | + * This filter fixes such behavior: it looks up locale in the session first or uses default |
| 44 | + * locale (English). To be able to overwrite locale that was set by RequestContextFilter, |
| 45 | + * filter should be invoked after it. |
| 46 | + * |
| 47 | + * @author Slava Semushin |
| 48 | + */ |
| 49 | +class SessionLocaleResolverAwareFilter implements Filter { |
| 50 | + private static final Logger LOG = |
| 51 | + LoggerFactory.getLogger(SessionLocaleResolverAwareFilter.class); |
| 52 | + |
| 53 | + @Override |
| 54 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 55 | + // Intentionally empty: nothing to initialize |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 60 | + throws IOException, ServletException { |
| 61 | + |
| 62 | + try { |
| 63 | + HttpServletRequest req = (HttpServletRequest)request; |
| 64 | + LOG.debug("Handling request {} {}", req.getMethod(), req.getRequestURI()); |
| 65 | + |
| 66 | + Locale locale = (Locale)WebUtils.getSessionAttribute( |
| 67 | + (HttpServletRequest)request, |
| 68 | + SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME |
| 69 | + ); |
| 70 | + |
| 71 | + if (locale == null) { |
| 72 | + locale = Locale.ENGLISH; |
| 73 | + LOG.debug("Locale reset to 'en' (default)"); |
| 74 | + } else { |
| 75 | + LOG.debug("Locale reset to '{}' (from session)", locale); |
| 76 | + } |
| 77 | + |
| 78 | + LocaleContextHolder.setLocale(locale); |
| 79 | + |
| 80 | + } catch (RuntimeException ex) { // NOPMD: AvoidCatchingGenericException |
| 81 | + LOG.warn("Couldn't handle request: {}", ex); |
| 82 | + |
| 83 | + } finally { |
| 84 | + chain.doFilter(request, response); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void destroy() { |
| 90 | + // Intentionally empty: nothing to do |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments