1
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
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 }