1   /**
2    * Copyright (c) 2000-2008 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.portal.servlet.filters.doubleclick;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.BaseFilter;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.util.SystemProperties;
33  
34  import java.io.IOException;
35  
36  import javax.servlet.FilterChain;
37  import javax.servlet.ServletException;
38  import javax.servlet.ServletRequest;
39  import javax.servlet.ServletResponse;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.http.HttpSession;
43  
44  import org.apache.commons.lang.time.StopWatch;
45  
46  /**
47   * <a href="DoubleClickFilter.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Olaf Fricke
50   * @author Brian Wing Shun Chan
51   * @author Raymond Aug�
52   *
53   */
54  public class DoubleClickFilter extends BaseFilter {
55  
56      public static final boolean USE_FILTER = GetterUtil.getBoolean(
57          PropsUtil.get(DoubleClickFilter.class.getName()), true);
58  
59      public static final String ENCODING = GetterUtil.getString(
60          SystemProperties.get("file.encoding"), StringPool.UTF8);
61  
62      public void doFilter(
63              ServletRequest req, ServletResponse res, FilterChain chain)
64          throws IOException, ServletException {
65  
66          if (_log.isDebugEnabled()) {
67              if (USE_FILTER) {
68                  _log.debug("Double click prevention is enabled");
69              }
70              else {
71                  _log.debug("Double click prevention is disabled");
72              }
73          }
74  
75          if (USE_FILTER) {
76              HttpServletRequest httpReq = (HttpServletRequest)req;
77              HttpServletResponse httpRes = (HttpServletResponse)res;
78  
79              StopWatch stopWatch = null;
80  
81              if (_log.isDebugEnabled()) {
82                  stopWatch = new StopWatch();
83  
84                  stopWatch.start();
85              }
86  
87              HttpSession ses = httpReq.getSession(false);
88  
89              if (ses == null) {
90                  doFilter(DoubleClickFilter.class, req, res, chain);
91              }
92              else {
93                  DoubleClickController controller = null;
94  
95                  synchronized (ses) {
96                      controller = (DoubleClickController)ses.getAttribute(
97                          _CONTROLLER_KEY);
98  
99                      if (controller == null) {
100                         controller = new DoubleClickController();
101 
102                         ses.setAttribute(_CONTROLLER_KEY, controller);
103                     }
104                 }
105 
106                 boolean ok = false;
107 
108                 try {
109                     controller.control(httpReq, httpRes, chain);
110 
111                     ok = true;
112                 }
113                 finally {
114                     if (_log.isDebugEnabled()) {
115                         String completeURL = HttpUtil.getCompleteURL(httpReq);
116 
117                         if (ok) {
118                             _log.debug(
119                                 "Double click prevention succeded in " +
120                                     stopWatch.getTime() + " ms for " +
121                                         completeURL);
122                         }
123                         else {
124                             _log.debug(
125                                 "Double click prevention failed in " +
126                                     stopWatch.getTime() + " ms for " +
127                                         completeURL);
128                         }
129                     }
130                 }
131             }
132         }
133         else {
134             doFilter(DoubleClickFilter.class, req, res, chain);
135         }
136     }
137 
138     private static final String _CONTROLLER_KEY =
139         DoubleClickFilter.class.getName();
140 
141     private static Log _log = LogFactoryUtil.getLog(DoubleClickFilter.class);
142 
143 }