1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.filters.doubleclick;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.servlet.filters.BasePortalFilter;
21  
22  import javax.servlet.FilterChain;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.commons.lang.time.StopWatch;
28  
29  /**
30   * <a href="DoubleClickFilter.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Olaf Fricke
33   * @author Brian Wing Shun Chan
34   * @author Raymond Augé
35   */
36  public class DoubleClickFilter extends BasePortalFilter {
37  
38      protected void processFilter(
39              HttpServletRequest request, HttpServletResponse response,
40              FilterChain filterChain)
41          throws Exception {
42  
43          StopWatch stopWatch = null;
44  
45          if (_log.isDebugEnabled()) {
46              stopWatch = new StopWatch();
47  
48              stopWatch.start();
49          }
50  
51          HttpSession session = request.getSession(false);
52  
53          if (session == null) {
54              processFilter(
55                  DoubleClickFilter.class, request, response, filterChain);
56          }
57          else {
58              DoubleClickController controller = null;
59  
60              synchronized (session) {
61                  controller = (DoubleClickController)session.getAttribute(
62                      _CONTROLLER_KEY);
63  
64                  if (controller == null) {
65                      controller = new DoubleClickController();
66  
67                      session.setAttribute(_CONTROLLER_KEY, controller);
68                  }
69              }
70  
71              boolean ok = false;
72  
73              try {
74                  controller.control(request, response, filterChain);
75  
76                  ok = true;
77              }
78              finally {
79                  if (_log.isDebugEnabled()) {
80                      String completeURL = HttpUtil.getCompleteURL(request);
81  
82                      if (ok) {
83                          _log.debug(
84                              "Double click prevention succeded in " +
85                                  stopWatch.getTime() + " ms for " + completeURL);
86                      }
87                      else {
88                          _log.debug(
89                              "Double click prevention failed in " +
90                                  stopWatch.getTime() + " ms for " + completeURL);
91                      }
92                  }
93              }
94          }
95      }
96  
97      private static final String _CONTROLLER_KEY =
98          DoubleClickFilter.class.getName();
99  
100     private static Log _log = LogFactoryUtil.getLog(DoubleClickFilter.class);
101 
102 }