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