001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.TransientValue;
021    import com.liferay.portal.servlet.filters.BasePortalFilter;
022    
023    import javax.servlet.FilterChain;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    import javax.servlet.http.HttpSession;
027    
028    import org.apache.commons.lang.time.StopWatch;
029    
030    /**
031     * @author Olaf Fricke
032     * @author Brian Wing Shun Chan
033     * @author Raymond Augé
034     */
035    public class DoubleClickFilter extends BasePortalFilter {
036    
037            @Override
038            protected void processFilter(
039                            HttpServletRequest request, HttpServletResponse response,
040                            FilterChain filterChain)
041                    throws Exception {
042    
043                    StopWatch stopWatch = null;
044    
045                    if (_log.isDebugEnabled()) {
046                            stopWatch = new StopWatch();
047    
048                            stopWatch.start();
049                    }
050    
051                    HttpSession session = request.getSession(false);
052    
053                    if (session == null) {
054                            processFilter(
055                                    DoubleClickFilter.class, request, response, filterChain);
056                    }
057                    else {
058                            DoubleClickController doubleClickController = null;
059    
060                            synchronized (session) {
061                                    TransientValue<DoubleClickController> transientValue =
062                                            (TransientValue<DoubleClickController>)session.getAttribute(
063                                                    _CONTROLLER_KEY);
064    
065                                    if (transientValue != null) {
066                                            doubleClickController = transientValue.getValue();
067                                    }
068    
069                                    if (doubleClickController == null) {
070                                            doubleClickController = new DoubleClickController();
071    
072                                            transientValue = new TransientValue<DoubleClickController>(
073                                                    doubleClickController);
074    
075                                            session.setAttribute(_CONTROLLER_KEY, transientValue);
076                                    }
077                            }
078    
079                            boolean ok = false;
080    
081                            try {
082                                    doubleClickController.control(request, response, filterChain);
083    
084                                    ok = true;
085                            }
086                            finally {
087                                    if (stopWatch != null) {
088                                            String completeURL = HttpUtil.getCompleteURL(request);
089    
090                                            if (ok) {
091                                                    _log.debug(
092                                                            "Double click prevention succeeded in " +
093                                                                    stopWatch.getTime() + " ms for " + completeURL);
094                                            }
095                                            else {
096                                                    _log.debug(
097                                                            "Double click prevention failed in " +
098                                                                    stopWatch.getTime() + " ms for " + completeURL);
099                                            }
100                                    }
101                            }
102                    }
103            }
104    
105            private static final String _CONTROLLER_KEY =
106                    DoubleClickFilter.class.getName();
107    
108            private static Log _log = LogFactoryUtil.getLog(DoubleClickFilter.class);
109    
110    }