* @throws javax.servlet.ServletException
*/
public Action service(AtmosphereRequest req, AtmosphereResponse res)
throws IOException, ServletException {
CometEvent event = (CometEvent) req.getAttribute(COMET_EVENT);
// Comet is not enabled.
if (event == null) {
throw new IllegalStateException(unableToDetectComet());
}
Action action = null;
// For now, we are just interested in CometEvent.READ
if (event.getEventType() == EventType.BEGIN) {
action = suspended(req, res);
if (action.type() == Action.TYPE.SUSPEND) {
// Do nothing except setting the times out
try {
if (action.timeout() != -1) {
event.setTimeout((int) action.timeout());
} else {
event.setTimeout(Integer.MAX_VALUE);
}
req.setAttribute(SUSPENDED, true);
} catch (UnsupportedOperationException ex) {
// TODO: Must implement the same functionality using a Scheduler
logger.trace("Warning: CometEvent.setTimeout not supported on this Tomcat instance. " +
" [The Tomcat native connector does not support timeouts on asynchronous I/O.]");
}
req.setAttribute(SUSPENDED, true);
} else {
try {
event.close();
} catch (IllegalStateException ex) {
logger.trace("event.close", ex);
}
}
} else if (event.getEventType() == EventType.READ) {
// Not implemented
} else if (event.getEventSubType() == CometEvent.EventSubType.CLIENT_DISCONNECT) {
if (req.getAttribute(SUSPENDED) != null && closeConnectionOnInputStream) {
req.setAttribute(SUSPENDED, null);
action = cancelled(req, res);
}
try {
event.close();
} catch (IllegalStateException ex) {
logger.trace("event.close", ex);
}
} else if (event.getEventSubType() == CometEvent.EventSubType.TIMEOUT) {
action = timedout(req, res);
try {
event.close();
} catch (IllegalStateException ex) {
logger.trace("event.close", ex);
}
} else if (event.getEventType() == EventType.ERROR) {
try {
event.close();
} catch (IllegalStateException ex) {
logger.trace("event.close", ex);
}
} else if (event.getEventType() == EventType.END) {
if (req.resource() != null && req.resource().isResumed()) {
AtmosphereResourceImpl.class.cast(req.resource()).cancel();
} else if (req.getAttribute(SUSPENDED) != null && closeConnectionOnInputStream) {
req.setAttribute(SUSPENDED, null);
action = cancelled(req, res);
} else {
try {
event.close();
} catch (IllegalStateException ex) {
logger.trace("event.close", ex);
}
}
}