1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.filters.doubleclick;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.util.Http;
27  import com.liferay.util.SystemProperties;
28  
29  import java.io.IOException;
30  
31  import javax.servlet.Filter;
32  import javax.servlet.FilterChain;
33  import javax.servlet.FilterConfig;
34  import javax.servlet.ServletException;
35  import javax.servlet.ServletRequest;
36  import javax.servlet.ServletResponse;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpSession;
40  
41  import org.apache.commons.lang.time.StopWatch;
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="DoubleClickFilter.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Olaf Fricke
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class DoubleClickFilter implements Filter {
53  
54      public static final boolean USE_FILTER = GetterUtil.getBoolean(
55          SystemProperties.get(DoubleClickFilter.class.getName()), true);
56  
57      public static final String ENCODING = GetterUtil.getString(
58          SystemProperties.get("file.encoding"), "UTF-8");
59  
60      public void init(FilterConfig config) throws ServletException {
61      }
62  
63      public void doFilter(
64              ServletRequest req, ServletResponse res, FilterChain chain)
65          throws IOException, ServletException {
66  
67          if (_log.isDebugEnabled()) {
68              if (USE_FILTER) {
69                  _log.debug("Double click prevention is enabled");
70              }
71              else {
72                  _log.debug("Double click prevention is disabled");
73              }
74          }
75  
76          if (USE_FILTER) {
77              HttpServletRequest httpReq = (HttpServletRequest)req;
78              HttpServletResponse httpRes = (HttpServletResponse)res;
79  
80              StopWatch stopWatch = null;
81  
82              if (_log.isDebugEnabled()) {
83                  stopWatch = new StopWatch();
84  
85                  stopWatch.start();
86              }
87  
88              HttpSession ses = httpReq.getSession(false);
89  
90              if (ses == null) {
91                  chain.doFilter(req, res);
92              }
93              else {
94                  DoubleClickController controller = null;
95  
96                  synchronized (ses) {
97                      controller = (DoubleClickController)ses.getAttribute(
98                          _CONTROLLER_KEY);
99  
100                     if (controller == null) {
101                         controller = new DoubleClickController();
102 
103                         ses.setAttribute(_CONTROLLER_KEY, controller);
104                     }
105                 }
106 
107                 boolean ok = false;
108 
109                 try {
110                     controller.control(httpReq, httpRes, chain);
111 
112                     ok = true;
113                 }
114                 finally {
115                     if (_log.isDebugEnabled()) {
116                         String completeURL = Http.getCompleteURL(httpReq);
117 
118                         if (ok) {
119                             _log.debug(
120                                 "Double click prevention succeded in " +
121                                     stopWatch.getTime() + " ms for " +
122                                         completeURL);
123                         }
124                         else {
125                             _log.debug(
126                                 "Double click prevention failed in " +
127                                     stopWatch.getTime() + " ms for " +
128                                         completeURL);
129                         }
130                     }
131                 }
132             }
133         }
134         else {
135             chain.doFilter(req, res);
136         }
137     }
138 
139     public void destroy() {
140     }
141 
142     private static final String _CONTROLLER_KEY =
143         DoubleClickFilter.class.getName();
144 
145     private static Log _log = LogFactory.getLog(DoubleClickFilter.class);
146 
147 }