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