* @throws java.io.IOException
* @throws javax.servlet.ServletException
*/
public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
HttpEvent event = (HttpEvent) req.getAttribute(HTTP_EVENT);
// Comet is not enabled.
if (event == null) {
logger.error("HttpEvent is null, JBoss APR Not Properly installed");
throw unableToDetectComet;
}
if (logger.isTraceEnabled()) {
logger.trace("Event Type {} for {}", event.getType(), req.getRequestURL().toString());
}
Action action = null;
// For now, we are just interested in HttpEvent.REA
AtmosphereResource r = req.resource();
if (event.getType() == HttpEvent.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) {
// Swallow s Tomcat APR isn't supporting time out
// TODO: Must implement the same functionality using a Scheduler
}
} else if (action.type() == Action.TYPE.RESUME) {
event.close();
} else {
event.close();
}
} else if (event.getType() == HttpEvent.EventType.READ) {
// Not implemented
logger.debug("Receiving bytes, unable to process them.");
} else if (event.getType() == HttpEvent.EventType.EOF
|| event.getType() == HttpEvent.EventType.ERROR
|| event.getType() == HttpEvent.EventType.END) {
if (r != null && r.isResumed()) {
AtmosphereResourceImpl.class.cast(req.resource()).cancel();
} else if (req.getAttribute(SUSPENDED) != null && closeConnectionOnInputStream) {
req.setAttribute(SUSPENDED, null);
action = cancelled(req, res);
} else {
event.close();
}
} else if (event.getType() == HttpEvent.EventType.TIMEOUT) {
action = timedout(req, res);
event.close();
}
return action;
}