Search in sources :

Example 1 with PushMode

use of com.vaadin.flow.shared.communication.PushMode in project flow by vaadin.

the class BootstrapHandler method createAndInitUI.

protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass, VaadinRequest request, VaadinResponse response, VaadinSession session) {
    UI ui = ReflectTools.createInstance(uiClass);
    PushConfiguration pushConfiguration = ui.getPushConfiguration();
    ui.getInternals().setSession(session);
    ui.setLocale(session.getLocale());
    BootstrapContext context = new BootstrapContext(request, response, session, ui);
    Optional<Push> push = context.getPageConfigurationAnnotation(Push.class);
    DeploymentConfiguration deploymentConfiguration = context.getSession().getService().getDeploymentConfiguration();
    PushMode pushMode = push.map(Push::value).orElseGet(deploymentConfiguration::getPushMode);
    pushConfiguration.setPushMode(pushMode);
    pushConfiguration.setPushUrl(deploymentConfiguration.getPushURL());
    push.map(Push::transport).ifPresent(pushConfiguration::setTransport);
    // Set thread local here so it is available in init
    UI.setCurrent(ui);
    ui.doInit(request, session.getNextUIid());
    session.addUI(ui);
    // After init and adding UI to session fire init listeners.
    session.getService().fireUIInitListeners(ui);
    return context;
}
Also used : PushConfiguration(com.vaadin.flow.component.PushConfiguration) UI(com.vaadin.flow.component.UI) Push(com.vaadin.flow.component.page.Push) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) PushMode(com.vaadin.flow.shared.communication.PushMode)

Example 2 with PushMode

use of com.vaadin.flow.shared.communication.PushMode in project flow by vaadin.

the class PushHandler method connectionLost.

void connectionLost(AtmosphereResourceEvent event) {
    if (event == null) {
        getLogger().error("Could not get event. This should never happen.");
        return;
    }
    // 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) {
        getLogger().error("Could not get resource. This should never happen.");
        return;
    }
    VaadinServletRequest vaadinRequest = new VaadinServletRequest(resource.getRequest(), service);
    VaadinSession session;
    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;
    }
    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;
            } 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
        }
    }
}
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) 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 3 with PushMode

use of com.vaadin.flow.shared.communication.PushMode in project flow by vaadin.

the class PushConfigurationImpl method setPushMode.

@Override
public void setPushMode(PushMode pushMode) {
    if (pushMode == null) {
        throw new IllegalArgumentException("Push mode cannot be null");
    }
    VaadinSession session = ui.getSession();
    if (session == null) {
        throw new UIDetachedException("Cannot set the push mode for a detached UI");
    }
    assert session.hasLock();
    if (pushMode.isEnabled() && !session.getService().ensurePushAvailable()) {
        throw new IllegalStateException("Push is not available. See previous log messages for more information.");
    }
    PushMode oldMode = getPushConfigurationMap().getPushMode();
    if (oldMode != pushMode) {
        getPushConfigurationMap().setPushMode(pushMode);
        if (!oldMode.isEnabled() && pushMode.isEnabled()) {
            // The push connection is initially in a disconnected state;
            // the client will establish the connection
            ui.getInternals().setPushConnection(new AtmospherePushConnection(ui));
        }
    // Nothing to do here if disabling push;
    // the client will close the connection
    }
}
Also used : AtmospherePushConnection(com.vaadin.flow.server.communication.AtmospherePushConnection) VaadinSession(com.vaadin.flow.server.VaadinSession) PushMode(com.vaadin.flow.shared.communication.PushMode)

Aggregations

PushMode (com.vaadin.flow.shared.communication.PushMode)3 UI (com.vaadin.flow.component.UI)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 PushConfiguration (com.vaadin.flow.component.PushConfiguration)1 Push (com.vaadin.flow.component.page.Push)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 AtmospherePushConnection (com.vaadin.flow.server.communication.AtmospherePushConnection)1 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)1 JsonException (elemental.json.JsonException)1 IOException (java.io.IOException)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1