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