View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.interceptors;
20  
21  import java.util.TimeZone;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.log4j.Logger;
27  import org.promotego.beans.UserHolder;
28  import org.springframework.beans.factory.annotation.Required;
29  import org.springframework.web.servlet.ModelAndView;
30  import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
31  
32  /***
33   * @author alf
34   *
35   */
36  public class TimeZoneInterceptor extends
37  		HandlerInterceptorAdapter
38  {
39      /***
40       * The key used to store the time zone in a request attribute.
41       */
42      private static final String TZ_REQUEST_ATTRIBUTE_KEY = "PGTZ";
43  
44      private static Logger m_logger = Logger.getLogger(TimeZoneInterceptor.class);
45      
46      private UserHolder m_userHolder;
47      
48      /***
49       * Set the Time Zone on the user, if the parameter for setting it is present.
50       */
51      @Override
52      public boolean preHandle(HttpServletRequest request,
53              HttpServletResponse response, Object handler) throws Exception
54      {
55          setUserTimeZone(request);
56          
57          return true;
58      }
59  
60      /***
61       * If the user didn't exist before, but does exist after the controller handled
62       * the request, pull the TZ from the request and set it on the user now.
63       */
64      @Override
65      public void postHandle(HttpServletRequest request,
66              HttpServletResponse response, Object handler,
67              ModelAndView modelAndView) throws Exception
68      {
69          if (request.getParameter(TZ_REQUEST_ATTRIBUTE_KEY) != null && m_userHolder.getUser() != null)
70          {
71              m_userHolder.getUser().setTimeZone((TimeZone)request.getAttribute(TZ_REQUEST_ATTRIBUTE_KEY));
72              request.removeAttribute(TZ_REQUEST_ATTRIBUTE_KEY);
73          }
74      }
75  
76      /***
77       * @param request
78       */
79      private void setUserTimeZone(HttpServletRequest request)
80      {
81          String tzValue = request.getParameter("x-VisitorTimeZoneOffset");
82          if (tzValue != null && !tzValue.equalsIgnoreCase("na"))
83          {
84              try
85              {
86                  int tzOffset = Integer.parseInt(tzValue);
87                  m_logger.debug("Got tz offset " + tzOffset);
88                  TimeZone theZone = TimeZone.getTimeZone(TimeZone.getAvailableIDs(tzOffset)[0]);
89                  m_logger.debug("Got time zone " + theZone.toString());
90                  if (m_userHolder.getUser() != null)
91                  {
92                      m_userHolder.getUser().setTimeZone(theZone);
93                  }
94                  else
95                  {
96                      // Store it in the session so it can be retrieved when the User is created.
97                      request.setAttribute(TZ_REQUEST_ATTRIBUTE_KEY, theZone);
98                  }
99              }
100             catch (NumberFormatException e)
101             {
102                 m_logger.info("Got unexpected time zone offset: " + tzValue);
103             } 
104         }
105     }
106 
107     @Required
108     public void setUserHolder(UserHolder userHolder)
109     {
110         m_userHolder = userHolder;
111     }
112 }