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.util.Collections;
26  import java.util.Enumeration;
27  import java.util.List;
28  import java.util.Locale;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletRequestWrapper;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /**
37   * HttpRequestWrapper overriding methods getLocale(), getLocales() to include
38   * the user's preferred locale.
39   */
40  public class LocaleRequestWrapper extends HttpServletRequestWrapper {
41      private final transient Log log = LogFactory
42  	    .getLog(LocaleRequestWrapper.class);
43  
44      private final Locale preferredLocale;
45  
46      /**
47       * Sets preferred local to user's locale
48       * 
49       * @param decorated
50       *                the current decorated request
51       * @param userLocale
52       *                the user's locale
53       */
54      public LocaleRequestWrapper(final HttpServletRequest decorated,
55  	    final Locale userLocale) {
56  	super(decorated);
57  	preferredLocale = userLocale;
58  	if (null == preferredLocale) {
59  	    log.error("preferred locale = null, it is an unexpected value!");
60  	}
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public Locale getLocale() {
68  	if (null != preferredLocale) {
69  	    return preferredLocale;
70  	} else {
71  	    return super.getLocale();
72  	}
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      @SuppressWarnings("unchecked")
80      public Enumeration<Locale> getLocales() {
81  	if (null != preferredLocale) {
82  	    List<Locale> l = Collections.list(super.getLocales());
83  	    if (l.contains(preferredLocale)) {
84  		l.remove(preferredLocale);
85  	    }
86  	    l.add(0, preferredLocale);
87  	    return Collections.enumeration(l);
88  	} else {
89  	    return super.getLocales();
90  	}
91      }
92  
93  }