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.servlet.BufferCacheServletResponse;
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                    BufferCacheServletResponse bufferCacheServletResponse = null;
043    
044                    synchronized (this) {
045                            if (_bufferCacheServletResponse == null) {
046                                    firstRequest = true;
047    
048                                    _bufferCacheServletResponse = new BufferCacheServletResponse(
049                                            response);
050                                    _throwable = null;
051                            }
052    
053                            bufferCacheServletResponse = _bufferCacheServletResponse;
054                    }
055    
056                    if (firstRequest) {
057                            try {
058                                    filterChain.doFilter(request, _bufferCacheServletResponse);
059                            }
060                            catch (Throwable t) {
061                                    _throwable = t;
062                            }
063                            finally {
064                                    synchronized (this) {
065                                            _bufferCacheServletResponse = null;
066    
067                                            notifyAll();
068                                    }
069                            }
070                    }
071                    else {
072                            synchronized (this) {
073                                    while (_bufferCacheServletResponse != null) {
074                                            try {
075                                                    wait();
076                                            }
077                                            catch (InterruptedException ie) {
078                                                    Thread currentThread = Thread.currentThread();
079    
080                                                    currentThread.interrupt();
081                                            }
082                                    }
083                            }
084                    }
085    
086                    if (_throwable != null) {
087                            try {
088                                    throw _throwable;
089                            }
090                            catch (IOException ioe) {
091                                    throw ioe;
092                            }
093                            catch (ServletException se) {
094                                    throw se;
095                            }
096                            catch (RuntimeException re) {
097                                    throw re;
098                            }
099                            catch (Error e) {
100                                    throw e;
101                            }
102                            catch (Throwable t) {
103                                    throw new RuntimeException(t);
104                            }
105                    }
106    
107                    CacheResponseData cacheResponseData = new CacheResponseData(
108                            bufferCacheServletResponse);
109    
110                    CacheResponseUtil.write(response, cacheResponseData);
111            }
112    
113            private BufferCacheServletResponse _bufferCacheServletResponse;
114            private Throwable _throwable;
115    
116    }