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 javax.servlet.http.HttpServletRequest;
13 import java.util.ArrayList;
14 import java.util.List;
16 public class LocaleConfiguration {
18 final String defaultLocale;
19 private final List<String> allowedLocales = new ArrayList<>();
21 public LocaleConfiguration(final String defaultLocale,
22 final String... allowedLocales) {
24 this.defaultLocale = defaultLocale;
26 for (final String locale : allowedLocales)
27 getAllowedLocales().add(locale);
30 public String detectCurrentLocale(final HttpServletRequest request) {
32 final String sessionLocaleString = (String) request.getSession()
33 .getAttribute("locale");
35 String result = defaultLocale;
36 if (isAllowedLocale(sessionLocaleString))
37 result = sessionLocaleString;
39 final String requestLocale = request.getParameter("locale");
41 if (isAllowedLocale(requestLocale))
42 result = requestLocale;
44 request.getSession().setAttribute("locale", result);
49 public List<String> getAllowedLocales() {
50 return allowedLocales;
53 public boolean isAllowedLocale(final String locale) {
54 return allowedLocales.contains(locale);