Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/AsyncContinuation.java @ 928:23a57aad34c0
remove isAsync()
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 09 Oct 2016 18:54:32 -0600 |
| parents | 1c1c350fbe4b |
| children | 3191abe890ef |
| 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 */ | |
| 928 | 291 protected synchronized void unhandle() |
| 865 | 292 { |
| 928 | 293 switch(_state) |
| 865 | 294 { |
| 928 | 295 case __DISPATCHED: |
| 296 _state = __UNCOMPLETED; | |
| 297 return; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
298 |
| 928 | 299 default: |
| 300 throw new IllegalStateException(this.getStatusString()); | |
| 865 | 301 } |
| 302 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
303 |
| 865 | 304 /* ------------------------------------------------------------ */ |
| 305 public void dispatch() | |
| 306 { | |
| 307 boolean dispatch=false; | |
| 308 synchronized (this) | |
| 309 { | |
| 310 switch(_state) | |
| 311 { | |
| 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 protected void expired() | |
| 320 { | |
| 321 } | |
| 322 | |
| 323 /* ------------------------------------------------------------ */ | |
| 324 /* (non-Javadoc) | |
| 325 * @see javax.servlet.ServletRequest#complete() | |
| 326 */ | |
| 327 public void complete() | |
| 328 { | |
| 329 // just like resume, except don't set _resumed=true; | |
| 330 boolean dispatch=false; | |
| 331 synchronized (this) | |
| 332 { | |
| 333 switch(_state) | |
| 334 { | |
| 335 case __DISPATCHED: | |
| 336 throw new IllegalStateException(this.getStatusString()); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
| 865 | 338 default: |
| 339 throw new IllegalStateException(this.getStatusString()); | |
| 340 } | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 /* ------------------------------------------------------------ */ | |
| 345 /* (non-Javadoc) | |
| 346 * @see javax.servlet.ServletRequest#complete() | |
| 347 */ | |
| 348 public void errorComplete() | |
| 349 { | |
| 350 // just like complete except can overrule a prior dispatch call; | |
| 351 synchronized (this) | |
| 352 { | |
| 353 switch(_state) | |
| 354 { | |
| 355 default: | |
| 356 throw new IllegalStateException(this.getStatusString()); | |
| 357 } | |
| 358 } | |
| 359 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
360 |
| 865 | 361 /* ------------------------------------------------------------ */ |
| 362 @Override | |
| 363 public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException | |
| 364 { | |
| 365 try | |
| 366 { | |
| 367 // TODO inject | |
| 368 return clazz.newInstance(); | |
| 369 } | |
| 370 catch(Exception e) | |
| 371 { | |
| 372 throw new ServletException(e); | |
| 373 } | |
| 374 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
375 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
376 |
| 865 | 377 /* ------------------------------------------------------------ */ |
| 378 /* (non-Javadoc) | |
| 379 * @see javax.servlet.ServletRequest#complete() | |
| 380 */ | |
| 381 protected void doComplete(Throwable ex) | |
| 382 { | |
| 383 final List<ContinuationListener> cListeners; | |
| 384 final List<AsyncListener> aListeners; | |
| 385 synchronized (this) | |
| 386 { | |
| 387 switch(_state) | |
| 388 { | |
| 389 case __UNCOMPLETED: | |
|
918
7b62446899c6
remove unused states in AsyncContinuation
Franklin Schmidt <fschmidt@gmail.com>
parents:
917
diff
changeset
|
390 _state = __COMPLETED; |
| 865 | 391 cListeners=_continuationListeners; |
| 392 aListeners=_asyncListeners; | |
| 393 break; | |
| 394 | |
| 395 default: | |
| 396 cListeners=null; | |
| 397 aListeners=null; | |
| 398 throw new IllegalStateException(this.getStatusString()); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 if (aListeners!=null) | |
| 403 { | |
| 404 for (AsyncListener listener : aListeners) | |
| 405 { | |
| 406 try | |
| 407 { | |
| 408 if (ex!=null) | |
| 409 { | |
| 410 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_EXCEPTION,ex); | |
| 411 _event.getSuppliedRequest().setAttribute(RequestDispatcher.ERROR_MESSAGE,ex.getMessage()); | |
| 412 listener.onError(_event); | |
| 413 } | |
| 414 else | |
| 415 listener.onComplete(_event); | |
| 416 } | |
| 417 catch(Exception e) | |
| 418 { | |
| 419 LOG.warn("",e); | |
| 420 } | |
| 421 } | |
| 422 } | |
| 423 if (cListeners!=null) | |
| 424 { | |
| 425 for (ContinuationListener listener : cListeners) | |
| 426 { | |
| 427 try | |
| 428 { | |
| 429 listener.onComplete(this); | |
| 430 } | |
| 431 catch(Exception e) | |
| 432 { | |
| 433 LOG.warn("",e); | |
| 434 } | |
| 435 } | |
| 436 } | |
| 437 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
438 |
| 865 | 439 /* ------------------------------------------------------------ */ |
| 440 protected void recycle() | |
| 441 { | |
| 442 synchronized (this) | |
| 443 { | |
| 444 switch(_state) | |
| 445 { | |
| 446 case __DISPATCHED: | |
| 447 throw new IllegalStateException(getStatusString()); | |
| 448 default: | |
| 449 _state=__IDLE; | |
| 450 } | |
| 451 _initial = true; | |
| 452 _responseWrapped=false; | |
| 453 cancelTimeout(); | |
| 454 _timeoutMs=DEFAULT_TIMEOUT; | |
| 455 _continuationListeners=null; | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 /* ------------------------------------------------------------ */ | |
| 460 protected void cancelTimeout() | |
| 461 { | |
| 462 EndPoint endp=_connection.getEndPoint(); | |
| 463 if (endp.isBlocking()) | |
| 464 { | |
| 465 synchronized(this) | |
| 466 { | |
| 467 _expireAt=0; | |
| 468 this.notifyAll(); | |
| 469 } | |
| 470 } | |
| 471 else | |
| 472 { | |
| 473 final AsyncEventState event=_event; | |
| 474 if (event!=null) | |
| 475 { | |
| 476 ((AsyncEndPoint)endp).cancelTimeout(event._timeout); | |
| 477 } | |
| 478 } | |
| 479 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
480 |
| 865 | 481 boolean isUncompleted() |
| 482 { | |
| 483 synchronized (this) | |
| 484 { | |
| 485 return _state==__UNCOMPLETED; | |
| 486 } | |
| 487 } | |
| 488 | |
| 489 public boolean isComplete() | |
| 490 { | |
| 491 synchronized (this) | |
| 492 { | |
| 493 return _state==__COMPLETED; | |
| 494 } | |
| 495 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
496 |
| 865 | 497 /* ------------------------------------------------------------ */ |
| 498 public void dispatch(ServletContext context, String path) | |
| 499 { | |
| 500 _event._dispatchContext=context; | |
| 501 _event.setPath(path); | |
| 502 dispatch(); | |
| 503 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
| 865 | 505 /* ------------------------------------------------------------ */ |
| 506 public void dispatch(String path) | |
| 507 { | |
| 508 _event.setPath(path); | |
| 509 dispatch(); | |
| 510 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
511 |
| 865 | 512 /* ------------------------------------------------------------ */ |
| 513 public Request getBaseRequest() | |
| 514 { | |
| 515 return _connection.getRequest(); | |
| 516 } | |
| 517 | |
| 518 /* ------------------------------------------------------------ */ | |
| 519 public ServletRequest getRequest() | |
| 520 { | |
| 521 if (_event!=null) | |
| 522 return _event.getSuppliedRequest(); | |
| 523 return _connection.getRequest(); | |
| 524 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
525 |
| 865 | 526 /* ------------------------------------------------------------ */ |
| 527 public ServletResponse getResponse() | |
| 528 { | |
| 529 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
| 530 return _event.getSuppliedResponse(); | |
| 531 return _connection.getResponse(); | |
| 532 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
533 |
| 865 | 534 /* ------------------------------------------------------------ */ |
| 535 public void start(final Runnable run) | |
| 536 { | |
| 537 final AsyncEventState event=_event; | |
| 538 if (event!=null) | |
| 539 { | |
|
875
b9aa175d9a29
remove Server.getThreadPool()
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
540 _connection.getServer().threadPool.execute(new Runnable() |
| 865 | 541 { |
| 542 public void run() | |
| 543 { | |
| 544 ((Context)event.getServletContext()).getContextHandler().handle(run); | |
| 545 } | |
| 546 }); | |
| 547 } | |
| 548 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
549 |
| 865 | 550 /* ------------------------------------------------------------ */ |
| 551 public boolean hasOriginalRequestAndResponse() | |
| 552 { | |
| 553 synchronized (this) | |
| 554 { | |
| 555 return (_event!=null && _event.getSuppliedRequest()==_connection._request && _event.getSuppliedResponse()==_connection._response); | |
| 556 } | |
| 557 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
558 |
| 865 | 559 /* ------------------------------------------------------------ */ |
| 560 public ContextHandler getContextHandler() | |
| 561 { | |
| 562 final AsyncEventState event=_event; | |
| 563 if (event!=null) | |
| 564 return ((Context)event.getServletContext()).getContextHandler(); | |
| 565 return null; | |
| 566 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
567 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
568 |
| 865 | 569 /* ------------------------------------------------------------ */ |
| 570 /** | |
| 571 * @see org.eclipse.jetty.continuation.Continuation#getServletResponse() | |
| 572 */ | |
| 573 public ServletResponse getServletResponse() | |
| 574 { | |
| 575 if (_responseWrapped && _event!=null && _event.getSuppliedResponse()!=null) | |
| 576 return _event.getSuppliedResponse(); | |
| 577 return _connection.getResponse(); | |
| 578 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
579 |
| 865 | 580 /* ------------------------------------------------------------ */ |
| 581 /** | |
| 582 * @see org.eclipse.jetty.continuation.Continuation#getAttribute(java.lang.String) | |
| 583 */ | |
| 584 public Object getAttribute(String name) | |
| 585 { | |
| 586 return _connection.getRequest().getAttribute(name); | |
| 587 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
588 |
| 865 | 589 /* ------------------------------------------------------------ */ |
| 590 /** | |
| 591 * @see org.eclipse.jetty.continuation.Continuation#removeAttribute(java.lang.String) | |
| 592 */ | |
| 593 public void removeAttribute(String name) | |
| 594 { | |
| 595 _connection.getRequest().removeAttribute(name); | |
| 596 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
597 |
| 865 | 598 /* ------------------------------------------------------------ */ |
| 599 /** | |
| 600 * @see org.eclipse.jetty.continuation.Continuation#setAttribute(java.lang.String, java.lang.Object) | |
| 601 */ | |
| 602 public void setAttribute(String name, Object attribute) | |
| 603 { | |
| 604 _connection.getRequest().setAttribute(name,attribute); | |
| 605 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
606 |
| 865 | 607 /* ------------------------------------------------------------ */ |
| 608 /** | |
| 609 * @see org.eclipse.jetty.continuation.Continuation#undispatch() | |
| 610 */ | |
| 611 public void undispatch() | |
| 612 { | |
| 613 if (isSuspended()) | |
| 614 { | |
| 615 if (LOG.isDebugEnabled()) | |
| 616 throw new ContinuationThrowable(); | |
| 617 else | |
| 618 throw __exception; | |
| 619 } | |
| 620 throw new IllegalStateException("!suspended"); | |
| 621 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
622 |
| 865 | 623 /* ------------------------------------------------------------ */ |
| 624 /* ------------------------------------------------------------ */ | |
| 625 public class AsyncTimeout extends Timeout.Task implements Runnable | |
| 626 { | |
| 627 @Override | |
| 628 public void expired() | |
| 629 { | |
| 630 AsyncContinuation.this.expired(); | |
| 631 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
632 |
| 865 | 633 @Override |
| 634 public void run() | |
| 635 { | |
| 636 AsyncContinuation.this.expired(); | |
| 637 } | |
| 638 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
639 |
| 865 | 640 /* ------------------------------------------------------------ */ |
| 641 /* ------------------------------------------------------------ */ | |
| 642 public class AsyncEventState extends AsyncEvent | |
| 643 { | |
| 644 private final ServletContext _suspendedContext; | |
| 645 private ServletContext _dispatchContext; | |
| 646 private String _pathInContext; | |
| 647 private Timeout.Task _timeout= new AsyncTimeout(); | |
| 648 | |
| 649 public AsyncEventState(ServletContext context, ServletRequest request, ServletResponse response) | |
| 650 { | |
| 651 super(AsyncContinuation.this, request,response); | |
| 652 _suspendedContext=context; | |
| 653 // Get the base request So we can remember the initial paths | |
| 654 Request r=_connection.getRequest(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
655 |
| 865 | 656 // If we haven't been async dispatched before |
| 657 if (r.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null) | |
| 658 { | |
| 659 // We are setting these attributes during startAsync, when the spec implies that | |
| 660 // they are only available after a call to AsyncContext.dispatch(...); | |
| 661 | |
| 662 // have we been forwarded before? | |
| 663 String uri=(String)r.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); | |
| 664 if (uri!=null) | |
| 665 { | |
| 666 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri); | |
| 667 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH)); | |
| 668 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH)); | |
| 669 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getAttribute(RequestDispatcher.FORWARD_PATH_INFO)); | |
| 670 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING)); | |
| 671 } | |
| 672 else | |
| 673 { | |
| 674 r.setAttribute(AsyncContext.ASYNC_REQUEST_URI,r.getRequestURI()); | |
| 675 r.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,r.getContextPath()); | |
| 676 r.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,r.getServletPath()); | |
| 677 r.setAttribute(AsyncContext.ASYNC_PATH_INFO,r.getPathInfo()); | |
| 678 r.setAttribute(AsyncContext.ASYNC_QUERY_STRING,r.getQueryString()); | |
| 679 } | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 public ServletContext getSuspendedContext() | |
| 684 { | |
| 685 return _suspendedContext; | |
| 686 } | |
| 687 | |
| 688 public ServletContext getDispatchContext() | |
| 689 { | |
| 690 return _dispatchContext; | |
| 691 } | |
| 692 | |
| 693 public ServletContext getServletContext() | |
| 694 { | |
| 695 return _dispatchContext==null?_suspendedContext:_dispatchContext; | |
| 696 } | |
| 697 | |
| 698 public void setPath(String path) | |
| 699 { | |
| 700 _pathInContext=path; | |
| 701 } | |
| 702 | |
| 703 /* ------------------------------------------------------------ */ | |
| 704 /** | |
| 705 * @return The path in the context | |
| 706 */ | |
| 707 public String getPath() | |
| 708 { | |
| 709 return _pathInContext; | |
| 710 } | |
| 711 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
712 } |
