001
014
015 package com.liferay.portal.servlet.filters.doubleclick;
016
017 import com.liferay.portal.kernel.servlet.ByteBufferServletResponse;
018 import com.liferay.util.servlet.filters.CacheResponseData;
019 import com.liferay.util.servlet.filters.CacheResponseUtil;
020
021 import java.io.IOException;
022 import java.io.Serializable;
023
024 import javax.servlet.FilterChain;
025 import javax.servlet.ServletException;
026 import javax.servlet.http.HttpServletRequest;
027 import javax.servlet.http.HttpServletResponse;
028
029
034 public class DoubleClickController implements Serializable {
035
036 public void control(
037 HttpServletRequest request, HttpServletResponse response,
038 FilterChain filterChain)
039 throws IOException, ServletException {
040
041 boolean firstRequest = false;
042
043 ByteBufferServletResponse byteBufferResponse = null;
044
045 synchronized (this) {
046 if (_byteBufferResponse == null) {
047 firstRequest = true;
048
049 _byteBufferResponse = new ByteBufferServletResponse(response);
050 _throwable = null;
051 }
052
053 byteBufferResponse = _byteBufferResponse;
054 }
055
056 if (firstRequest) {
057 try {
058 filterChain.doFilter(request, _byteBufferResponse);
059 }
060 catch (Throwable t) {
061 _throwable = t;
062 }
063 finally {
064 synchronized (this) {
065 _byteBufferResponse = null;
066
067 notifyAll();
068 }
069 }
070 }
071 else {
072 synchronized (this) {
073 while (_byteBufferResponse != null) {
074 try {
075 wait();
076 }
077 catch (InterruptedException ie) {
078 Thread currentThread = Thread.currentThread();
079
080 currentThread.interrupt();
081 }
082 }
083 }
084 }
085
086 if (_throwable != null) {
087 try {
088 throw _throwable;
089 }
090 catch (IOException ioe) {
091 throw ioe;
092 }
093 catch (ServletException se) {
094 throw se;
095 }
096 catch (RuntimeException re) {
097 throw re;
098 }
099 catch (Error e) {
100 throw e;
101 }
102 catch (Throwable t) {
103 throw new RuntimeException(t);
104 }
105 }
106
107 CacheResponseData cacheResponseData = new CacheResponseData(
108 byteBufferResponse);
109
110 CacheResponseUtil.write(response, cacheResponseData);
111 }
112
113 private ByteBufferServletResponse _byteBufferResponse;
114 private Throwable _throwable;
115
116 }