001    /**
002     * Copyright (c) 2000-2010 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            protected void processFilter(
037                            HttpServletRequest request, HttpServletResponse response,
038                            FilterChain filterChain)
039                    throws Exception {
040    
041                    StopWatch stopWatch = null;
042    
043                    if (_log.isDebugEnabled()) {
044                            stopWatch = new StopWatch();
045    
046                            stopWatch.start();
047                    }
048    
049                    HttpSession session = request.getSession(false);
050    
051                    if (session == null) {
052                            processFilter(
053                                    DoubleClickFilter.class, request, response, filterChain);
054                    }
055                    else {
056                            DoubleClickController controller = null;
057    
058                            synchronized (session) {
059                                    controller = (DoubleClickController)session.getAttribute(
060                                            _CONTROLLER_KEY);
061    
062                                    if (controller == null) {
063                                            controller = new DoubleClickController();
064    
065                                            session.setAttribute(_CONTROLLER_KEY, controller);
066                                    }
067                            }
068    
069                            boolean ok = false;
070    
071                            try {
072                                    controller.control(request, response, filterChain);
073    
074                                    ok = true;
075                            }
076                            finally {
077                                    if (_log.isDebugEnabled()) {
078                                            String completeURL = HttpUtil.getCompleteURL(request);
079    
080                                            if (ok) {
081                                                    _log.debug(
082                                                            "Double click prevention succeded in " +
083                                                                    stopWatch.getTime() + " ms for " + completeURL);
084                                            }
085                                            else {
086                                                    _log.debug(
087                                                            "Double click prevention failed in " +
088                                                                    stopWatch.getTime() + " ms for " + completeURL);
089                                            }
090                                    }
091                            }
092                    }
093            }
094    
095            private static final String _CONTROLLER_KEY =
096                    DoubleClickFilter.class.getName();
097    
098            private static Log _log = LogFactoryUtil.getLog(DoubleClickFilter.class);
099    
100    }