-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.network;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.ArrayList;
-import java.util.List;
-
-public class LocaleConfiguration {
-
- final String defaultLocale;
- private final List<String> allowedLocales = new ArrayList<>();
-
- public LocaleConfiguration(final String defaultLocale,
- final String... allowedLocales) {
-
- this.defaultLocale = defaultLocale;
-
- for (final String locale : allowedLocales)
- getAllowedLocales().add(locale);
- }
-
- public String detectCurrentLocale(final HttpServletRequest request) {
-
- final String sessionLocaleString = (String) request.getSession()
- .getAttribute("locale");
-
- String result = defaultLocale;
- if (isAllowedLocale(sessionLocaleString))
- result = sessionLocaleString;
-
- final String requestLocale = request.getParameter("locale");
-
- if (isAllowedLocale(requestLocale))
- result = requestLocale;
-
- request.getSession().setAttribute("locale", result);
-
- return result;
- }
-
- public List<String> getAllowedLocales() {
- return allowedLocales;
- }
-
- public boolean isAllowedLocale(final String locale) {
- return allowedLocales.contains(locale);
- }
-
-}