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