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