1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.doubleclick;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.util.servlet.filters.CacheResponse;
27  import com.liferay.util.servlet.filters.CacheResponseData;
28  import com.liferay.util.servlet.filters.CacheResponseUtil;
29  
30  import java.io.IOException;
31  import java.io.Serializable;
32  
33  import javax.servlet.FilterChain;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="DoubleClickController.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Olaf Fricke
42   * @author Brian Wing Shun Chan
43   */
44  public class DoubleClickController implements Serializable {
45  
46      public void control(
47              HttpServletRequest request, HttpServletResponse response,
48              FilterChain filterChain)
49          throws IOException, ServletException {
50  
51          boolean firstRequest = false;
52  
53          CacheResponse cacheResponse = null;
54  
55          synchronized (this) {
56              if (_cacheResponse == null) {
57                  firstRequest = true;
58  
59                  _cacheResponse = new CacheResponse(response, StringPool.UTF8);
60                  _throwable = null;
61              }
62  
63              cacheResponse = _cacheResponse;
64          }
65  
66          if (firstRequest) {
67              try {
68                  filterChain.doFilter(request, _cacheResponse);
69              }
70              catch (Throwable t) {
71                  _throwable = t;
72              }
73              finally {
74                  synchronized (this) {
75                      _cacheResponse = null;
76  
77                      notifyAll();
78                  }
79              }
80          }
81          else {
82              synchronized (this) {
83                  while (_cacheResponse != null) {
84                      try {
85                          wait();
86                      }
87                      catch (InterruptedException ie) {
88                          Thread currentThread = Thread.currentThread();
89  
90                          currentThread.interrupt();
91                      }
92                  }
93              }
94          }
95  
96          if (_throwable != null) {
97              try {
98                  throw _throwable;
99              }
100             catch (IOException ioe) {
101                 throw ioe;
102             }
103             catch (ServletException se) {
104                 throw se;
105             }
106             catch (RuntimeException re) {
107                 throw re;
108             }
109             catch (Error e) {
110                 throw e;
111             }
112             catch (Throwable t) {
113                 throw new RuntimeException(t);
114             }
115         }
116 
117         CacheResponseData data = new CacheResponseData(
118             cacheResponse.getData(), cacheResponse.getContentType(),
119             cacheResponse.getHeaders());
120 
121         CacheResponseUtil.write(response, data);
122     }
123 
124     private CacheResponse _cacheResponse;
125     private Throwable _throwable;
126 
127 }