1
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
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 }