Пример #1
0
  /** Push Event to client. */
  public void push(Event anEvent) throws IOException {
    Log.debug("BCA event=" + anEvent.toXML());

    // Check if we should refresh
    if (anEvent.getEventType().equals(Protocol.E_REFRESH)) {
      // Append refresh and tail of HTML document
      // Construct the JS callback line to be sent as last line of doc.
      // This will refresh the request using the unique id to determine
      // the subscriber instance on the server. The client will wait for
      // a number of milliseconds.
      long refreshWaitMillis = Long.parseLong(anEvent.getField(P_WAIT));

      // Create servlet request for requesting next events (refresh)
      String url = anEvent.getField(P_URL);
      String jsRefreshTrigger =
          "\n<script language=\"JavaScript\">url=\'"
              + url
              + "\';\n setTimeout(\"refresh()\", "
              + refreshWaitMillis
              + ");\n</script>";

      send(jsRefreshTrigger + END_DOCUMENT);
    } else {
      send(event2JavaScript(anEvent));
    }
  }
Пример #2
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);
   }
 }
Пример #3
0
 /**
  * * 新增方法,sessionId为用户id
  *
  * @param anId
  * @param anEvent
  * @return
  * @throws PushletException
  */
 public static Session create(String anId, Event anEvent) throws PushletException {
   Session session;
   try {
     session =
         (Session)
             Config.getClass(SESSION_CLASS, "nl.justobjects.pushlet.core.Session").newInstance();
   } catch (Throwable t) {
     throw new PushletException("Cannot instantiate Session from config", t);
   }
   // Init session
   session.id = anEvent.getField("id"); // sessionId为有意义的用户id</span><span>
   session.controller = Controller.create(session);
   session.subscriber = Subscriber.create(session);
   return session;
 }
Пример #4
0
  /** Converts the Java Event to a JavaScript function call in browser page. */
  protected String event2JavaScript(Event event) throws IOException {

    // Convert the event to a comma-separated string.
    String jsArgs = "";
    for (Iterator iter = event.getFieldNames(); iter.hasNext(); ) {
      String name = (String) iter.next();
      String value = event.getField(name);
      String nextArgument =
          (jsArgs.equals("") ? "" : ",") + "'" + name + "'" + ", \"" + value + "\"";
      jsArgs += nextArgument;
    }

    // Construct and return the function call */
    return "<script language=\"JavaScript\">parent.push(" + jsArgs + ");</script>";
  }