super(config);
}
@Override
public Action service(final AtmosphereRequest req, final AtmosphereResponse res) throws IOException, ServletException {
Action action = null;
Continuation c = (Continuation) req.getAttribute(Continuation.class.getName());
if (c == null || c.isInitial()) {
action = suspended(req, res);
if (action.type() == Action.TYPE.SUSPEND && req.getAttribute(FrameworkConfig.CANCEL_SUSPEND_OPERATION) == null) {
c = getContinuation(req);
req.setAttribute(Continuation.class.getName(), c);
if (action.timeout() != -1) {
c.setTimeout(action.timeout());
} else {
// Jetty 7 does something really weird if you set it to
// Long.MAX_VALUE, which is to resume automatically.
c.setTimeout(Integer.MAX_VALUE);
}
c.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE));
c.addContinuationListener(new ContinuationListener() {
@Override
public void onComplete(Continuation continuation) {
AtmosphereResourceImpl r = (AtmosphereResourceImpl) req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);
if (r != null) {
try {
r.cancel();
} catch (IOException e) {
}
}
}
@Override
public void onTimeout(Continuation continuation) {
AtmosphereResourceImpl r = (AtmosphereResourceImpl) req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);
if (r != null) {
try {
timedout(r.getRequest(), r.getResponse());
} catch (Throwable t) {
logger.error("", t);
}
} else {
logger.trace("AtmosphereResource was null");
}
try {
continuation.complete();
} catch (Throwable t) {
}
}
});
if (req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE) != null) {
c.suspend(res);
}
} else if (action.type() == Action.TYPE.RESUME) {
// If resume occurs during a suspend operation, stop processing.
Boolean resumeOnBroadcast = (Boolean) req.getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
if (resumeOnBroadcast != null && resumeOnBroadcast) {
return action;
}