]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/AsyncContextEvent.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / AsyncContextEvent.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.server;
20
21
22 import javax.servlet.AsyncContext;
23 import javax.servlet.AsyncEvent;
24 import javax.servlet.RequestDispatcher;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28
29 import org.eclipse.jetty.server.handler.ContextHandler.Context;
30 import org.eclipse.jetty.util.thread.Scheduler;
31
32 public class AsyncContextEvent extends AsyncEvent implements Runnable
33 {
34     final private Context _context;
35     final private AsyncContextState _asyncContext;
36     private volatile HttpChannelState _state;
37     private ServletContext _dispatchContext;
38     private String _dispatchPath;
39     private volatile Scheduler.Task _timeoutTask;
40     private Throwable _throwable;
41
42     public AsyncContextEvent(Context context,AsyncContextState asyncContext, HttpChannelState state, Request baseRequest, ServletRequest request, ServletResponse response)
43     {
44         super(null,request,response,null);
45         _context=context;
46         _asyncContext=asyncContext;
47         _state=state;
48
49         // If we haven't been async dispatched before
50         if (baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null)
51         {
52             // We are setting these attributes during startAsync, when the spec implies that
53             // they are only available after a call to AsyncContext.dispatch(...);
54
55             // have we been forwarded before?
56             String uri=(String)baseRequest.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
57             if (uri!=null)
58             {
59                 baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,uri);
60                 baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,baseRequest.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH));
61                 baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,baseRequest.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
62                 baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,baseRequest.getAttribute(RequestDispatcher.FORWARD_PATH_INFO));
63                 baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,baseRequest.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING));
64             }
65             else
66             {
67                 baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,baseRequest.getRequestURI());
68                 baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,baseRequest.getContextPath());
69                 baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,baseRequest.getServletPath());
70                 baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,baseRequest.getPathInfo());
71                 baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,baseRequest.getQueryString());
72             }
73         }
74     }
75
76     public ServletContext getSuspendedContext()
77     {
78         return _context;
79     }
80     
81     public Context getContext()
82     {
83         return _context;
84     }
85
86     public ServletContext getDispatchContext()
87     {
88         return _dispatchContext;
89     }
90
91     public ServletContext getServletContext()
92     {
93         return _dispatchContext==null?_context:_dispatchContext;
94     }
95
96     /**
97      * @return The path in the context
98      */
99     public String getPath()
100     {
101         return _dispatchPath;
102     }
103     
104     public void setTimeoutTask(Scheduler.Task task)
105     {
106         _timeoutTask = task;
107     }
108     
109     public void cancelTimeoutTask()
110     {
111         Scheduler.Task task=_timeoutTask;
112         _timeoutTask=null;
113         if (task!=null)
114             task.cancel();
115     }
116
117     @Override
118     public AsyncContext getAsyncContext()
119     {
120         return _asyncContext;
121     }
122     
123     @Override
124     public Throwable getThrowable()
125     {
126         return _throwable;
127     }
128     
129     public void setThrowable(Throwable throwable)
130     {
131         _throwable=throwable;
132     }
133
134     public void setDispatchContext(ServletContext context)
135     {
136         _dispatchContext=context;
137     }
138     
139     public void setDispatchPath(String path)
140     {
141         _dispatchPath=path;
142     }
143     
144     public void completed()
145     {
146         _timeoutTask=null;
147         _asyncContext.reset();
148     }
149
150     public HttpChannelState getHttpChannelState()
151     {
152         return _state;
153     }
154
155     @Override
156     public void run()
157     {
158         Scheduler.Task task=_timeoutTask;
159         _timeoutTask=null;
160         if (task!=null)
161             _state.expired();
162     }
163
164 }