* @throws java.io.IOException
* @throws javax.servlet.ServletException
*/
public Action service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
HttpEvent event = (HttpEvent) req.getAttribute(HTTP_EVENT);
// Comet is not enabled.
if (event == null) {
throw unableToDetectComet;
}
Action action = null;
// For now, we are just interested in HttpEvent.REA
if (event.getType() == HttpEvent.EventType.BEGIN) {
action = suspended(req, res);
if (action.type == Action.TYPE.SUSPEND) {
logger.debug("Suspending response: {}", res);
// Do nothing except setting the times out
try {
if (action.timeout != -1) {
event.setTimeout((int) action.timeout);
}
else {
event.setTimeout(Integer.MAX_VALUE);
}
}
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) {
logger.debug("Resuming response: {}", res);
event.close();
}
else {
event.close();
}
}
else if (event.getType() == HttpEvent.EventType.READ) {
// Not implemente
} else if (event.getType() == HttpEvent.EventType.EOF) {
logger.debug("Client closed connection: response: {}", res);
if (!resumed.remove(event)) {
logger.debug("Client closed connection: response: {}", res);
action = cancelled(req, res);
} else {
logger.debug("Cancelling response: {}", res);
}
event.close();
} else if (event.getType() == HttpEvent.EventType.ERROR) {
event.close();
}
else if (event.getType() == HttpEvent.EventType.END) {
if (!resumed.remove(event)) {
logger.debug("Client closed connection response: {}", res);
action = cancelled(req, res);
}
else {
logger.debug("Cancelling response: {}", res);
}
event.close();
}
else if (event.getType() == HttpEvent.EventType.TIMEOUT) {
logger.debug("Timing out {}", res);
action = timedout(req, res);
event.close();
}
return action;
}