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