Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 934:fe461f7cfc8e
simplify AsyncContinuation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 21:03:00 -0600 |
parents | c9513d80f305 |
children | aa7dc1802d29 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import javax.servlet.AsyncListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import javax.servlet.RequestDispatcher; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import javax.servlet.ServletException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.util.List; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import javax.servlet.ServletContext; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import javax.servlet.ServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import javax.servlet.ServletResponse; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import javax.servlet.http.HttpServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.continuation.Continuation; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.continuation.ContinuationListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.server.handler.ContextHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import org.eclipse.jetty.server.handler.ContextHandler.Context; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.util.URIUtil; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
40 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
41 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 /* ------------------------------------------------------------ */ |
933
c9513d80f305
remove AsyncContext support
Franklin Schmidt <fschmidt@gmail.com>
parents:
932
diff
changeset
|
44 /** Implementation of Continuation interfaces |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 */ |
933
c9513d80f305
remove AsyncContext support
Franklin Schmidt <fschmidt@gmail.com>
parents:
932
diff
changeset
|
47 public class AsyncContinuation implements Continuation |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 { |
865 | 49 private static final Logger LOG = LoggerFactory.getLogger(AsyncContinuation.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
865 | 51 private final static long DEFAULT_TIMEOUT=30000L; |
931
6f7e2ff51879
remove ContinuationThrowable
Franklin Schmidt <fschmidt@gmail.com>
parents:
930
diff
changeset
|
52 |
865 | 53 // STATES: |
54 // handling() suspend() unhandle() resume() complete() doComplete() | |
55 // startAsync() dispatch() | |
56 // IDLE DISPATCHED | |
57 // DISPATCHED ASYNCSTARTED UNCOMPLETED | |
58 // ASYNCSTARTED ASYNCWAIT REDISPATCHING COMPLETING | |
59 // REDISPATCHING REDISPATCHED | |
60 // ASYNCWAIT REDISPATCH COMPLETING | |
61 // REDISPATCH REDISPATCHED | |
62 // REDISPATCHED ASYNCSTARTED UNCOMPLETED | |
63 // COMPLETING UNCOMPLETED UNCOMPLETED | |
64 // UNCOMPLETED COMPLETED | |
65 // COMPLETED | |
66 private static final int __IDLE=0; // Idle request | |
67 private static final int __DISPATCHED=1; // Request dispatched to filter/servlet | |
68 private static final int __UNCOMPLETED=8; // Request is completable | |
69 private static final int __COMPLETED=9; // Request is complete | |
70 | |
71 /* ------------------------------------------------------------ */ | |
72 protected AbstractHttpConnection _connection; | |
73 private List<AsyncListener> _asyncListeners; | |
74 private List<ContinuationListener> _continuationListeners; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
75 |
865 | 76 /* ------------------------------------------------------------ */ |
77 private int _state; | |
78 private volatile long _expireAt; | |
79 | |
80 protected AsyncContinuation() | |
81 { | |
82 _state=__IDLE; | |
83 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
85 protected synchronized void setConnection(final AbstractHttpConnection connection) |
865 | 86 { |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
87 _connection=connection; |
865 | 88 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 |
865 | 90 /* ------------------------------------------------------------ */ |
91 public void addListener(AsyncListener listener) | |
92 { | |
93 synchronized(this) | |
94 { | |
95 if (_asyncListeners==null) | |
96 _asyncListeners=new ArrayList<AsyncListener>(); | |
97 _asyncListeners.add(listener); | |
98 } | |
99 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
100 |
865 | 101 /* ------------------------------------------------------------ */ |
102 public void addListener(AsyncListener listener,ServletRequest request, ServletResponse response) | |
103 { | |
104 synchronized(this) | |
105 { | |
106 // TODO handle the request/response ??? | |
107 if (_asyncListeners==null) | |
108 _asyncListeners=new ArrayList<AsyncListener>(); | |
109 _asyncListeners.add(listener); | |
110 } | |
111 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
865 | 113 /* ------------------------------------------------------------ */ |
114 public void addContinuationListener(ContinuationListener listener) | |
115 { | |
116 synchronized(this) | |
117 { | |
118 if (_continuationListeners==null) | |
119 _continuationListeners=new ArrayList<ContinuationListener>(); | |
120 _continuationListeners.add(listener); | |
121 } | |
122 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
123 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
124 |
865 | 125 |
126 @Override | |
127 public String toString() | |
128 { | |
129 synchronized (this) | |
130 { | |
131 return super.toString()+"@"+getStatusString(); | |
132 } | |
133 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
134 |
865 | 135 public String getStatusString() |
136 { | |
137 synchronized (this) | |
138 { | |
139 return | |
140 ((_state==__IDLE)?"IDLE": | |
141 (_state==__DISPATCHED)?"DISPATCHED": | |
142 (_state==__UNCOMPLETED)?"UNCOMPLETED": | |
143 (_state==__COMPLETED)?"COMPLETE": | |
929 | 144 ("UNKNOWN?"+_state)); |
865 | 145 } |
146 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
929 | 148 protected synchronized void handling() |
865 | 149 { |
929 | 150 switch(_state) |
865 | 151 { |
929 | 152 case __IDLE: |
153 _state=__DISPATCHED; | |
154 if (_asyncListeners!=null) | |
155 _asyncListeners.clear(); | |
156 return; | |
157 | |
158 default: | |
159 throw new IllegalStateException(this.getStatusString()); | |
865 | 160 } |
161 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
865 | 163 /* ------------------------------------------------------------ */ |
164 /** | |
165 * Signal that the HttpConnection has finished handling the request. | |
166 * For blocking connectors, this call may block if the request has | |
167 * been suspended (startAsync called). | |
168 * @return true if handling is complete, false if the request should | |
169 * be handled again (eg because of a resume that happened before unhandle was called) | |
170 */ | |
928 | 171 protected synchronized void unhandle() |
865 | 172 { |
928 | 173 switch(_state) |
865 | 174 { |
928 | 175 case __DISPATCHED: |
176 _state = __UNCOMPLETED; | |
177 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 |
928 | 179 default: |
180 throw new IllegalStateException(this.getStatusString()); | |
865 | 181 } |
182 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
184 |
865 | 185 /* ------------------------------------------------------------ */ |
186 /* (non-Javadoc) | |
187 * @see javax.servlet.ServletRequest#complete() | |
188 */ | |
189 protected void doComplete(Throwable ex) | |
190 { | |
191 final List<ContinuationListener> cListeners; | |
192 final List<AsyncListener> aListeners; | |
193 synchronized (this) | |
194 { | |
195 switch(_state) | |
196 { | |
197 case __UNCOMPLETED: | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
198 _state = __COMPLETED; |
865 | 199 cListeners=_continuationListeners; |
200 aListeners=_asyncListeners; | |
201 break; | |
202 | |
203 default: | |
204 cListeners=null; | |
205 aListeners=null; | |
206 throw new IllegalStateException(this.getStatusString()); | |
207 } | |
208 } | |
209 | |
210 if (aListeners!=null) | |
211 { | |
212 for (AsyncListener listener : aListeners) | |
213 { | |
214 try | |
215 { | |
216 if (ex!=null) | |
217 { | |
932
947df3056ddb
remove AsyncEventState
Franklin Schmidt <fschmidt@gmail.com>
parents:
931
diff
changeset
|
218 throw new UnsupportedOperationException(); |
865 | 219 } |
220 else | |
932
947df3056ddb
remove AsyncEventState
Franklin Schmidt <fschmidt@gmail.com>
parents:
931
diff
changeset
|
221 listener.onComplete(null); |
865 | 222 } |
223 catch(Exception e) | |
224 { | |
225 LOG.warn("",e); | |
226 } | |
227 } | |
228 } | |
229 if (cListeners!=null) | |
230 { | |
231 for (ContinuationListener listener : cListeners) | |
232 { | |
233 try | |
234 { | |
235 listener.onComplete(this); | |
236 } | |
237 catch(Exception e) | |
238 { | |
239 LOG.warn("",e); | |
240 } | |
241 } | |
242 } | |
243 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
244 |
865 | 245 /* ------------------------------------------------------------ */ |
246 protected void recycle() | |
247 { | |
248 synchronized (this) | |
249 { | |
250 switch(_state) | |
251 { | |
252 case __DISPATCHED: | |
253 throw new IllegalStateException(getStatusString()); | |
254 default: | |
255 _state=__IDLE; | |
256 } | |
257 cancelTimeout(); | |
258 _continuationListeners=null; | |
259 } | |
260 } | |
261 | |
262 /* ------------------------------------------------------------ */ | |
263 protected void cancelTimeout() | |
264 { | |
265 EndPoint endp=_connection.getEndPoint(); | |
266 if (endp.isBlocking()) | |
267 { | |
268 synchronized(this) | |
269 { | |
270 _expireAt=0; | |
271 this.notifyAll(); | |
272 } | |
273 } | |
274 else | |
275 { | |
276 } | |
277 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
278 |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
279 synchronized boolean isUncompleted() |
865 | 280 { |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
281 return _state==__UNCOMPLETED; |
865 | 282 } |
283 | |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
284 public synchronized boolean isComplete() |
865 | 285 { |
934
fe461f7cfc8e
simplify AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
933
diff
changeset
|
286 return _state==__COMPLETED; |
865 | 287 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
288 |
865 | 289 /* ------------------------------------------------------------ */ |
290 public Request getBaseRequest() | |
291 { | |
292 return _connection.getRequest(); | |
293 } | |
294 | |
295 /* ------------------------------------------------------------ */ | |
296 public ServletRequest getRequest() | |
297 { | |
298 return _connection.getRequest(); | |
299 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
300 |
865 | 301 /* ------------------------------------------------------------ */ |
302 public ServletResponse getResponse() | |
303 { | |
304 return _connection.getResponse(); | |
305 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
306 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
307 |
865 | 308 /* ------------------------------------------------------------ */ |
309 /** | |
310 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
311 */ | |
312 public ServletResponse getServletResponse() | |
313 { | |
314 return _connection.getResponse(); | |
315 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
865 | 317 /* ------------------------------------------------------------ */ |
318 /** | |
319 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
320 */ | |
321 public Object getAttribute(String name) | |
322 { | |
323 return _connection.getRequest().getAttribute(name); | |
324 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
325 |
865 | 326 /* ------------------------------------------------------------ */ |
327 /** | |
328 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
329 */ | |
330 public void removeAttribute(String name) | |
331 { | |
332 _connection.getRequest().removeAttribute(name); | |
333 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
334 |
865 | 335 /* ------------------------------------------------------------ */ |
336 /** | |
337 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
338 */ | |
339 public void setAttribute(String name, Object attribute) | |
340 { | |
341 _connection.getRequest().setAttribute(name,attribute); | |
342 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
344 } |