/**
   * Invoked by the Tomcat AIO when a Comet request gets detected.
   *
   * @param req the {@link AtmosphereRequest}
   * @param res the {@link AtmosphereResponse}
   * @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) {
        close(event);
      } else {
        close(event);
      }
    } 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 {
        close(event);
      }
    } else if (event.getType() == HttpEvent.EventType.TIMEOUT) {
      action = timedout(req, res);
      close(event);
    }
    return action;
  }
Exemple #2
0
  /**
   * Perform the filtering that has been configured for this Filter, matching against the specified
   * request property.
   *
   * @param property The property to check against the allow/deny rules
   * @param event The comet event to be filtered
   * @param chain The comet filter chain
   * @exception IOException if an input/output error occurs
   * @exception ServletException if a servlet error occurs
   */
  protected void processCometEvent(String property, HttpEvent event, HttpEventFilterChain chain)
      throws IOException, ServletException {
    HttpServletResponse response = event.getHttpServletResponse();

    if (isAllowed(property)) {
      chain.doFilterEvent(event);
    } else {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
      event.close();
    }
  }
 private void close(HttpEvent event) {
   try {
     event.close();
   } catch (Exception ex) {
     logger.trace("event.close", ex);
   }
 }