/
AsynchronousProcessor.java
executable file
·596 lines (525 loc) · 24 KB
/
AsynchronousProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright 2008-2021 Async-IO.org
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.cpr;
import org.atmosphere.container.Servlet30CometSupport;
import org.atmosphere.util.EndpointMapper;
import org.atmosphere.util.ExecutorsFactory;
import org.atmosphere.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static org.atmosphere.cpr.Action.TYPE.SKIP_ATMOSPHEREHANDLER;
import static org.atmosphere.cpr.ApplicationConfig.PROPERTY_SESSION_CREATE;
import static org.atmosphere.cpr.AtmosphereFramework.AtmosphereHandlerWrapper;
import static org.atmosphere.cpr.FrameworkConfig.ATMOSPHERE_HANDLER_WRAPPER;
import static org.atmosphere.cpr.FrameworkConfig.ATMOSPHERE_RESOURCE;
import static org.atmosphere.cpr.HeaderConfig.X_ATMOSPHERE_ERROR;
import static org.atmosphere.cpr.HeaderConfig.X_ATMOSPHERE_TRANSPORT;
/**
* Base class which implement the semantics of suspending and resuming of a Comet/WebSocket Request.
*
* @author Jeanfrancois Arcand
*/
public abstract class AsynchronousProcessor implements AsyncSupport<AtmosphereResourceImpl> {
private static final Logger logger = LoggerFactory.getLogger(AsynchronousProcessor.class);
protected static final Action timedoutAction = new Action(Action.TYPE.TIMEOUT);
protected static final Action cancelledAction = new Action(Action.TYPE.CANCELLED);
protected final AtmosphereConfig config;
private EndpointMapper<AtmosphereHandlerWrapper> mapper;
private final long closingTime;
private boolean closeOnCancel;
public AsynchronousProcessor(AtmosphereConfig config) {
this.config = config;
closingTime = Long.parseLong(config.getInitParameter(ApplicationConfig.CLOSED_ATMOSPHERE_THINK_TIME, "0"));
closeOnCancel = config.getInitParameter(ApplicationConfig.CLOSE_STREAM_ON_CANCEL, false);
config.startupHook(framework -> mapper = framework.endPointMapper());
}
@Override
public void init(ServletConfig sc) throws ServletException {
}
/**
* Is {@link HttpSession} supported
*
* @return true if supported
*/
protected boolean supportSession() {
return config.isSupportSession();
}
/**
* Is {@link HttpSession} timeout removal supported
*
* @return true if supported
*/
protected boolean allowSessionTimeoutRemoval() {
return config.isSessionTimeoutRemovalAllowed();
}
/**
* Return the container's name.
*/
public String getContainerName() {
return config.getServletConfig().getServletContext().getServerInfo();
}
/**
* All proprietary Comet based {@link Servlet} must invoke the suspended method when the first request comes in.
* The returned value, of type {@link Action}, tells the proprietary Comet {@link Servlet} to suspended or not the
* current {@link AtmosphereResponse}.
*
* @param request the {@link AtmosphereRequest}
* @param response the {@link AtmosphereResponse}
* @return action the Action operation.
*/
public Action suspended(AtmosphereRequest request, AtmosphereResponse response) throws IOException, ServletException {
return action(request, response);
}
/**
* Invoke the {@link AtmosphereHandler#onRequest} method.
*
* @param req the {@link AtmosphereRequest}
* @param res the {@link AtmosphereResponse}
* @return action the Action operation.
*/
Action action(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
if (!Utils.properProtocol(req)) {
logger.debug("Invalid request state.");
res.setStatus(501);
res.addHeader(X_ATMOSPHERE_ERROR, "Websocket protocol not supported");
res.flushBuffer();
return new Action();
}
if (Utils.webSocketEnabled(req) && !supportWebSocket()) {
logger.warn("Websocket protocol not supported");
res.setStatus(501);
res.addHeader(X_ATMOSPHERE_ERROR, "Websocket protocol not supported");
res.flushBuffer();
return new Action();
}
if (config.handlers().isEmpty()) {
logger.error("No AtmosphereHandler found. Make sure you define it inside WEB-INF/atmosphere.xml or annotate using @___Service");
throw new AtmosphereMappingException("No AtmosphereHandler found. Make sure you define it inside WEB-INF/atmosphere.xml or annotate using @___Service");
}
if (res.request() == null) {
res.request(req);
}
if (supportSession()) {
// Create the session needed to support the Resume
// operation from disparate requests.
HttpSession s = req.getSession(config.getInitParameter(PROPERTY_SESSION_CREATE, true));
// https://github.com/Atmosphere/atmosphere/issues/2034
try {
if (s != null && s.isNew()) {
s.setAttribute(getClass().getName(), "");
s.removeAttribute(getClass().getName());
}
} catch (IllegalStateException ex) {
AtmosphereResourceImpl r = (AtmosphereResourceImpl) req.resource();
logger.warn("Session Expired for {}. Closing the connection", req.uuid(), ex);
if (r != null) {
logger.trace("Ending request for {}", r.uuid());
endRequest(r, true);
return Action.CANCELLED;
} else {
logger.trace("Sending error for {}", req.uuid());
res.setStatus(500);
res.addHeader(X_ATMOSPHERE_ERROR, "Session expired");
res.flushBuffer();
return new Action();
}
}
}
req.setAttribute(FrameworkConfig.SUPPORT_SESSION, supportSession());
int tracing = 0;
AtmosphereHandlerWrapper handlerWrapper = map(req);
if (config.getBroadcasterFactory() == null) {
logger.error("Atmosphere is misconfigured and will not work. BroadcasterFactory is null");
return Action.CANCELLED;
}
AtmosphereResourceImpl resource = configureWorkflow(null, handlerWrapper, req, res);
String v = req.getHeader(HeaderConfig.X_ATMO_BINARY);
if (v != null) {
resource.forceBinaryWrite(Boolean.parseBoolean(v));
}
if (resource.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET && !Utils.webSocketEnabled(req) && !Utils.isRunningTest()) {
logger.warn("Transport not matching webSocketEnabled. Ending request for {}", resource.uuid());
return Action.CANCELLED;
}
// handler interceptor lists
LinkedList<AtmosphereInterceptor> invokedInterceptors = handlerWrapper.interceptors;
Action a = invokeInterceptors(invokedInterceptors, resource, tracing);
if (a.type() != Action.TYPE.CONTINUE && a.type() != Action.TYPE.SKIP_ATMOSPHEREHANDLER) {
return a;
}
try {
// Remap occured.
if (req.getAttribute(FrameworkConfig.NEW_MAPPING) != null) {
req.removeAttribute(FrameworkConfig.NEW_MAPPING);
handlerWrapper = map(req);
if (handlerWrapper == null) {
logger.debug("Remap {}", resource.uuid());
throw new AtmosphereMappingException("Invalid state. No AtmosphereHandler maps request for " + req.getRequestURI());
}
resource = configureWorkflow(resource, handlerWrapper, req, res);
resource.setBroadcaster(handlerWrapper.broadcaster);
}
//Unit test mock the request and will throw NPE.
boolean skipAtmosphereHandler = req.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) != null
? (Boolean) req.getAttribute(SKIP_ATMOSPHEREHANDLER.name()) : Boolean.FALSE;
if (!skipAtmosphereHandler) {
try {
logger.trace("\t Last: {}", handlerWrapper.atmosphereHandler.getClass().getName());
handlerWrapper.atmosphereHandler.onRequest(resource);
} catch (IOException t) {
resource.onThrowable(t);
throw t;
}
}
} finally {
postInterceptors(handlerWrapper != null ? handlerWrapper.interceptors : invokedInterceptors, resource);
}
Action action = resource.action();
if (supportSession() && allowSessionTimeoutRemoval() && action.type().equals(Action.TYPE.SUSPEND)) {
// Do not allow times out.
SessionTimeoutSupport.setupTimeout(config, req.getSession(config.getInitParameter(ApplicationConfig.PROPERTY_SESSION_CREATE, true)));
}
logger.trace("Action for {} was {} with transport " + req.getHeader(X_ATMOSPHERE_TRANSPORT), req.resource() != null ? req.resource().uuid() : "null", action);
return action;
}
private AtmosphereResourceImpl configureWorkflow(AtmosphereResourceImpl resource,
AtmosphereHandlerWrapper handlerWrapper,
AtmosphereRequest req, AtmosphereResponse res) {
// Check Broadcaster state. If destroyed, replace it.
Broadcaster b = handlerWrapper.broadcaster;
if (b.isDestroyed()) {
BroadcasterFactory f = config.getBroadcasterFactory();
synchronized (f) {
f.remove(b, b.getID());
try {
handlerWrapper.broadcaster = f.get(b.getID());
} catch (IllegalStateException ex) {
// Something wrong occurred, let's not fail and loookup the value
logger.trace("", ex);
// fallback to lookup
handlerWrapper.broadcaster = f.lookup(b.getID(), true);
}
}
}
if (resource == null) {
resource = (AtmosphereResourceImpl) req.getAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
}
if (resource == null) {
// TODO: cast is dangerous
resource = (AtmosphereResourceImpl)
config.resourcesFactory().create(config, handlerWrapper.broadcaster, res, this, handlerWrapper.atmosphereHandler);
} else {
// TODO: REDESIGN, UGLY.
try {
// Make sure it wasn't set before
resource.getBroadcaster();
} catch (IllegalStateException ex) {
resource.setBroadcaster(handlerWrapper.broadcaster);
}
resource.atmosphereHandler(handlerWrapper.atmosphereHandler);
}
req.setAttribute(ATMOSPHERE_RESOURCE, resource);
req.setAttribute(ATMOSPHERE_HANDLER_WRAPPER, handlerWrapper);
req.setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.FALSE);
return resource;
}
protected void shutdown() {
Collection<AtmosphereResource> c = config.resourcesFactory().findAll();
for (AtmosphereResource r : c) {
try {
r.close();
} catch (IOException e) {
logger.trace("", e);
}
}
}
@Override
public void action(AtmosphereResourceImpl r) {
}
@Override
public AsyncSupport<AtmosphereResourceImpl> complete(AtmosphereResourceImpl r) {
return this;
}
private String path(AtmosphereRequest request) {
String path;
String pathInfo = null;
try {
pathInfo = request.getPathInfo();
} catch (IllegalStateException ex) {
// http://java.net/jira/browse/GRIZZLY-1301
}
if (pathInfo != null) {
path = request.getServletPath() + pathInfo;
} else {
path = request.getServletPath();
}
if (path == null || path.isEmpty()) {
path = "/";
}
return path;
}
public Action invokeInterceptors(List<AtmosphereInterceptor> c, AtmosphereResource r, int tracing) {
Action a = Action.CONTINUE;
try {
for (AtmosphereInterceptor arc : c) {
if (!((AtmosphereResourceImpl) r).isInScope()) {
logger.warn("Request closed during processing {} and transport {}", r.uuid(), r.transport());
return Action.CANCELLED;
}
try {
a = arc.inspect(r);
} catch (Exception ex) {
logger.error("Interceptor " + arc + " crashed. Processing will continue with other interceptor.", ex);
continue;
}
if (a == null) {
logger.trace("Action was null for {}", arc);
a = Action.CANCELLED;
}
boolean skip = a.type() == SKIP_ATMOSPHEREHANDLER;
if (skip) {
logger.trace("AtmosphereInterceptor {} asked to skip the AtmosphereHandler for {}", arc, r.uuid());
r.getRequest().setAttribute(SKIP_ATMOSPHEREHANDLER.name(), Boolean.TRUE);
}
if (a.type() != Action.TYPE.CONTINUE) {
logger.trace("Interceptor {} interrupted the dispatch for {} with " + a, arc, r.uuid());
return a;
}
if (logger.isTraceEnabled()) {
logger.trace("\t {}: {} for {}", tracing++, arc.getClass().getName(), r.uuid());
}
}
return a;
} catch (Throwable t) {
logger.error("", t);
return Action.CANCELLED;
}
}
public void postInterceptors(List<AtmosphereInterceptor> c, AtmosphereResource r) {
AtmosphereInterceptor arc = null;
for (int i = c.size() - 1; i > -1; i--) {
try {
arc = c.get(i);
arc.postInspect(r);
} catch (Exception ex) {
logger.error("Interceptor " + arc + " crashed. Processing will continue with other interceptor.", ex);
continue;
}
}
}
/**
* Return the {@link AtmosphereHandler} mapped to the passed servlet-path.
*
* @param req the {@link AtmosphereResponse}
* @return the {@link AtmosphereHandler} mapped to the passed servlet-path.
*/
protected AtmosphereHandlerWrapper map(AtmosphereRequest req) {
AtmosphereHandlerWrapper atmosphereHandlerWrapper = mapper.map(req, config.handlers());
if (atmosphereHandlerWrapper == null) {
logger.debug("No AtmosphereHandler maps request for {} with mapping {}", req.getRequestURI(), config.handlers());
throw new AtmosphereMappingException("No AtmosphereHandler maps request for " + req.getRequestURI());
}
return atmosphereHandlerWrapper;
}
/**
* All proprietary Comet based {@link Servlet} must invoke the resume method when the Atmosphere's application
* decide to resume the {@link AtmosphereResponse}. The returned value, of type {@link Action}, tells the
* proprietary Comet {@link Servlet} to resume (again), suspended or do nothing with the current {@link AtmosphereResponse}.
*
* @param request the {@link AtmosphereRequest}
* @param response the {@link AtmosphereResponse}
* @return action the Action operation.
*/
public Action resumed(AtmosphereRequest request, AtmosphereResponse response)
throws IOException, ServletException {
AtmosphereResourceImpl r =
(AtmosphereResourceImpl) request.getAttribute(ATMOSPHERE_RESOURCE);
if (r == null) return Action.CANCELLED; // We are cancelled already
AtmosphereHandler atmosphereHandler = r.getAtmosphereHandler();
AtmosphereResourceEvent event = r.getAtmosphereResourceEvent();
if (event != null && event.isResuming() && !event.isCancelled()) {
synchronized (r) {
atmosphereHandler.onStateChange(event);
}
}
return Action.RESUME;
}
/**
* All proprietary Comet based {@link Servlet} must invoke the timedout method when the underlying WebServer time
* out the {@link AtmosphereResponse}. The returned value, of type {@link Action}, tells the proprietary
* Comet {@link Servlet} to resume (again), suspended or do nothing with the current {@link AtmosphereResponse}.
*
* @param req the {@link AtmosphereRequest}
* @param res the {@link AtmosphereResponse}
* @return action the Action operation.
*/
public Action timedout(AtmosphereRequest req, AtmosphereResponse res)
throws IOException, ServletException {
logger.trace("Timing out {}", req);
endRequest((AtmosphereResourceImpl) req.resource(), false);
return timedoutAction;
}
/**
* Cancel or times out an {@link AtmosphereResource} by invoking it's associated {@link AtmosphereHandler#onStateChange(AtmosphereResourceEvent)}
*
* @param r an {@link AtmosphereResource}
* @param cancelled true if cancelled, false if timedout
* @return true if the operation was executed.
*/
public boolean completeLifecycle(final AtmosphereResource r, boolean cancelled) {
if (r != null && !r.isCancelled() && !((AtmosphereResourceImpl) r).getAndSetInClosingPhase()) {
logger.trace("Finishing lifecycle for AtmosphereResource {}", r.uuid());
final AtmosphereResourceImpl impl = (AtmosphereResourceImpl) r;
try {
if (impl.isCancelled()) {
logger.debug("{} is already cancelled", impl.uuid());
return false;
}
AtmosphereResourceEventImpl e = impl.getAtmosphereResourceEvent();
// https://github.com/Atmosphere/atmosphere/issues/1756
// Do not go inside Atmosphere once undeployed to avoid all kind of issues.
if (config.framework().isDestroyed()) {
cancelled = true;
}
if (!e.isClosedByClient()) {
if (cancelled) {
e.setCancelled(cancelled);
} else {
e.setIsResumedOnTimeout(true);
Broadcaster b = r.getBroadcaster();
if (b instanceof DefaultBroadcaster) {
((DefaultBroadcaster) b).broadcastOnResume(r);
}
// TODO: Was it there for legacy reason?
// impl.getAtmosphereResourceEvent().setIsResumedOnTimeout(impl.resumeOnBroadcast());
}
}
invokeAtmosphereHandler(impl);
} catch (Throwable ex) {
// Something wrong happened, ignore the exception
logger.error("Failed to cancel resource: {}", impl.uuid(), ex);
} finally {
try {
impl.notifyListeners();
// Legacy code #1894 that may breaks Jersey when set to false on some old server.
if (closeOnCancel) {
try {
impl.getResponse(false).getOutputStream().close();
} catch (Throwable t) {
try {
impl.getResponse(false).getWriter().close();
} catch (Throwable t2) {
logger.trace("", t2);
}
}
}
impl.setIsInScope(false);
impl.cancel();
} catch (Throwable t) {
logger.debug("completeLifecycle", t);
} finally {
impl._destroy();
}
}
return true;
} else {
logger.trace("AtmosphereResource {} was already cancelled or gc", r != null ? r.uuid() : "null");
return false;
}
}
/**
* Invoke the associated {@link AtmosphereHandler}. This method must be synchronized on an AtmosphereResource.
*
* @param r a {@link AtmosphereResourceImpl}
*/
protected void invokeAtmosphereHandler(AtmosphereResourceImpl r) throws IOException {
AtmosphereRequest req = r.getRequest(false);
try {
// Rely on isInScope instead of synchronization https://github.com/Atmosphere/atmosphere/issues/1865
if (r.isInScope()) {
String disableOnEvent = r.getAtmosphereConfig().getInitParameter(ApplicationConfig.DISABLE_ONSTATE_EVENT);
r.getAtmosphereResourceEvent().setMessage(r.writeOnTimeout());
try {
if (disableOnEvent == null || !disableOnEvent.equals(String.valueOf(true))) {
AtmosphereHandler atmosphereHandler = r.getAtmosphereHandler();
if (atmosphereHandler != null && r.isInScope()) {
try {
Utils.inject(r);
} catch (IllegalAccessException e) {
logger.warn("", e);
}
atmosphereHandler.onStateChange(r.getAtmosphereResourceEvent());
}
}
} catch (IOException ex) {
try {
r.onThrowable(ex);
} catch (Throwable t) {
logger.warn("failed calling onThrowable()", ex);
}
}
} else {
logger.trace("AtmosphereResource out of scope {}", r.uuid());
}
} finally {
Utils.destroyMeteor(req);
}
}
/**
* All proprietary Comet based {@link Servlet} must invoke the cancelled method when the underlying WebServer
* detect that the client closed the connection.
*
* @param req the {@link AtmosphereRequest}
* @param res the {@link AtmosphereResponse}
* @return action the Action operation.
*/
public Action cancelled(final AtmosphereRequest req, final AtmosphereResponse res)
throws IOException, ServletException {
logger.trace("Cancelling {}", req);
final AtmosphereResourceImpl r = (AtmosphereResourceImpl) req.resource();
// Leave a chance to the client to send the disconnect message before processing the connection
if (closingTime > 0) {
ExecutorsFactory.getScheduler(config).schedule(() -> {
endRequest(r, true);
return null;
}, closingTime, TimeUnit.MILLISECONDS);
} else {
if (completeLifecycle(req.resource(), true)) {
config.framework().notify(Action.TYPE.CANCELLED, req, res);
}
}
return cancelledAction;
}
public void endRequest(AtmosphereResourceImpl r, boolean cancel) {
if (completeLifecycle(r, cancel)) {
config.framework().notify(Action.TYPE.CANCELLED, r.getRequest(false), r.getResponse(false));
}
}
@Override
public boolean supportWebSocket() {
return false;
}
}