1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.continuation;
20
21 import java.io.IOException;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.FilterConfig;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ContinuationFilter implements Filter
54 {
55 static boolean _initialized;
56 static boolean __debug;
57 private boolean _faux;
58 private boolean _jetty6;
59 private boolean _filtered;
60 ServletContext _context;
61 private boolean _debug;
62
63 public void init(FilterConfig filterConfig) throws ServletException
64 {
65 boolean jetty_7_or_greater="org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName());
66 _context = filterConfig.getServletContext();
67
68 String param=filterConfig.getInitParameter("debug");
69 _debug=param!=null&&Boolean.parseBoolean(param);
70 if (_debug)
71 __debug=true;
72
73 param=filterConfig.getInitParameter("jetty6");
74 if (param==null)
75 param=filterConfig.getInitParameter("partial");
76 if (param!=null)
77 _jetty6=Boolean.parseBoolean(param);
78 else
79 _jetty6=ContinuationSupport.__jetty6 && !jetty_7_or_greater;
80
81 param=filterConfig.getInitParameter("faux");
82 if (param!=null)
83 _faux=Boolean.parseBoolean(param);
84 else
85 _faux=!(jetty_7_or_greater || _jetty6 || _context.getMajorVersion()>=3);
86
87 _filtered=_faux||_jetty6;
88 if (_debug)
89 _context.log("ContinuationFilter "+
90 " jetty="+jetty_7_or_greater+
91 " jetty6="+_jetty6+
92 " faux="+_faux+
93 " filtered="+_filtered+
94 " servlet3="+ContinuationSupport.__servlet3);
95 _initialized=true;
96 }
97
98 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
99 {
100 if (_filtered)
101 {
102 Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
103 FilteredContinuation fc;
104 if (_faux && (c==null || !(c instanceof FauxContinuation)))
105 {
106 fc = new FauxContinuation(request);
107 request.setAttribute(Continuation.ATTRIBUTE,fc);
108 }
109 else
110 fc=(FilteredContinuation)c;
111
112 boolean complete=false;
113 while (!complete)
114 {
115 try
116 {
117 if (fc==null || (fc).enter(response))
118 chain.doFilter(request,response);
119 }
120 catch (ContinuationThrowable e)
121 {
122 debug("faux",e);
123 }
124 finally
125 {
126 if (fc==null)
127 fc = (FilteredContinuation) request.getAttribute(Continuation.ATTRIBUTE);
128
129 complete=fc==null || (fc).exit();
130 }
131 }
132 }
133 else
134 {
135 try
136 {
137 chain.doFilter(request,response);
138 }
139 catch (ContinuationThrowable e)
140 {
141 debug("caught",e);
142 }
143 }
144 }
145
146 private void debug(String string)
147 {
148 if (_debug)
149 {
150 _context.log(string);
151 }
152 }
153
154 private void debug(String string, Throwable th)
155 {
156 if (_debug)
157 {
158 if (th instanceof ContinuationThrowable)
159 _context.log(string+":"+th);
160 else
161 _context.log(string,th);
162 }
163 }
164
165 public void destroy()
166 {
167 }
168
169 public interface FilteredContinuation extends Continuation
170 {
171 boolean enter(ServletResponse response);
172 boolean exit();
173 }
174 }