View Javadoc

1   package net.trajano.twiff.internal.servlet;
2   
3   import java.util.Enumeration;
4   import java.util.Locale;
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletRequestWrapper;
7   
8   /***
9    * This wraps the servlet request with some if its returned elements that
10   * modified by the filter.
11   */
12  public final class LocaleHttpServletRequest extends HttpServletRequestWrapper {
13      /***
14       * Locale.
15       */
16      final Locale locale;
17  
18      /***
19       * Path info.
20       */
21      final String pathInfo;
22  
23      /***
24       * Servlet Path.
25       */
26      final String servletPath;
27  
28      /***
29       * @param request
30       *                   the servlet request.
31       * @param servletPath
32       */
33      public LocaleHttpServletRequest(final HttpServletRequest request, final String servletPath) {
34          super(request);
35          this.locale = null;
36          this.servletPath = servletPath;
37          this.pathInfo = null;
38      }
39  
40      /***
41       * @param request
42       *                   the servlet request.
43       * @param locale
44       *                   locale.
45       * @param servletPath
46       * @param pathInfo
47       */
48      public LocaleHttpServletRequest(final HttpServletRequest request, final Locale locale, final String servletPath, final String pathInfo) {
49          super(request);
50          this.locale = locale;
51          this.servletPath = servletPath;
52          this.pathInfo = pathInfo;
53      }
54  
55      /***
56       * Returns the overridden locale.
57       * 
58       * @return locale.
59       */
60      public Locale getLocale() {
61          if (locale == null)
62              return super.getLocale();
63          return locale;
64      }
65  
66      /***
67       * Returns the list of locales, with the overridden one being the first in
68       * the enumeration.
69       * 
70       * @return enumeration of locales.
71       */
72      public Enumeration getLocales() {
73          if (locale == null)
74              return super.getLocales();
75          Enumeration<Locale> locales = new Enumeration<Locale>() {
76              private Enumeration originalLocaleList = LocaleHttpServletRequest.super.getLocales();
77  
78              private boolean readFirst = false;
79  
80              public boolean hasMoreElements() {
81                  return !readFirst || originalLocaleList.hasMoreElements();
82              }
83  
84              public Locale nextElement() {
85                  if (!readFirst) {
86                      readFirst = true;
87                      return locale;
88                  } else {
89                      return (Locale) originalLocaleList.nextElement();
90                  }
91              }
92          };
93          return locales;
94      }
95  
96      /***
97       * Returns the path info.
98       * 
99       * @return the path info,
100      */
101     public String getPathInfo() {
102         return pathInfo;
103     }
104 
105     /***
106      * Returns the servlet path.
107      * 
108      * @return the servlet path.
109      */
110     public String getServletPath() {
111         return servletPath;
112     }
113 }