Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 927:1c1c350fbe4b
remove AsyncContinuation.cancel()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 18:34:24 -0600 |
parents | 88b20b775fa2 |
children | 23a57aad34c0 |
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 protected void cancelTimeout() | |
467 { | |
468 EndPoint endp=_connection.getEndPoint(); | |
469 if (endp.isBlocking()) | |
470 { | |
471 synchronized(this) | |
472 { | |
473 _expireAt=0; | |
474 this.notifyAll(); | |
475 } | |
476 } | |
477 else | |
478 { | |
479 final AsyncEventState event=_event; | |
480 if (event!=null) | |
481 { | |
482 ((AsyncEndPoint)endp).cancelTimeout(event._timeout); | |
483 } | |
484 } | |
485 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
486 |
865 | 487 boolean isUncompleted() |
488 { | |
489 synchronized (this) | |
490 { | |
491 return _state==__UNCOMPLETED; | |
492 } | |
493 } | |
494 | |
495 public boolean isComplete() | |
496 { | |
497 synchronized (this) | |
498 { | |
499 return _state==__COMPLETED; | |
500 } | |
501 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
502 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
503 |
865 | 504 public boolean isAsync() |
505 { | |
506 synchronized (this) | |
507 { | |
508 switch(_state) | |
509 { | |
510 case __IDLE: | |
511 case __DISPATCHED: | |
512 case __UNCOMPLETED: | |
513 case __COMPLETED: | |
514 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
515 |
865 | 516 default: |
517 return true; | |
518 } | |
519 } | |
520 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
521 |
865 | 522 /* ------------------------------------------------------------ */ |
523 public void dispatch(ServletContext context, String path) | |
524 { | |
525 _event._dispatchContext=context; | |
526 _event.setPath(path); | |
527 dispatch(); | |
528 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
529 |
865 | 530 /* ------------------------------------------------------------ */ |
531 public void dispatch(String path) | |
532 { | |
533 _event.setPath(path); | |
534 dispatch(); | |
535 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
536 |
865 | 537 /* ------------------------------------------------------------ */ |
538 public Request getBaseRequest() | |
539 { | |
540 return _connection.getRequest(); | |
541 } | |
542 | |
543 /* ------------------------------------------------------------ */ | |
544 public ServletRequest getRequest() | |
545 { | |
546 if (_event!=null) | |
547 return _event.getSuppliedRequest(); | |
548 return _connection.getRequest(); | |
549 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
550 |
865 | 551 /* ------------------------------------------------------------ */ |
552 public ServletResponse getResponse() | |
553 { | |
554 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
555 return _event.getSuppliedResponse(); | |
556 return _connection.getResponse(); | |
557 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
558 |
865 | 559 /* ------------------------------------------------------------ */ |
560 public void start(final Runnable run) | |
561 { | |
562 final AsyncEventState event=_event; | |
563 if (event!=null) | |
564 { | |
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
565 _connection.getServer().threadPool.execute(new Runnable() |
865 | 566 { |
567 public void run() | |
568 { | |
569 ((Context)event.getServletContext()).getContextHandler().handle(run); | |
570 } | |
571 }); | |
572 } | |
573 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
574 |
865 | 575 /* ------------------------------------------------------------ */ |
576 public boolean hasOriginalRequestAndResponse() | |
577 { | |
578 synchronized (this) | |
579 { | |
580 return (_event!=null && _event.getSuppliedRequest()==_connection._request && _event.getSuppliedResponse()==_connection._response); | |
581 } | |
582 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
583 |
865 | 584 /* ------------------------------------------------------------ */ |
585 public ContextHandler getContextHandler() | |
586 { | |
587 final AsyncEventState event=_event; | |
588 if (event!=null) | |
589 return ((Context)event.getServletContext()).getContextHandler(); | |
590 return null; | |
591 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
592 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
593 |
865 | 594 /* ------------------------------------------------------------ */ |
595 /** | |
596 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
597 */ | |
598 public ServletResponse getServletResponse() | |
599 { | |
600 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
601 return _event.getSuppliedResponse(); | |
602 return _connection.getResponse(); | |
603 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
604 |
865 | 605 /* ------------------------------------------------------------ */ |
606 /** | |
607 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
608 */ | |
609 public Object getAttribute(String name) | |
610 { | |
611 return _connection.getRequest().getAttribute(name); | |
612 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
865 | 614 /* ------------------------------------------------------------ */ |
615 /** | |
616 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
617 */ | |
618 public void removeAttribute(String name) | |
619 { | |
620 _connection.getRequest().removeAttribute(name); | |
621 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
622 |
865 | 623 /* ------------------------------------------------------------ */ |
624 /** | |
625 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
626 */ | |
627 public void setAttribute(String name, Object attribute) | |
628 { | |
629 _connection.getRequest().setAttribute(name,attribute); | |
630 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
631 |
865 | 632 /* ------------------------------------------------------------ */ |
633 /** | |
634 * @see org.eclipse.jetty.continuation.Continuation#undispatch() | |
635 */ | |
636 public void undispatch() | |
637 { | |
638 if (isSuspended()) | |
639 { | |
640 if (LOG.isDebugEnabled()) | |
641 throw new ContinuationThrowable(); | |
642 else | |
643 throw __exception; | |
644 } | |
645 throw new IllegalStateException("!suspended"); | |
646 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
647 |
865 | 648 /* ------------------------------------------------------------ */ |
649 /* ------------------------------------------------------------ */ | |
650 public class AsyncTimeout extends Timeout.Task implements Runnable | |
651 { | |
652 @Override | |
653 public void expired() | |
654 { | |
655 AsyncContinuation.this.expired(); | |
656 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
657 |
865 | 658 @Override |
659 public void run() | |
660 { | |
661 AsyncContinuation.this.expired(); | |
662 } | |
663 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
664 |
865 | 665 /* ------------------------------------------------------------ */ |
666 /* ------------------------------------------------------------ */ | |
667 public class AsyncEventState extends AsyncEvent | |
668 { | |
669 private final ServletContext _suspendedContext; | |
670 private ServletContext _dispatchContext; | |
671 private String _pathInContext; | |
672 private Timeout.Task _timeout= new AsyncTimeout(); | |
673 | |
674 public AsyncEventState(ServletContext context, ServletRequest request, ServletResponse response) | |
675 { | |
676 super(AsyncContinuation.this, request,response); | |
677 _suspendedContext=context; | |
678 // Get the base request So we can remember the initial paths | |
679 Request r=_connection.getRequest(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
680 |
865 | 681 // If we haven't been async dispatched before |
682 if (r.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null) | |
683 { | |
684 // We are setting these attributes during startAsync, when the spec implies that | |
685 // they are only available after a call to AsyncContext.dispatch(...); | |
686 | |
687 // have we been forwarded before? | |
688 String uri=(String)r.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); | |
689 if (uri!=null) | |
690 { | |
691 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri); | |
692 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); | |
693 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH)); | |
694 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getAttribute(RequestDispatcher.FORWARD_PATH_INFO)); | |
695 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); | |
696 } | |
697 else | |
698 { | |
699 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,r.getRequestURI()); | |
700 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getContextPath()); | |
701 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getServletPath()); | |
702 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getPathInfo()); | |
703 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getQueryString()); | |
704 } | |
705 } | |
706 } | |
707 | |
708 public ServletContext getSuspendedContext() | |
709 { | |
710 return _suspendedContext; | |
711 } | |
712 | |
713 public ServletContext getDispatchContext() | |
714 { | |
715 return _dispatchContext; | |
716 } | |
717 | |
718 public ServletContext getServletContext() | |
719 { | |
720 return _dispatchContext==null?_suspendedContext:_dispatchContext; | |
721 } | |
722 | |
723 public void setPath(String path) | |
724 { | |
725 _pathInContext=path; | |
726 } | |
727 | |
728 /* ------------------------------------------------------------ */ | |
729 /** | |
730 * @return The path in the context | |
731 */ | |
732 public String getPath() | |
733 { | |
734 return _pathInContext; | |
735 } | |
736 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
737 } |