Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 921:a5af9ee7cf91
remove isExpired()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 18:09:50 -0600 |
parents | 3268ddf919d4 |
children | 03f39c8abd6b |
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.AsyncContext; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import javax.servlet.AsyncEvent; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import javax.servlet.AsyncListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import javax.servlet.RequestDispatcher; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import javax.servlet.ServletException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.util.List; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import javax.servlet.ServletContext; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import javax.servlet.ServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import javax.servlet.ServletResponse; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import javax.servlet.http.HttpServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.continuation.Continuation; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import org.eclipse.jetty.continuation.ContinuationThrowable; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.continuation.ContinuationListener; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.server.handler.ContextHandler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.server.handler.ContextHandler.Context; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 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
|
43 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
44 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.thread.Timeout; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 /** Implementation of Continuation and AsyncContext interfaces |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 public class AsyncContinuation implements AsyncContext, Continuation |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 { |
865 | 53 private static final Logger LOG = LoggerFactory.getLogger(AsyncContinuation.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 |
865 | 55 private final static long DEFAULT_TIMEOUT=30000L; |
56 | |
57 private final static ContinuationThrowable __exception = new ContinuationThrowable(); | |
58 | |
59 // STATES: | |
60 // handling() suspend() unhandle() resume() complete() doComplete() | |
61 // startAsync() dispatch() | |
62 // IDLE DISPATCHED | |
63 // DISPATCHED ASYNCSTARTED UNCOMPLETED | |
64 // ASYNCSTARTED ASYNCWAIT REDISPATCHING COMPLETING | |
65 // REDISPATCHING REDISPATCHED | |
66 // ASYNCWAIT REDISPATCH COMPLETING | |
67 // REDISPATCH REDISPATCHED | |
68 // REDISPATCHED ASYNCSTARTED UNCOMPLETED | |
69 // COMPLETING UNCOMPLETED UNCOMPLETED | |
70 // UNCOMPLETED COMPLETED | |
71 // COMPLETED | |
72 private static final int __IDLE=0; // Idle request | |
73 private static final int __DISPATCHED=1; // Request dispatched to filter/servlet | |
74 private static final int __UNCOMPLETED=8; // Request is completable | |
75 private static final int __COMPLETED=9; // Request is complete | |
76 | |
77 /* ------------------------------------------------------------ */ | |
78 protected AbstractHttpConnection _connection; | |
79 private List<AsyncListener> _asyncListeners; | |
80 private List<ContinuationListener> _continuationListeners; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 |
865 | 82 /* ------------------------------------------------------------ */ |
83 private int _state; | |
84 private boolean _initial; | |
85 private boolean _resumed; | |
86 private volatile boolean _responseWrapped; | |
87 private long _timeoutMs=DEFAULT_TIMEOUT; | |
88 private AsyncEventState _event; | |
89 private volatile long _expireAt; | |
90 private volatile boolean _continuation; | |
91 | |
92 /* ------------------------------------------------------------ */ | |
93 protected AsyncContinuation() | |
94 { | |
95 _state=__IDLE; | |
96 _initial=true; | |
97 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
98 |
865 | 99 /* ------------------------------------------------------------ */ |
100 protected void setConnection(final AbstractHttpConnection connection) | |
101 { | |
102 synchronized(this) | |
103 { | |
104 _connection=connection; | |
105 } | |
106 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 |
865 | 108 /* ------------------------------------------------------------ */ |
109 public void addListener(AsyncListener listener) | |
110 { | |
111 synchronized(this) | |
112 { | |
113 if (_asyncListeners==null) | |
114 _asyncListeners=new ArrayList<AsyncListener>(); | |
115 _asyncListeners.add(listener); | |
116 } | |
117 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
118 |
865 | 119 /* ------------------------------------------------------------ */ |
120 public void addListener(AsyncListener listener,ServletRequest request, ServletResponse response) | |
121 { | |
122 synchronized(this) | |
123 { | |
124 // TODO handle the request/response ??? | |
125 if (_asyncListeners==null) | |
126 _asyncListeners=new ArrayList<AsyncListener>(); | |
127 _asyncListeners.add(listener); | |
128 } | |
129 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
130 |
865 | 131 /* ------------------------------------------------------------ */ |
132 public void addContinuationListener(ContinuationListener listener) | |
133 { | |
134 synchronized(this) | |
135 { | |
136 if (_continuationListeners==null) | |
137 _continuationListeners=new ArrayList<ContinuationListener>(); | |
138 _continuationListeners.add(listener); | |
139 } | |
140 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
141 |
865 | 142 /* ------------------------------------------------------------ */ |
143 public void setTimeout(long ms) | |
144 { | |
145 synchronized(this) | |
146 { | |
147 _timeoutMs=ms; | |
148 } | |
149 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
865 | 151 /* ------------------------------------------------------------ */ |
152 public long getTimeout() | |
153 { | |
154 synchronized(this) | |
155 { | |
156 return _timeoutMs; | |
157 } | |
158 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
159 |
865 | 160 /* ------------------------------------------------------------ */ |
161 public AsyncEventState getAsyncEventState() | |
162 { | |
163 synchronized(this) | |
164 { | |
165 return _event; | |
166 } | |
167 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
865 | 169 /* ------------------------------------------------------------ */ |
170 /** | |
171 * @see org.eclipse.jetty.continuation.Continuation#keepWrappers() | |
172 */ | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
173 |
865 | 174 /* ------------------------------------------------------------ */ |
175 /** | |
176 * @see org.eclipse.jetty.continuation.Continuation#isResponseWrapped() | |
177 */ | |
178 public boolean isResponseWrapped() | |
179 { | |
180 return _responseWrapped; | |
181 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
182 |
865 | 183 /* ------------------------------------------------------------ */ |
184 /* (non-Javadoc) | |
185 * @see javax.servlet.ServletRequest#isInitial() | |
186 */ | |
187 public boolean isInitial() | |
188 { | |
189 synchronized(this) | |
190 { | |
191 return _initial; | |
192 } | |
193 } | |
194 | |
195 public boolean isContinuation() | |
196 { | |
197 return _continuation; | |
198 } | |
199 | |
200 /* ------------------------------------------------------------ */ | |
201 /* (non-Javadoc) | |
202 * @see javax.servlet.ServletRequest#isSuspended() | |
203 */ | |
204 public boolean isSuspended() | |
205 { | |
206 synchronized(this) | |
207 { | |
208 switch(_state) | |
209 { | |
210 default: | |
211 return false; | |
212 } | |
213 } | |
214 } | |
215 | |
216 /* ------------------------------------------------------------ */ | |
217 public boolean isSuspending() | |
218 { | |
219 synchronized(this) | |
220 { | |
221 switch(_state) | |
222 { | |
223 default: | |
224 return false; | |
225 } | |
226 } | |
227 } | |
228 | |
229 /* ------------------------------------------------------------ */ | |
230 public boolean isDispatchable() | |
231 { | |
232 synchronized(this) | |
233 { | |
234 switch(_state) | |
235 { | |
236 default: | |
237 return false; | |
238 } | |
239 } | |
240 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
241 |
865 | 242 /* ------------------------------------------------------------ */ |
243 @Override | |
244 public String toString() | |
245 { | |
246 synchronized (this) | |
247 { | |
248 return super.toString()+"@"+getStatusString(); | |
249 } | |
250 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
251 |
865 | 252 /* ------------------------------------------------------------ */ |
253 public String getStatusString() | |
254 { | |
255 synchronized (this) | |
256 { | |
257 return | |
258 ((_state==__IDLE)?"IDLE": | |
259 (_state==__DISPATCHED)?"DISPATCHED": | |
260 (_state==__UNCOMPLETED)?"UNCOMPLETED": | |
261 (_state==__COMPLETED)?"COMPLETE": | |
262 ("UNKNOWN?"+_state))+ | |
263 (_initial?",initial":"")+ | |
921 | 264 (_resumed?",resumed":""); |
865 | 265 } |
266 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
267 |
865 | 268 /* ------------------------------------------------------------ */ |
269 /** | |
270 * @return false if the handling of the request should not proceed | |
271 */ | |
272 protected boolean handling() | |
273 { | |
274 synchronized (this) | |
275 { | |
276 _continuation=false; | |
277 | |
278 switch(_state) | |
279 { | |
280 case __IDLE: | |
281 _initial=true; | |
282 _state=__DISPATCHED; | |
283 if (_asyncListeners!=null) | |
284 _asyncListeners.clear(); | |
285 return true; | |
286 | |
287 default: | |
288 throw new IllegalStateException(this.getStatusString()); | |
289 } | |
290 } | |
291 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 |
865 | 293 /* ------------------------------------------------------------ */ |
294 /** | |
295 * Signal that the HttpConnection has finished handling the request. | |
296 * For blocking connectors, this call may block if the request has | |
297 * been suspended (startAsync called). | |
298 * @return true if handling is complete, false if the request should | |
299 * be handled again (eg because of a resume that happened before unhandle was called) | |
300 */ | |
301 protected boolean unhandle() | |
302 { | |
303 synchronized (this) | |
304 { | |
305 switch(_state) | |
306 { | |
307 case __DISPATCHED: | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
308 _state = __UNCOMPLETED; |
865 | 309 return true; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
310 |
865 | 311 case __IDLE: |
312 throw new IllegalStateException(this.getStatusString()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
313 |
865 | 314 default: |
315 throw new IllegalStateException(this.getStatusString()); | |
316 } | |
317 } | |
318 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
319 |
865 | 320 /* ------------------------------------------------------------ */ |
321 public void dispatch() | |
322 { | |
323 boolean dispatch=false; | |
324 synchronized (this) | |
325 { | |
326 switch(_state) | |
327 { | |
328 default: | |
329 throw new IllegalStateException(this.getStatusString()); | |
330 } | |
331 } | |
332 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
333 |
865 | 334 /* ------------------------------------------------------------ */ |
335 protected void expired() | |
336 { | |
337 } | |
338 | |
339 /* ------------------------------------------------------------ */ | |
340 /* (non-Javadoc) | |
341 * @see javax.servlet.ServletRequest#complete() | |
342 */ | |
343 public void complete() | |
344 { | |
345 // just like resume, except don't set _resumed=true; | |
346 boolean dispatch=false; | |
347 synchronized (this) | |
348 { | |
349 switch(_state) | |
350 { | |
351 case __DISPATCHED: | |
352 throw new IllegalStateException(this.getStatusString()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
353 |
865 | 354 default: |
355 throw new IllegalStateException(this.getStatusString()); | |
356 } | |
357 } | |
358 } | |
359 | |
360 /* ------------------------------------------------------------ */ | |
361 /* (non-Javadoc) | |
362 * @see javax.servlet.ServletRequest#complete() | |
363 */ | |
364 public void errorComplete() | |
365 { | |
366 // just like complete except can overrule a prior dispatch call; | |
367 synchronized (this) | |
368 { | |
369 switch(_state) | |
370 { | |
371 default: | |
372 throw new IllegalStateException(this.getStatusString()); | |
373 } | |
374 } | |
375 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
376 |
865 | 377 /* ------------------------------------------------------------ */ |
378 @Override | |
379 public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException | |
380 { | |
381 try | |
382 { | |
383 // TODO inject | |
384 return clazz.newInstance(); | |
385 } | |
386 catch(Exception e) | |
387 { | |
388 throw new ServletException(e); | |
389 } | |
390 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
391 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
392 |
865 | 393 /* ------------------------------------------------------------ */ |
394 /* (non-Javadoc) | |
395 * @see javax.servlet.ServletRequest#complete() | |
396 */ | |
397 protected void doComplete(Throwable ex) | |
398 { | |
399 final List<ContinuationListener> cListeners; | |
400 final List<AsyncListener> aListeners; | |
401 synchronized (this) | |
402 { | |
403 switch(_state) | |
404 { | |
405 case __UNCOMPLETED: | |
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
406 _state = __COMPLETED; |
865 | 407 cListeners=_continuationListeners; |
408 aListeners=_asyncListeners; | |
409 break; | |
410 | |
411 default: | |
412 cListeners=null; | |
413 aListeners=null; | |
414 throw new IllegalStateException(this.getStatusString()); | |
415 } | |
416 } | |
417 | |
418 if (aListeners!=null) | |
419 { | |
420 for (AsyncListener listener : aListeners) | |
421 { | |
422 try | |
423 { | |
424 if (ex!=null) | |
425 { | |
426 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_EXCEPTION,ex); | |
427 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_MESSAGE,ex.getMessage()); | |
428 listener.onError(_event); | |
429 } | |
430 else | |
431 listener.onComplete(_event); | |
432 } | |
433 catch(Exception e) | |
434 { | |
435 LOG.warn("",e); | |
436 } | |
437 } | |
438 } | |
439 if (cListeners!=null) | |
440 { | |
441 for (ContinuationListener listener : cListeners) | |
442 { | |
443 try | |
444 { | |
445 listener.onComplete(this); | |
446 } | |
447 catch(Exception e) | |
448 { | |
449 LOG.warn("",e); | |
450 } | |
451 } | |
452 } | |
453 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
454 |
865 | 455 /* ------------------------------------------------------------ */ |
456 protected void recycle() | |
457 { | |
458 synchronized (this) | |
459 { | |
460 switch(_state) | |
461 { | |
462 case __DISPATCHED: | |
463 throw new IllegalStateException(getStatusString()); | |
464 default: | |
465 _state=__IDLE; | |
466 } | |
467 _initial = true; | |
468 _resumed=false; | |
469 _responseWrapped=false; | |
470 cancelTimeout(); | |
471 _timeoutMs=DEFAULT_TIMEOUT; | |
472 _continuationListeners=null; | |
473 } | |
474 } | |
475 | |
476 /* ------------------------------------------------------------ */ | |
477 public void cancel() | |
478 { | |
479 synchronized (this) | |
480 { | |
481 cancelTimeout(); | |
482 _continuationListeners=null; | |
483 } | |
484 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
485 |
865 | 486 /* ------------------------------------------------------------ */ |
487 protected void scheduleDispatch() | |
488 { | |
489 EndPoint endp=_connection.getEndPoint(); | |
490 if (!endp.isBlocking()) | |
491 { | |
492 ((AsyncEndPoint)endp).asyncDispatch(); | |
493 } | |
494 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
495 |
865 | 496 /* ------------------------------------------------------------ */ |
497 protected void scheduleTimeout() | |
498 { | |
499 EndPoint endp=_connection.getEndPoint(); | |
500 if (_timeoutMs>0) | |
501 { | |
502 if (endp.isBlocking()) | |
503 { | |
504 synchronized(this) | |
505 { | |
506 _expireAt = System.currentTimeMillis()+_timeoutMs; | |
507 long wait=_timeoutMs; | |
508 while (_expireAt>0 && wait>0 && _connection.getServer().isRunning()) | |
509 { | |
510 try | |
511 { | |
512 this.wait(wait); | |
513 } | |
514 catch (InterruptedException e) | |
515 { | |
516 LOG.trace("",e); | |
517 } | |
518 wait=_expireAt-System.currentTimeMillis(); | |
519 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
520 |
865 | 521 if (_expireAt>0 && wait<=0 && _connection.getServer().isRunning()) |
522 { | |
523 expired(); | |
524 } | |
525 } | |
526 } | |
527 else | |
528 { | |
529 ((AsyncEndPoint)endp).scheduleTimeout(_event._timeout,_timeoutMs); | |
530 } | |
531 } | |
532 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
533 |
865 | 534 /* ------------------------------------------------------------ */ |
535 protected void cancelTimeout() | |
536 { | |
537 EndPoint endp=_connection.getEndPoint(); | |
538 if (endp.isBlocking()) | |
539 { | |
540 synchronized(this) | |
541 { | |
542 _expireAt=0; | |
543 this.notifyAll(); | |
544 } | |
545 } | |
546 else | |
547 { | |
548 final AsyncEventState event=_event; | |
549 if (event!=null) | |
550 { | |
551 ((AsyncEndPoint)endp).cancelTimeout(event._timeout); | |
552 } | |
553 } | |
554 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
555 |
865 | 556 /* ------------------------------------------------------------ */ |
557 boolean isUncompleted() | |
558 { | |
559 synchronized (this) | |
560 { | |
561 return _state==__UNCOMPLETED; | |
562 } | |
563 } | |
564 | |
565 /* ------------------------------------------------------------ */ | |
566 public boolean isComplete() | |
567 { | |
568 synchronized (this) | |
569 { | |
570 return _state==__COMPLETED; | |
571 } | |
572 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
573 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
574 |
865 | 575 /* ------------------------------------------------------------ */ |
576 public boolean isAsync() | |
577 { | |
578 synchronized (this) | |
579 { | |
580 switch(_state) | |
581 { | |
582 case __IDLE: | |
583 case __DISPATCHED: | |
584 case __UNCOMPLETED: | |
585 case __COMPLETED: | |
586 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
587 |
865 | 588 default: |
589 return true; | |
590 } | |
591 } | |
592 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
593 |
865 | 594 /* ------------------------------------------------------------ */ |
595 public void dispatch(ServletContext context, String path) | |
596 { | |
597 _event._dispatchContext=context; | |
598 _event.setPath(path); | |
599 dispatch(); | |
600 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
601 |
865 | 602 /* ------------------------------------------------------------ */ |
603 public void dispatch(String path) | |
604 { | |
605 _event.setPath(path); | |
606 dispatch(); | |
607 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
608 |
865 | 609 /* ------------------------------------------------------------ */ |
610 public Request getBaseRequest() | |
611 { | |
612 return _connection.getRequest(); | |
613 } | |
614 | |
615 /* ------------------------------------------------------------ */ | |
616 public ServletRequest getRequest() | |
617 { | |
618 if (_event!=null) | |
619 return _event.getSuppliedRequest(); | |
620 return _connection.getRequest(); | |
621 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
622 |
865 | 623 /* ------------------------------------------------------------ */ |
624 public ServletResponse getResponse() | |
625 { | |
626 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
627 return _event.getSuppliedResponse(); | |
628 return _connection.getResponse(); | |
629 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
630 |
865 | 631 /* ------------------------------------------------------------ */ |
632 public void start(final Runnable run) | |
633 { | |
634 final AsyncEventState event=_event; | |
635 if (event!=null) | |
636 { | |
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
637 _connection.getServer().threadPool.execute(new Runnable() |
865 | 638 { |
639 public void run() | |
640 { | |
641 ((Context)event.getServletContext()).getContextHandler().handle(run); | |
642 } | |
643 }); | |
644 } | |
645 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
865 | 647 /* ------------------------------------------------------------ */ |
648 public boolean hasOriginalRequestAndResponse() | |
649 { | |
650 synchronized (this) | |
651 { | |
652 return (_event!=null && _event.getSuppliedRequest()==_connection._request && _event.getSuppliedResponse()==_connection._response); | |
653 } | |
654 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
655 |
865 | 656 /* ------------------------------------------------------------ */ |
657 public ContextHandler getContextHandler() | |
658 { | |
659 final AsyncEventState event=_event; | |
660 if (event!=null) | |
661 return ((Context)event.getServletContext()).getContextHandler(); | |
662 return null; | |
663 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
664 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
665 |
865 | 666 /* ------------------------------------------------------------ */ |
667 /** | |
668 * @see Continuation#isResumed() | |
669 */ | |
670 public boolean isResumed() | |
671 { | |
672 synchronized (this) | |
673 { | |
674 return _resumed; | |
675 } | |
676 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
677 |
865 | 678 /* ------------------------------------------------------------ */ |
679 /** | |
680 * @see Continuation#resume() | |
681 */ | |
682 public void resume() | |
683 { | |
684 dispatch(); | |
685 } | |
686 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
687 |
865 | 688 /* ------------------------------------------------------------ */ |
689 /** | |
690 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
691 */ | |
692 public ServletResponse getServletResponse() | |
693 { | |
694 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
695 return _event.getSuppliedResponse(); | |
696 return _connection.getResponse(); | |
697 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
698 |
865 | 699 /* ------------------------------------------------------------ */ |
700 /** | |
701 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
702 */ | |
703 public Object getAttribute(String name) | |
704 { | |
705 return _connection.getRequest().getAttribute(name); | |
706 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
707 |
865 | 708 /* ------------------------------------------------------------ */ |
709 /** | |
710 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
711 */ | |
712 public void removeAttribute(String name) | |
713 { | |
714 _connection.getRequest().removeAttribute(name); | |
715 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
716 |
865 | 717 /* ------------------------------------------------------------ */ |
718 /** | |
719 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
720 */ | |
721 public void setAttribute(String name, Object attribute) | |
722 { | |
723 _connection.getRequest().setAttribute(name,attribute); | |
724 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
725 |
865 | 726 /* ------------------------------------------------------------ */ |
727 /** | |
728 * @see org.eclipse.jetty.continuation.Continuation#undispatch() | |
729 */ | |
730 public void undispatch() | |
731 { | |
732 if (isSuspended()) | |
733 { | |
734 if (LOG.isDebugEnabled()) | |
735 throw new ContinuationThrowable(); | |
736 else | |
737 throw __exception; | |
738 } | |
739 throw new IllegalStateException("!suspended"); | |
740 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
741 |
865 | 742 /* ------------------------------------------------------------ */ |
743 /* ------------------------------------------------------------ */ | |
744 public class AsyncTimeout extends Timeout.Task implements Runnable | |
745 { | |
746 @Override | |
747 public void expired() | |
748 { | |
749 AsyncContinuation.this.expired(); | |
750 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
751 |
865 | 752 @Override |
753 public void run() | |
754 { | |
755 AsyncContinuation.this.expired(); | |
756 } | |
757 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
758 |
865 | 759 /* ------------------------------------------------------------ */ |
760 /* ------------------------------------------------------------ */ | |
761 public class AsyncEventState extends AsyncEvent | |
762 { | |
763 private final ServletContext _suspendedContext; | |
764 private ServletContext _dispatchContext; | |
765 private String _pathInContext; | |
766 private Timeout.Task _timeout= new AsyncTimeout(); | |
767 | |
768 public AsyncEventState(ServletContext context, ServletRequest request, ServletResponse response) | |
769 { | |
770 super(AsyncContinuation.this, request,response); | |
771 _suspendedContext=context; | |
772 // Get the base request So we can remember the initial paths | |
773 Request r=_connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
774 |
865 | 775 // If we haven't been async dispatched before |
776 if (r.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null) | |
777 { | |
778 // We are setting these attributes during startAsync, when the spec implies that | |
779 // they are only available after a call to AsyncContext.dispatch(...); | |
780 | |
781 // have we been forwarded before? | |
782 String uri=(String)r.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); | |
783 if (uri!=null) | |
784 { | |
785 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri); | |
786 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); | |
787 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH)); | |
788 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getAttribute(RequestDispatcher.FORWARD_PATH_INFO)); | |
789 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); | |
790 } | |
791 else | |
792 { | |
793 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,r.getRequestURI()); | |
794 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getContextPath()); | |
795 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getServletPath()); | |
796 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getPathInfo()); | |
797 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getQueryString()); | |
798 } | |
799 } | |
800 } | |
801 | |
802 public ServletContext getSuspendedContext() | |
803 { | |
804 return _suspendedContext; | |
805 } | |
806 | |
807 public ServletContext getDispatchContext() | |
808 { | |
809 return _dispatchContext; | |
810 } | |
811 | |
812 public ServletContext getServletContext() | |
813 { | |
814 return _dispatchContext==null?_suspendedContext:_dispatchContext; | |
815 } | |
816 | |
817 public void setPath(String path) | |
818 { | |
819 _pathInContext=path; | |
820 } | |
821 | |
822 /* ------------------------------------------------------------ */ | |
823 /** | |
824 * @return The path in the context | |
825 */ | |
826 public String getPath() | |
827 { | |
828 return _pathInContext; | |
829 } | |
830 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
831 } |