- * NOTICE: This filter must set after org.lamsfoundation.lams.web.session.SystemSessionFilter
in
- * web.xml because it need get value from SystemSession .
+ * This filter must set after org.lamsfoundation.lams.web.session.SystemSessionFilter
in web.xml because
+ * it need get value from SystemSession .
*
- * @author Steve.Ni
- *
- * @version $Revision$
+ * @author Steve.Ni, Marcin Cieslak
*/
public class LocaleFilter extends OncePerRequestFilter {
- // private static Logger log = Logger.getLogger(LocaleFilter.class);
- private String encoding;
-
public static final String PREFERRED_LOCALE_KEY = "org.apache.struts.action.LOCALE";
/** Key used in request to get the required direction. Used by the HTML tag */
public static final String DIRECTION = "page_direction";
- /**
- * Set the encoding to use for requests. This encoding will be passed into a ServletRequest.setCharacterEncoding
- * call.
- */
- public void setEncoding(String encoding) {
- this.encoding = encoding;
- }
-
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
- // charset encoding
- if (!StringUtils.isEmpty(encoding)) {
- request.setCharacterEncoding(encoding);
- } else {
- request.setCharacterEncoding("UTF-8");
- }
-
Locale preferredLocale = null;
String direction = null;
TimeZone tz = null;
- // Comment: This getParameter() cause problem when reading WDDX packet, which need request.getInputStream()
- // method.
- // user set has first prority:
- // String locale = request.getParameter("locale");
- // if (locale != null)
- // preferredLocale = new Locale(locale);
- // if request does not assign locale, then get it from database
- if (preferredLocale == null) {
- HttpSession sharedsession = SessionManager.getSession();
- if (sharedsession != null) {
- UserDTO user = (UserDTO) sharedsession.getAttribute(AttributeNames.USER);
- if (user != null) {
- direction = user.getDirection();
- tz = user.getTimeZone();
- String lang = user.getLocaleLanguage();
- String country = user.getLocaleCountry();
- // would prefer both the language and country but that's not always feasible.
- // so we may end up with some confusing situations.
- if (!StringUtils.isEmpty(lang)) {
- preferredLocale = new Locale(lang, country != null ? country : "");
- }
+ // get locale from user settings
+ HttpSession session = SessionManager.getSession();
+ if (session != null) {
+ UserDTO user = (UserDTO) session.getAttribute(AttributeNames.USER);
+ if (user != null) {
+ direction = user.getDirection();
+ tz = user.getTimeZone();
+ String lang = user.getLocaleLanguage();
+ String country = user.getLocaleCountry();
+ // would prefer both the language and country but that's not always feasible.
+ // so we may end up with some confusing situations.
+ if (!StringUtils.isEmpty(lang)) {
+ preferredLocale = new Locale(lang, country == null ? "" : country);
}
}
}
+
+ // get default locale from configuration
if (preferredLocale == null) {
- // if request does not have, set it default then.
String defaults[] = LanguageUtil.getDefaultLangCountry();
- preferredLocale = new Locale(defaults[0] != null ? defaults[0] : "", defaults[1] != null ? defaults[1] : "");
-
+ preferredLocale = new Locale(defaults[0] == null ? "" : defaults[0], defaults[1] == null ? "" : defaults[1]);
}
-
if (direction == null) {
direction = LanguageUtil.getDefaultDirection();
}
if (tz == null) {
LanguageUtil.getDefaultTimeZone();
}
- HttpSession session = request.getSession(false);
// set locale for STURTS and JSTL
// set the time zone - must be set for dates to display the time zone
if (session != null) {
- if (preferredLocale != null) {
- session.setAttribute(LocaleFilter.PREFERRED_LOCALE_KEY, preferredLocale);
- Config.set(session, Config.FMT_LOCALE, preferredLocale);
- session.setAttribute(LocaleFilter.DIRECTION, direction);
- }
+ session.setAttribute(LocaleFilter.PREFERRED_LOCALE_KEY, preferredLocale);
+ session.setAttribute(LocaleFilter.DIRECTION, direction);
+ Config.set(session, Config.FMT_LOCALE, preferredLocale);
Config.set(session, Config.FMT_TIME_ZONE, tz);
}
- if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
+ if (!(request instanceof LocaleRequestWrapper)) {
request = new LocaleRequestWrapper(request, preferredLocale);
LocaleContextHolder.setLocale(preferredLocale);
}
- if (chain != null) {
- chain.doFilter(request, response);
- }
+ chain.doFilter(request, response);
// Reset thread-bound LocaleContext.
LocaleContextHolder.setLocaleContext(null);
}
-
-}
+}
\ No newline at end of file