| DoubleClickController.java |
1 /**
2 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions 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.util.servlet.filters.CacheResponse;
26 import com.liferay.util.servlet.filters.CacheResponseData;
27 import com.liferay.util.servlet.filters.CacheResponseUtil;
28
29 import java.io.IOException;
30 import java.io.Serializable;
31
32 import javax.servlet.FilterChain;
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 /**
38 * <a href="DoubleClickController.java.html"><b><i>View Source</i></b></a>
39 *
40 * @author Olaf Fricke
41 * @author Brian Wing Shun Chan
42 *
43 */
44 public class DoubleClickController implements Serializable {
45
46 public void control(
47 HttpServletRequest req, HttpServletResponse res, FilterChain chain)
48 throws IOException, ServletException {
49
50 boolean firstRequest = false;
51
52 CacheResponse cacheResponse = null;
53
54 synchronized (this) {
55 if (_cacheResponse == null) {
56 firstRequest = true;
57
58 _cacheResponse = new CacheResponse(
59 res, DoubleClickFilter.ENCODING);
60 _throwable = null;
61 }
62
63 cacheResponse = _cacheResponse;
64 }
65
66 if (firstRequest) {
67 try {
68 chain.doFilter(req, _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().interrupt();
89 }
90 }
91 }
92 }
93
94 if (_throwable != null) {
95 try {
96 throw _throwable;
97 }
98 catch (IOException ioe) {
99 throw ioe;
100 }
101 catch (ServletException se) {
102 throw se;
103 }
104 catch (RuntimeException re) {
105 throw re;
106 }
107 catch (Error e) {
108 throw e;
109 }
110 catch (Throwable t) {
111 throw new RuntimeException(t);
112 }
113 }
114
115 CacheResponseData data = new CacheResponseData(
116 cacheResponse.getData(), cacheResponse.getContentType(),
117 cacheResponse.getHeaders());
118
119 CacheResponseUtil.write(res, data);
120 }
121
122 private CacheResponse _cacheResponse;
123 private Throwable _throwable;
124
125 }