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.util.StringPool;
18  import com.liferay.util.servlet.filters.CacheResponse;
19  import com.liferay.util.servlet.filters.CacheResponseData;
20  import com.liferay.util.servlet.filters.CacheResponseUtil;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  
25  import javax.servlet.FilterChain;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * <a href="DoubleClickController.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Olaf Fricke
34   * @author Brian Wing Shun Chan
35   */
36  public class DoubleClickController implements Serializable {
37  
38      public void control(
39              HttpServletRequest request, HttpServletResponse response,
40              FilterChain filterChain)
41          throws IOException, ServletException {
42  
43          boolean firstRequest = false;
44  
45          CacheResponse cacheResponse = null;
46  
47          synchronized (this) {
48              if (_cacheResponse == null) {
49                  firstRequest = true;
50  
51                  _cacheResponse = new CacheResponse(response, StringPool.UTF8);
52                  _throwable = null;
53              }
54  
55              cacheResponse = _cacheResponse;
56          }
57  
58          if (firstRequest) {
59              try {
60                  filterChain.doFilter(request, _cacheResponse);
61              }
62              catch (Throwable t) {
63                  _throwable = t;
64              }
65              finally {
66                  synchronized (this) {
67                      _cacheResponse = null;
68  
69                      notifyAll();
70                  }
71              }
72          }
73          else {
74              synchronized (this) {
75                  while (_cacheResponse != null) {
76                      try {
77                          wait();
78                      }
79                      catch (InterruptedException ie) {
80                          Thread currentThread = Thread.currentThread();
81  
82                          currentThread.interrupt();
83                      }
84                  }
85              }
86          }
87  
88          if (_throwable != null) {
89              try {
90                  throw _throwable;
91              }
92              catch (IOException ioe) {
93                  throw ioe;
94              }
95              catch (ServletException se) {
96                  throw se;
97              }
98              catch (RuntimeException re) {
99                  throw re;
100             }
101             catch (Error e) {
102                 throw e;
103             }
104             catch (Throwable t) {
105                 throw new RuntimeException(t);
106             }
107         }
108 
109         CacheResponseData cacheResponseData = new CacheResponseData(
110             cacheResponse.unsafeGetData(), cacheResponse.getContentLength(),
111             cacheResponse.getContentType(), cacheResponse.getHeaders());
112 
113         CacheResponseUtil.write(response, cacheResponseData);
114     }
115 
116     private CacheResponse _cacheResponse;
117     private Throwable _throwable;
118 
119 }