1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.filters.doubleclick;
16  
17  import com.liferay.portal.kernel.servlet.StringServletResponse;
18  import com.liferay.util.servlet.filters.CacheResponseData;
19  import com.liferay.util.servlet.filters.CacheResponseUtil;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  
24  import javax.servlet.FilterChain;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="DoubleClickController.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Olaf Fricke
33   * @author Brian Wing Shun Chan
34   */
35  public class DoubleClickController implements Serializable {
36  
37      public void control(
38              HttpServletRequest request, HttpServletResponse response,
39              FilterChain filterChain)
40          throws IOException, ServletException {
41  
42          boolean firstRequest = false;
43  
44          StringServletResponse stringResponse = null;
45  
46          synchronized (this) {
47              if (_stringResponse == null) {
48                  firstRequest = true;
49  
50                  _stringResponse = new StringServletResponse(response);
51                  _throwable = null;
52              }
53  
54              stringResponse = _stringResponse;
55          }
56  
57          if (firstRequest) {
58              try {
59                  filterChain.doFilter(request, _stringResponse);
60              }
61              catch (Throwable t) {
62                  _throwable = t;
63              }
64              finally {
65                  synchronized (this) {
66                      _stringResponse = null;
67  
68                      notifyAll();
69                  }
70              }
71          }
72          else {
73              synchronized (this) {
74                  while (_stringResponse != null) {
75                      try {
76                          wait();
77                      }
78                      catch (InterruptedException ie) {
79                          Thread currentThread = Thread.currentThread();
80  
81                          currentThread.interrupt();
82                      }
83                  }
84              }
85          }
86  
87          if (_throwable != null) {
88              try {
89                  throw _throwable;
90              }
91              catch (IOException ioe) {
92                  throw ioe;
93              }
94              catch (ServletException se) {
95                  throw se;
96              }
97              catch (RuntimeException re) {
98                  throw re;
99              }
100             catch (Error e) {
101                 throw e;
102             }
103             catch (Throwable t) {
104                 throw new RuntimeException(t);
105             }
106         }
107 
108         CacheResponseData cacheResponseData = new CacheResponseData(
109             stringResponse);
110 
111         CacheResponseUtil.write(response, cacheResponseData);
112     }
113 
114     private StringServletResponse _stringResponse;
115     private Throwable _throwable;
116 
117 }