001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }