Beispiel #1
0
  private static AtmospherePushConnection getConnectionForUI(UI ui) {
    PushConnection pushConnection = ui.getPushConnection();
    if (pushConnection instanceof AtmospherePushConnection) {
      AtmospherePushConnection apc = (AtmospherePushConnection) pushConnection;
      if (apc.isConnected()) {
        return apc;
      }
    }

    return null;
  }
Beispiel #2
0
        @Override
        public void run(AtmosphereResource resource, UI ui) throws IOException {
          getLogger()
              .log(Level.FINER, "New push connection with transport {0}", resource.transport());
          resource.getResponse().setContentType("text/plain; charset=UTF-8");

          VaadinSession session = ui.getSession();
          if (resource.transport() == TRANSPORT.STREAMING) {
            // IE8 requires a longer padding to work properly if the
            // initial message is small (#11573). Chrome does not work
            // without the original padding...
            WebBrowser browser = session.getBrowser();
            if (browser.isIE() && browser.getBrowserMajorVersion() == 8) {
              resource.padding(LONG_PADDING);
            }

            // Must ensure that the streaming response contains
            // "Connection: close", otherwise iOS 6 will wait for the
            // response to this request before sending another request to
            // the same server (as it will apparently try to reuse the same
            // connection)
            resource.getResponse().addHeader("Connection", "close");
          }

          String requestToken =
              resource.getRequest().getParameter(ApplicationConstants.CSRF_TOKEN_PARAMETER);
          if (!VaadinService.isCsrfTokenValid(session, requestToken)) {
            getLogger()
                .log(
                    Level.WARNING,
                    "Invalid CSRF token in new connection received from {0}",
                    resource.getRequest().getRemoteHost());
            // Refresh on client side, create connection just for
            // sending a message
            sendRefreshAndDisconnect(resource);
            return;
          }

          resource.suspend();

          AtmospherePushConnection connection = new AtmospherePushConnection(ui);
          connection.connect(resource);

          ui.setPushConnection(connection);
        }
Beispiel #3
0
        @Override
        public void run(AtmosphereResource resource, UI ui) throws IOException {
          AtmosphereRequest req = resource.getRequest();

          AtmospherePushConnection connection = getConnectionForUI(ui);

          assert connection != null
              : "Got push from the client "
                  + "even though the connection does not seem to be "
                  + "valid. This might happen if a HttpSession is "
                  + "serialized and deserialized while the push "
                  + "connection is kept open or if the UI has a "
                  + "connection of unexpected type.";

          Reader reader = connection.receiveMessage(req.getReader());
          if (reader == null) {
            // The whole message was not yet received
            return;
          }

          // Should be set up by caller
          VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
          assert vaadinRequest != null;

          try {
            new ServerRpcHandler().handleRpc(ui, reader, vaadinRequest);
            connection.push(false);
          } catch (JSONException e) {
            getLogger().log(Level.SEVERE, "Error writing JSON to response", e);
            // Refresh on client side
            sendRefreshAndDisconnect(resource);
          } catch (InvalidUIDLSecurityKeyException e) {
            getLogger()
                .log(
                    Level.WARNING,
                    "Invalid security key received from {0}",
                    resource.getRequest().getRemoteHost());
            // Refresh on client side
            sendRefreshAndDisconnect(resource);
          }
        }
  @Test
  public void testSerialization() throws Exception {

    UI ui = EasyMock.createNiceMock(UI.class);
    AtmosphereResource resource = EasyMock.createNiceMock(AtmosphereResource.class);

    AtmospherePushConnection connection = new AtmospherePushConnection(ui);
    connection.connect(resource);

    Assert.assertEquals(State.CONNECTED, connection.getState());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    new ObjectOutputStream(baos).writeObject(connection);

    connection =
        (AtmospherePushConnection)
            new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();

    Assert.assertEquals(State.DISCONNECTED, connection.getState());
  }
Beispiel #5
0
 /**
  * Sends a refresh message to the given atmosphere resource. Uses an AtmosphereResource instead of
  * an AtmospherePushConnection even though it might be possible to look up the
  * AtmospherePushConnection from the UI to ensure border cases work correctly, especially when
  * there temporarily are two push connections which try to use the same UI. Using the
  * AtmosphereResource directly guarantees the message goes to the correct recipient.
  *
  * @param resource The atmosphere resource to send refresh to
  */
 private static void sendRefreshAndDisconnect(AtmosphereResource resource) throws IOException {
   AtmospherePushConnection connection = new AtmospherePushConnection(null);
   connection.connect(resource);
   connection.sendMessage(VaadinService.createCriticalNotificationJSON(null, null, null, null));
   connection.disconnect();
 }