@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); }
@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()); }
/** * 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(); }