View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library 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 GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.filter;
24  
25  import java.io.IOException;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import javax.servlet.FilterChain;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  import javax.servlet.jsp.jstl.core.Config;
35  
36  import org.springframework.context.i18n.LocaleContextHolder;
37  import org.springframework.web.filter.OncePerRequestFilter;
38  
39  import com.gisgraphy.Constants;
40  
41  /**
42   * Filter to wrap request with a request including user preferred locale.
43   */
44  public class LocaleFilter extends OncePerRequestFilter {
45  
46      /**
47       * This method looks for a "locale" request parameter. If it finds one, it
48       * sets it as the preferred locale and also configures it to work with JSTL.
49       * 
50       * @param request
51       *                the current request
52       * @param response
53       *                the current response
54       * @param chain
55       *                the chain
56       * @throws IOException
57       *                 when something goes wrong
58       * @throws ServletException
59       *                 when a communication failure happens
60       */
61      @Override
62      @SuppressWarnings("unchecked")
63      public void doFilterInternal(HttpServletRequest request,
64  	    HttpServletResponse response, FilterChain chain)
65  	    throws IOException, ServletException {
66  
67  	String locale = request.getParameter("locale");
68  	Locale preferredLocale = null;
69  
70  	if (locale != null) {
71  	    int indexOfUnderscore = locale.indexOf('-');
72  	    if (indexOfUnderscore != -1) {
73  		String language = locale.substring(0, indexOfUnderscore);
74  		String country = locale.substring(indexOfUnderscore + 1);
75  		preferredLocale = new Locale(language, country);
76  	    } else {
77  		preferredLocale = new Locale(locale);
78  	    }
79  	}
80  
81  	HttpSession session = request.getSession(false);
82  
83  	if (session != null) {
84  	    if (preferredLocale == null) {
85  		preferredLocale = (Locale) session
86  			.getAttribute(Constants.PREFERRED_LOCALE_KEY);
87  	    } else {
88  		session.setAttribute(Constants.PREFERRED_LOCALE_KEY,
89  			preferredLocale);
90  		Config.set(session, Config.FMT_LOCALE, preferredLocale);
91  	    }
92  
93  	    if (preferredLocale != null
94  		    && !(request instanceof LocaleRequestWrapper)) {
95  		request = new LocaleRequestWrapper(request, preferredLocale);
96  		LocaleContextHolder.setLocale(preferredLocale);
97  	    }
98  	}
99  
100 	String theme = request.getParameter("theme");
101 	if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) {
102 	    Map<String, Object> config = (Map) getServletContext()
103 		    .getAttribute(Constants.CONFIG);
104 	    config.put(Constants.CSS_THEME, theme);
105 	}
106 
107 	chain.doFilter(request, response);
108 
109 	// Reset thread-bound LocaleContext.
110 	LocaleContextHolder.setLocaleContext(null);
111     }
112 }