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