001    /**
002     * Copyright (c) 2000-2010 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.servlet.StringServletResponse;
018    import com.liferay.util.servlet.filters.CacheResponseData;
019    import com.liferay.util.servlet.filters.CacheResponseUtil;
020    
021    import java.io.IOException;
022    import java.io.Serializable;
023    
024    import javax.servlet.FilterChain;
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Olaf Fricke
031     * @author Brian Wing Shun Chan
032     */
033    public class DoubleClickController implements Serializable {
034    
035            public void control(
036                            HttpServletRequest request, HttpServletResponse response,
037                            FilterChain filterChain)
038                    throws IOException, ServletException {
039    
040                    boolean firstRequest = false;
041    
042                    StringServletResponse stringResponse = null;
043    
044                    synchronized (this) {
045                            if (_stringResponse == null) {
046                                    firstRequest = true;
047    
048                                    _stringResponse = new StringServletResponse(response);
049                                    _throwable = null;
050                            }
051    
052                            stringResponse = _stringResponse;
053                    }
054    
055                    if (firstRequest) {
056                            try {
057                                    filterChain.doFilter(request, _stringResponse);
058                            }
059                            catch (Throwable t) {
060                                    _throwable = t;
061                            }
062                            finally {
063                                    synchronized (this) {
064                                            _stringResponse = null;
065    
066                                            notifyAll();
067                                    }
068                            }
069                    }
070                    else {
071                            synchronized (this) {
072                                    while (_stringResponse != null) {
073                                            try {
074                                                    wait();
075                                            }
076                                            catch (InterruptedException ie) {
077                                                    Thread currentThread = Thread.currentThread();
078    
079                                                    currentThread.interrupt();
080                                            }
081                                    }
082                            }
083                    }
084    
085                    if (_throwable != null) {
086                            try {
087                                    throw _throwable;
088                            }
089                            catch (IOException ioe) {
090                                    throw ioe;
091                            }
092                            catch (ServletException se) {
093                                    throw se;
094                            }
095                            catch (RuntimeException re) {
096                                    throw re;
097                            }
098                            catch (Error e) {
099                                    throw e;
100                            }
101                            catch (Throwable t) {
102                                    throw new RuntimeException(t);
103                            }
104                    }
105    
106                    CacheResponseData cacheResponseData = new CacheResponseData(
107                            stringResponse);
108    
109                    CacheResponseUtil.write(response, cacheResponseData);
110            }
111    
112            private StringServletResponse _stringResponse;
113            private Throwable _throwable;
114    
115    }