View Javadoc

1   package net.sf.cantina.application;
2   
3   import net.sf.cantina.DataSource;
4   import net.sf.cantina.util.Localizer;
5   import org.apache.log4j.Logger;
6   
7   import javax.servlet.*;
8   import javax.servlet.http.HttpServletRequest;
9   import java.io.IOException;
10  import java.util.Locale;
11  
12  /***
13   * Servlet filter that open/closes the hibernate connection upon
14   * reception/response of an HTTP request.
15   * @see net.sf.cantina.datasource.HibernateDataSource
16   */
17  
18  public class ApplicationFilter implements javax.servlet.Filter
19  {
20    public static final Logger logger = Logger.getLogger(ApplicationFilter.class);
21    public static final String PARAM_DEFAULT_LOCALE = "defaultLocale";
22    public static final String SESSION_PARAM_LOCALE = "cantinaLocale";
23    public static final String REQUEST_PARAM_LOCALE = "cantinaLocale";
24    private FilterConfig filterConfig;
25  
26    public FilterConfig getFilterConfig()
27    {
28      return filterConfig;
29    }
30  
31  
32    /***
33      * Gets filter config paramerters like the default locale
34      */
35  
36    public void init(FilterConfig filterConfig) throws ServletException
37    {
38      try
39      {
40        //set filter config
41        this.filterConfig = filterConfig;
42  
43        //init application
44        logger.debug("Starting Cantina");
45        Application.init();
46      } catch (Exception e)
47      {
48        throw new ServletException(e);
49      }
50    }
51       
52    /***
53     * Open/closes the hibernate session.
54     */
55  
56    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
57    throws IOException, ServletException
58    {
59      try
60      {
61        /*
62        Tomcat and resin both interpret requests as latin-1 by default.
63        Tests with mozilla revealed that request.getCharacterEncoding()
64        is mostly null all the time, even when the contentType and encoding are
65        set in the jsp page and in the accept-charset param of the form.
66        */
67        if (request.getCharacterEncoding() == null)
68        {
69          logger.debug("Browser did not send an encoding. Interpreting request as UTF-8.");
70          request.setCharacterEncoding("UTF-8");
71          //response.setContentType("text/html;charset=UTF-8");
72        }
73  
74        //looking at the request to see if user requested a new language
75        setSessionLocale((HttpServletRequest)request);
76  
77        //choose the response locale
78        setResponseLocale((HttpServletRequest)request,response);
79  
80        //continuing
81        chain.doFilter(request, response);
82      } finally
83      {
84        logger.debug("Closing datasource session");
85        DataSource.getInstance().closeSession();
86      }
87    }
88  
89    public static void setSessionLocale(HttpServletRequest r)
90    {
91      String requestLocaleParameter = r.getParameter(REQUEST_PARAM_LOCALE);
92      if (requestLocaleParameter != null && !"".equals(requestLocaleParameter))
93        r.getSession().setAttribute(SESSION_PARAM_LOCALE,Localizer.parseLocaleString(requestLocaleParameter));
94    }
95  
96    public void setResponseLocale(HttpServletRequest request,ServletResponse response)
97    {
98      Locale configLocale = null, responseLocale = null , sessionLocale = null;
99      String configLocaleString = getFilterConfig().getInitParameter(PARAM_DEFAULT_LOCALE);
100     if (configLocaleString != null && !"".equals(configLocaleString))
101       configLocale = Localizer.parseLocaleString(configLocaleString);
102     sessionLocale = (Locale)((HttpServletRequest)request).getSession().getAttribute(SESSION_PARAM_LOCALE);
103     if (null !=  sessionLocale)
104       responseLocale = sessionLocale;
105     else if (configLocale != null)
106       responseLocale = configLocale;
107     if (responseLocale != null)
108     {
109       logger.debug("Setting response locale to ["+responseLocale.getDisplayName()+"]");
110       response.setLocale(responseLocale);
111     }
112   }
113   /***
114    * Nothing for now.
115    */
116 
117   public void destroy()
118   {
119     logger.debug("Shutting down Cantina");
120     try
121     {
122       Application.shut();
123     } catch (Exception e)
124     {
125       logger.error("Error shutting down cantina",e);
126     }
127   }
128 }