Search in sources :

Example 31 with AtmosphereResource

use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.

the class DebugWindowConnectionTest method reload_resourceIsNotSet_reloadCommandIsNotSent.

@Test
public void reload_resourceIsNotSet_reloadCommandIsNotSent() {
    AtmosphereResource resource = Mockito.mock(AtmosphereResource.class);
    Broadcaster broadcaster = Mockito.mock(Broadcaster.class);
    Mockito.when(resource.getBroadcaster()).thenReturn(broadcaster);
    Assert.assertFalse(reload.isLiveReload(resource));
    reload.reload();
    Mockito.verifyNoInteractions(broadcaster);
}
Also used : AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) Broadcaster(org.atmosphere.cpr.Broadcaster) Test(org.junit.Test)

Example 32 with AtmosphereResource

use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.

the class DebugWindowConnectionTest method onConnect_suspend_sayHello.

@Test
public void onConnect_suspend_sayHello() {
    AtmosphereResource resource = Mockito.mock(AtmosphereResource.class);
    Broadcaster broadcaster = Mockito.mock(Broadcaster.class);
    Mockito.when(resource.getBroadcaster()).thenReturn(broadcaster);
    reload.onConnect(resource);
    Assert.assertTrue(reload.isLiveReload(resource));
    Mockito.verify(resource).suspend(-1);
    Mockito.verify(broadcaster).broadcast("{\"command\": \"hello\"}", resource);
}
Also used : AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) Broadcaster(org.atmosphere.cpr.Broadcaster) Test(org.junit.Test)

Example 33 with AtmosphereResource

use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.

the class PushHandler method handleConnectionLost.

private VaadinSession handleConnectionLost(AtmosphereResourceEvent event) {
    if (event == null) {
        getLogger().error("Could not get event. This should never happen.");
        return null;
    }
    // We don't want to use callWithUi here, as it assumes there's a client
    // request active and does requestStart and requestEnd among other
    // things.
    AtmosphereResource resource = event.getResource();
    if (resource == null) {
        return null;
    }
    // In development mode we may have a live-reload push channel
    // that should be closed.
    Optional<BrowserLiveReload> liveReload = BrowserLiveReloadAccessor.getLiveReloadFromService(service);
    if (isDebugWindowConnection(resource) && liveReload.isPresent() && liveReload.get().isLiveReload(resource)) {
        liveReload.get().onDisconnect(resource);
        return null;
    }
    VaadinServletRequest vaadinRequest = new VaadinServletRequest(resource.getRequest(), service);
    VaadinSession session = null;
    try {
        session = service.findVaadinSession(vaadinRequest);
    } catch (SessionExpiredException e) {
        // This happens at least if the server is restarted without
        // preserving the session. After restart the client reconnects, gets
        // a session expired notification and then closes the connection and
        // ends up here
        getLogger().debug("Session expired before push disconnect event was received", e);
        return session;
    }
    UI ui;
    session.lock();
    try {
        VaadinSession.setCurrent(session);
        // Sets UI.currentInstance
        ui = service.findUI(vaadinRequest);
        if (ui == null) {
            /*
                 * UI not found, could be because FF has asynchronously closed
                 * the websocket connection and Atmosphere has already done
                 * cleanup of the request attributes.
                 *
                 * In that case, we still have a chance of finding the right UI
                 * by iterating through the UIs in the session looking for one
                 * using the same AtmosphereResource.
                 */
            ui = findUiUsingResource(resource, session.getUIs());
            if (ui == null) {
                getLogger().debug("Could not get UI. This should never happen," + " except when reloading in Firefox and Chrome -" + " see http://dev.vaadin.com/ticket/14251.");
                return session;
            } else {
                getLogger().info("No UI was found based on data in the request," + " but a slower lookup based on the AtmosphereResource succeeded." + " See http://dev.vaadin.com/ticket/14251 for more details.");
            }
        }
        PushMode pushMode = ui.getPushConfiguration().getPushMode();
        AtmospherePushConnection pushConnection = getConnectionForUI(ui);
        String id = resource.uuid();
        if (pushConnection == null) {
            getLogger().warn("Could not find push connection to close: {} with transport {}", id, resource.transport());
        } else {
            if (!pushMode.isEnabled()) {
                /*
                     * The client is expected to close the connection after push
                     * mode has been set to disabled.
                     */
                getLogger().debug("Connection closed for resource {}", id);
            } else {
                /*
                     * Unexpected cancel, e.g. if the user closes the browser
                     * tab.
                     */
                getLogger().debug("Connection unexpectedly closed for resource {} with transport {}", id, resource.transport());
            }
            pushConnection.connectionLost();
        }
    } catch (final Exception e) {
        callErrorHandler(session, e);
    } finally {
        try {
            session.unlock();
        } catch (Exception e) {
            getLogger().warn("Error while unlocking session", e);
        // can't call ErrorHandler, we (hopefully) don't have a lock
        }
    }
    return session;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) InvalidUIDLSecurityKeyException(com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException) JsonException(elemental.json.JsonException) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) IOException(java.io.IOException) PushMode(com.vaadin.flow.shared.communication.PushMode)

Example 34 with AtmosphereResource

use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.

the class ExcludeSessionBroadcaster method broadcast.

/**
 * a list of sessions will be exclude for this broadcast
 *
 * @param msg
 * @param sessions
 * @return
 */
public Future<Object> broadcast(Object msg, List<HttpSession> sessions) {
    if (destroyed.get()) {
        return futureDone(msg);
    }
    Set<AtmosphereResource> subset = new HashSet<AtmosphereResource>();
    subset.addAll(resources);
    for (AtmosphereResource r : resources) {
        if (!r.getAtmosphereResourceEvent().isCancelled() && sessions.contains(r.getRequest().getSession())) {
            subset.remove(r);
        }
    }
    start();
    Object newMsg = filter(msg);
    if (newMsg == null) {
        return futureDone(msg);
    }
    BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, subset.size());
    dispatchMessages(new Deliver(newMsg, subset, f, msg));
    return f;
}
Also used : Deliver(org.atmosphere.cpr.Deliver) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) BroadcasterFuture(org.atmosphere.cpr.BroadcasterFuture) HashSet(java.util.HashSet)

Example 35 with AtmosphereResource

use of org.atmosphere.cpr.AtmosphereResource in project atmosphere by Atmosphere.

the class OnDisconnectInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    AtmosphereRequest request = ((AtmosphereResourceImpl) r).getRequest(false);
    String uuid = r.uuid();
    if (closeMessage(request)) {
        if (config.resourcesFactory() == null || uuid == null) {
            logger.debug("Illegal state for uuid {} and AtmosphereResourceFactory {}", r.uuid(), config.resourcesFactory());
            return Action.CANCELLED;
        }
        AtmosphereResource ss = config.resourcesFactory().find(uuid);
        if (ss == null) {
            logger.debug("No Suspended Connection found for {}. Using the AtmosphereResource associated with the close message", uuid);
            ss = r;
        }
        logger.debug("AtmosphereResource {} disconnected", uuid);
        // Block websocket closing detection
        ((AtmosphereResourceEventImpl) ss.getAtmosphereResourceEvent()).isClosedByClient(true);
        if (AsynchronousProcessor.class.isAssignableFrom(config.framework().getAsyncSupport().getClass())) {
            ((AsynchronousProcessor) config.framework().getAsyncSupport()).completeLifecycle(ss, false);
        }
        return Action.CANCELLED;
    }
    return Action.CONTINUE;
}
Also used : AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) AtmosphereResourceEventImpl(org.atmosphere.cpr.AtmosphereResourceEventImpl)

Aggregations

AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)37 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)14 IOException (java.io.IOException)10 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)9 Broadcaster (org.atmosphere.cpr.Broadcaster)8 Test (org.junit.Test)7 UI (com.vaadin.flow.component.UI)5 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)4 JsonException (elemental.json.JsonException)4 AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)4 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)3 ServletException (jakarta.servlet.ServletException)3 HashSet (java.util.HashSet)3 Future (java.util.concurrent.Future)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 AtmosphereResourceEventImpl (org.atmosphere.cpr.AtmosphereResourceEventImpl)3