1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50 if (baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI)==null)
51 {
52
53
54
55
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
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
130
131
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.onTimeout();
162 }
163
164 public void addThrowable(Throwable e)
165 {
166 if (_throwable==null)
167 _throwable=e;
168 else
169 _throwable.addSuppressed(e);
170 }
171
172 }