2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.network;
12 import java.util.ArrayList;
13 import java.util.List;
15 import javax.servlet.http.HttpServletRequest;
17 public class LocaleConfiguration {
21 private final List<String> allowedLocales = new ArrayList<String>();
23 public LocaleConfiguration(final String defaultLocale,
24 final String... allowedLocales) {
26 this.defaultLocale = defaultLocale;
28 for (final String locale : allowedLocales)
29 getAllowedLocales().add(locale);
32 public String detectCurrentLocale(final HttpServletRequest request) {
34 final String sessionLocaleString = (String) request.getSession()
35 .getAttribute("locale");
37 String result = defaultLocale;
38 if (isAllowedLocale(sessionLocaleString))
39 result = sessionLocaleString;
41 final String requestLocale = request.getParameter("locale");
43 if (isAllowedLocale(requestLocale))
44 result = requestLocale;
46 request.getSession().setAttribute("locale", result);
51 public List<String> getAllowedLocales() {
52 return allowedLocales;
55 public boolean isAllowedLocale(final String locale) {
56 return allowedLocales.contains(locale);