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