1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.httpmonitor.proxy; |
---|
16 | |
---|
17 | import java.nio.ByteBuffer; |
---|
18 | import java.util.ArrayList; |
---|
19 | import java.util.HashMap; |
---|
20 | import java.util.List; |
---|
21 | import java.util.Map; |
---|
22 | import java.util.Timer; |
---|
23 | import java.util.TimerTask; |
---|
24 | import java.util.logging.Level; |
---|
25 | |
---|
26 | import javax.servlet.http.HttpServletRequest; |
---|
27 | import javax.servlet.http.HttpServletResponse; |
---|
28 | |
---|
29 | import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent; |
---|
30 | import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException; |
---|
31 | import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; |
---|
32 | import de.ugoe.cs.autoquest.httpmonitor.exchange.Status; |
---|
33 | import de.ugoe.cs.util.console.Console; |
---|
34 | |
---|
35 | /** |
---|
36 | * <p> |
---|
37 | * {@link ExchangeListener}s need to be managed, as there is one instantiated for each proxied |
---|
38 | * request/response. They need to be timed out, if for a longer time, no update is received. All |
---|
39 | * this is done by the ExchangeListenerManager. |
---|
40 | * </p> |
---|
41 | * |
---|
42 | * @author Patrick Harms |
---|
43 | */ |
---|
44 | class ExchangeListenerManager implements HttpMonitorComponent { |
---|
45 | |
---|
46 | /** */ |
---|
47 | private static final long serialVersionUID = 1L; |
---|
48 | |
---|
49 | /** |
---|
50 | * <p> |
---|
51 | * the timeout after which a writer of an inactive client is closed |
---|
52 | * </p> |
---|
53 | */ |
---|
54 | private static final int SESSION_TIMEOUT = 10 * 60 * 1000; |
---|
55 | |
---|
56 | /** |
---|
57 | * <p> |
---|
58 | * the exchange handler forwarded to the exchange listeners |
---|
59 | * </p> |
---|
60 | */ |
---|
61 | private HttpMonitorExchangeHandler exchangeHandler; |
---|
62 | |
---|
63 | /** |
---|
64 | * <p> |
---|
65 | * the mapping of requests handled by the proxy to the respective exchange listeners |
---|
66 | * </p> |
---|
67 | */ |
---|
68 | private Map<HttpServletRequest, ExchangeListener> listeners; |
---|
69 | |
---|
70 | /** |
---|
71 | * <p> |
---|
72 | * a timer used to detect exchange listener timeouts |
---|
73 | * </p> |
---|
74 | */ |
---|
75 | private Timer listenerTimer; |
---|
76 | |
---|
77 | /** |
---|
78 | * <p> |
---|
79 | * creates the exchange listener manager with the exchange handler to be forwarded to the |
---|
80 | * exchange listeners |
---|
81 | * </p> |
---|
82 | * |
---|
83 | * @param exchangeHandler the exchange handler to be forwarded to the exchange listeners |
---|
84 | */ |
---|
85 | ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler) { |
---|
86 | this.exchangeHandler = exchangeHandler; |
---|
87 | } |
---|
88 | |
---|
89 | /* (non-Javadoc) |
---|
90 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init() |
---|
91 | */ |
---|
92 | @Override |
---|
93 | public synchronized void init() throws IllegalStateException, HttpMonitorException { |
---|
94 | Console.traceln(Level.FINER, "initializing exchange listener manager"); |
---|
95 | |
---|
96 | if (listeners != null) { |
---|
97 | throw new IllegalStateException("already initialized"); |
---|
98 | } |
---|
99 | |
---|
100 | listeners = new HashMap<HttpServletRequest, ExchangeListener>(); |
---|
101 | listenerTimer = new Timer(); |
---|
102 | } |
---|
103 | |
---|
104 | /* (non-Javadoc) |
---|
105 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start() |
---|
106 | */ |
---|
107 | @Override |
---|
108 | public synchronized void start() throws IllegalStateException, HttpMonitorException { |
---|
109 | Console.traceln(Level.FINER, "starting exchange listener manager"); |
---|
110 | |
---|
111 | if (listeners == null) { |
---|
112 | throw new IllegalStateException("not initialized"); |
---|
113 | } |
---|
114 | |
---|
115 | listenerTimer.schedule |
---|
116 | (new ListenerMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2); |
---|
117 | } |
---|
118 | |
---|
119 | /* (non-Javadoc) |
---|
120 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop() |
---|
121 | */ |
---|
122 | @Override |
---|
123 | public synchronized void stop() { |
---|
124 | Console.traceln(Level.FINER, "stopping exchange listener manager"); |
---|
125 | |
---|
126 | if (listenerTimer != null) { |
---|
127 | listenerTimer.cancel(); |
---|
128 | } |
---|
129 | |
---|
130 | if (listeners != null) { |
---|
131 | for (ExchangeListener listener : listeners.values()) { |
---|
132 | listener.onFinish(Status.TIMEOUT); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | listeners = null; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * <p> |
---|
141 | * called, when the request was received by the proxy. Calls the appropriate method on |
---|
142 | * the exchange listener. |
---|
143 | * </p> |
---|
144 | * |
---|
145 | * @param request the request of the exchange |
---|
146 | */ |
---|
147 | public void onRequest(HttpServletRequest request) throws IllegalStateException { |
---|
148 | ensureListener(request).onRequest(request); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * <p> |
---|
153 | * called, when some content of the request was processed by the proxy. Calls the appropriate |
---|
154 | * method on the exchange listener. |
---|
155 | * </p> |
---|
156 | * |
---|
157 | * @param request the request of the exchange |
---|
158 | * @param data the processed content of the request of the exchange |
---|
159 | */ |
---|
160 | public void onRequestContent(HttpServletRequest request, ByteBuffer data) |
---|
161 | throws IllegalStateException |
---|
162 | { |
---|
163 | ensureListener(request).onRequestContent(data); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * <p> |
---|
168 | * called, when the response is to be returned by the proxy. Calls the appropriate |
---|
169 | * method on the exchange listener. |
---|
170 | * </p> |
---|
171 | * |
---|
172 | * @param request the request of the exchange |
---|
173 | * @param response the response of the exchange |
---|
174 | */ |
---|
175 | public void onResponse(HttpServletRequest request, HttpServletResponse response) |
---|
176 | throws IllegalStateException |
---|
177 | { |
---|
178 | ensureListener(request).onResponse(response); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * <p> |
---|
183 | * called, when some content of the response was processed by the proxy. Calls the appropriate |
---|
184 | * method on the exchange listener. |
---|
185 | * </p> |
---|
186 | * |
---|
187 | * @param request the request of the exchange |
---|
188 | * @param data the processed content of the response of the exchange |
---|
189 | */ |
---|
190 | public void onResponseContent(HttpServletRequest request, ByteBuffer data) |
---|
191 | throws IllegalStateException |
---|
192 | { |
---|
193 | ensureListener(request).onResponseContent(data); |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * <p> |
---|
198 | * called, when proxy finished proxying a request. Calls the appropriate method on the |
---|
199 | * exchange listener and afterwards cleans up the listener. |
---|
200 | * </p> |
---|
201 | * |
---|
202 | * @param request the request of the exchange |
---|
203 | * @param status the status of the proxying after finalization |
---|
204 | */ |
---|
205 | public void onFinish(HttpServletRequest request, Status status) throws IllegalStateException { |
---|
206 | ensureListener(request).onFinish(status); |
---|
207 | Console.traceln(Level.FINEST, "removing exchange listener for " + request); |
---|
208 | listeners.remove(request); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * <p> |
---|
213 | * convenience method to ensure a listener for a specific HTTP servlet request to be handled. |
---|
214 | * </p> |
---|
215 | */ |
---|
216 | private ExchangeListener ensureListener(HttpServletRequest request) { |
---|
217 | ExchangeListener listener = listeners.get(request); |
---|
218 | |
---|
219 | if (listener == null) { |
---|
220 | synchronized (this) { |
---|
221 | listener = listeners.get(request); |
---|
222 | if (listener == null) { |
---|
223 | Console.traceln(Level.FINEST, "creating exchange listener for " + request); |
---|
224 | listener = new ExchangeListener(exchangeHandler); |
---|
225 | listeners.put(request, listener); |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | return listener; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * <p> |
---|
235 | * internal timer task used for detecting exchange timeouts |
---|
236 | * </p> |
---|
237 | * |
---|
238 | * @author Patrick Harms |
---|
239 | */ |
---|
240 | public class ListenerMonitorTimerTask extends TimerTask { |
---|
241 | |
---|
242 | /* (non-Javadoc) |
---|
243 | * @see java.lang.Runnable#run() |
---|
244 | */ |
---|
245 | @Override |
---|
246 | public void run() { |
---|
247 | synchronized (ExchangeListenerManager.this) { |
---|
248 | List<HttpServletRequest> timeoutRequests = new ArrayList<HttpServletRequest>(); |
---|
249 | for (Map.Entry<HttpServletRequest, ExchangeListener> entry : listeners.entrySet()) { |
---|
250 | ExchangeListener listener = entry.getValue(); |
---|
251 | |
---|
252 | if (System.currentTimeMillis() - listener.getLastUpdate() > SESSION_TIMEOUT) { |
---|
253 | timeoutRequests.add(entry.getKey()); |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | for (HttpServletRequest request : timeoutRequests) { |
---|
258 | ExchangeListener listener = listeners.remove(request); |
---|
259 | listener.onFinish(Status.TIMEOUT); |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|
265 | |
---|
266 | } |
---|