001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet.filters.doubleclick;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.servlet.filters.BasePortalFilter;
021    
022    import javax.servlet.FilterChain;
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    import javax.servlet.http.HttpSession;
026    
027    import org.apache.commons.lang.time.StopWatch;
028    
029    /**
030     * @author Olaf Fricke
031     * @author Brian Wing Shun Chan
032     * @author Raymond Augé
033     */
034    public class DoubleClickFilter extends BasePortalFilter {
035    
036            @Override
037            protected void processFilter(
038                            HttpServletRequest request, HttpServletResponse response,
039                            FilterChain filterChain)
040                    throws Exception {
041    
042                    StopWatch stopWatch = null;
043    
044                    if (_log.isDebugEnabled()) {
045                            stopWatch = new StopWatch();
046    
047                            stopWatch.start();
048                    }
049    
050                    HttpSession session = request.getSession(false);
051    
052                    if (session == null) {
053                            processFilter(
054                                    DoubleClickFilter.class, request, response, filterChain);
055                    }
056                    else {
057                            DoubleClickController doubleClickController = null;
058    
059                            synchronized (session) {
060                                    doubleClickController =
061                                            (DoubleClickController)session.getAttribute(
062                                                    _CONTROLLER_KEY);
063    
064                                    if (doubleClickController == null) {
065                                            doubleClickController = new DoubleClickController();
066    
067                                            session.setAttribute(
068                                                    _CONTROLLER_KEY, doubleClickController);
069                                    }
070                            }
071    
072                            boolean ok = false;
073    
074                            try {
075                                    doubleClickController.control(request, response, filterChain);
076    
077                                    ok = true;
078                            }
079                            finally {
080                                    if (stopWatch != null) {
081                                            String completeURL = HttpUtil.getCompleteURL(request);
082    
083                                            if (ok) {
084                                                    _log.debug(
085                                                            "Double click prevention succeeded in " +
086                                                                    stopWatch.getTime() + " ms for " + completeURL);
087                                            }
088                                            else {
089                                                    _log.debug(
090                                                            "Double click prevention failed in " +
091                                                                    stopWatch.getTime() + " ms for " + completeURL);
092                                            }
093                                    }
094                            }
095                    }
096            }
097    
098            private static final String _CONTROLLER_KEY =
099                    DoubleClickFilter.class.getName();
100    
101            private static Log _log = LogFactoryUtil.getLog(DoubleClickFilter.class);
102    
103    }