1
19
20 package com.liferay.portal.servlet.filters.doubleclick;
21
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.util.servlet.filters.CacheResponse;
24 import com.liferay.util.servlet.filters.CacheResponseData;
25 import com.liferay.util.servlet.filters.CacheResponseUtil;
26
27 import java.io.IOException;
28 import java.io.Serializable;
29
30 import javax.servlet.FilterChain;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35
42 public class DoubleClickController implements Serializable {
43
44 public void control(
45 HttpServletRequest request, HttpServletResponse response,
46 FilterChain filterChain)
47 throws IOException, ServletException {
48
49 boolean firstRequest = false;
50
51 CacheResponse cacheResponse = null;
52
53 synchronized (this) {
54 if (_cacheResponse == null) {
55 firstRequest = true;
56
57 _cacheResponse = new CacheResponse(response, StringPool.UTF8);
58 _throwable = null;
59 }
60
61 cacheResponse = _cacheResponse;
62 }
63
64 if (firstRequest) {
65 try {
66 filterChain.doFilter(request, _cacheResponse);
67 }
68 catch (Throwable t) {
69 _throwable = t;
70 }
71 finally {
72 synchronized (this) {
73 _cacheResponse = null;
74
75 notifyAll();
76 }
77 }
78 }
79 else {
80 synchronized (this) {
81 while (_cacheResponse != null) {
82 try {
83 wait();
84 }
85 catch (InterruptedException ie) {
86 Thread currentThread = Thread.currentThread();
87
88 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(response, data);
120 }
121
122 private CacheResponse _cacheResponse;
123 private Throwable _throwable;
124
125 }