Esempio n. 1
0
 /** Generic request handler (GET+POST). */
 protected void doRequest(
     Event anEvent, HttpServletRequest request, HttpServletResponse response) {
   // Must have valid event type.
   String eventType = anEvent.getEventType();
   try {
     // Get Session: either by creating (on Join eventType)
     // or by id (any other eventType, since client is supposed to have
     // joined).
     Session session = null;
     if (eventType.startsWith(Protocol.E_JOIN)) {
       // Join request: create new subscriber
       session = SessionManager.getInstance().createSession(anEvent);
       String userAgent = request.getHeader("User-Agent");
       if (userAgent != null) {
         userAgent = userAgent.toLowerCase();
       } else {
         userAgent = "unknown";
       }
       session.setUserAgent(userAgent);
     } else {
       // Must be a request for existing Session
       // Get id
       String id = anEvent.getField(P_ID);
       // We must have an id value
       if (id == null) {
         response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No id specified");
         Log.warn("Pushlet: bad request, no id specified event=" + eventType);
         return;
       }
       // We have an id: get the session object
       session = SessionManager.getInstance().getSession(id);
       // Check for invalid id
       if (session == null) {
         response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid or expired id: " + id);
         Log.warn("Pushlet:  bad request, no session found id=" + id + " event=" + eventType);
         return;
       }
     }
     // ASSERTION: we have a valid Session
     // Let Controller handle request further
     // including exceptions
     Command command = Command.create(session, anEvent, request, response);
     session.getController().doCommand(command);
   } catch (Throwable t) {
     // Hmm we should never ever get here
     Log.warn("Pushlet:  Exception in doRequest() event=" + eventType, t);
     t.printStackTrace();
     response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
   }
 }
Esempio n. 2
0
  public void init() throws ServletException {
    try {
      // Load configuration (from classpath or WEB-INF root path)
      String webInfPath = getServletContext().getRealPath("/") + "/WEB-INF";
      Config.load(webInfPath);
      Log.init();
      // Start
      Log.info(
          "init() Pushlet Webapp - version="
              + Version.SOFTWARE_VERSION
              + " built="
              + Version.BUILD_DATE);
      // Start session manager
      SessionManager.getInstance().start();
      // Start event Dispatcher
      Dispatcher.getInstance().start();

      if (Config.getBoolProperty(Config.SOURCES_ACTIVATE)) {
        EventSourceManager.start(webInfPath);
      } else {
        Log.info("Not starting local event sources");
      }
    } catch (Throwable t) {
      throw new ServletException("Failed to initialize Pushlet framework " + t, t);
    }
  }
Esempio n. 3
0
 public void destroy() {
   Log.info("destroy(): Exit Pushlet webapp");
   if (Config.getBoolProperty(Config.SOURCES_ACTIVATE)) {
     // Stop local event sources
     EventSourceManager.stop();
   } else {
     Log.info("No local event sources to stop");
   }
   // Should abort all subscribers
   Dispatcher.getInstance().stop();
   // Should stop all sessions
   SessionManager.getInstance().stop();
 }